Rate-Limit an API (spring MVC)

Johny19 picture Johny19 · Dec 22, 2014 · Viewed 13.6k times · Source

I'm looking the best more efficient way to implement (or use an already setup) rate limiter that would protect all my rest api url. the protection I'm looking at is a "call per second per user limiter"

I had a look on the net and what comes out was the use of either "Redis" or Guava RateLimiter.

To be honest I have never used Redis and I'am really not familiar with it. But by looking on its docs it seems that it has a quite robust rate limiter system.

I have also had a look at Guava's RateLimiter. And it looks a bit easier to use (don't need a redis installation etc...)

So I would like some suggestion of what would be "in my case" the best solution? Is using Redis "too much"?

Have any of you already tried RateLimter? Is this a good solution? Is it scaleable?

PS: I am also open to other solutions than the 2 I aforementioned if you think there are better choices.

Thank you!

Answer

Lukasz R. picture Lukasz R. · Dec 13, 2018

If you are trying to limit access to your Spring-based REST api you should use token-bucket algorithm.

There is bucket4j-spring-boot-starter project which uses bucket4j library to rate-limit access to the REST api. You can configure it via application properties file. There is an option to limit the access based on IP address or username.

If you are using Netflix Zuul you could use Spring Cloud Zuul RateLimit which uses different storage options: Consul, Redis, Spring Data and Bucket4j.