Pull to refresh

A tiny Rate Limiter Library for Spring MVC

Java *

In microservice world the problem of high load is exteremely big especially when we have a REST API which is accessed quite extensively. Why do we need throttling? The main answer is to decrease the load of the service at the moment.

Different frameworks have different solutions, mostly some additional libraries. Also there is a Guava RateLimiter and Bucket4J . What is interesting Spring MVC being one of the most popular solutions for building REST APIs (thank you Spring Boot) doesn't have any built in rate limiter. As for external solutions there not that many ways around.

Today, I would like to present a tiny experimental library specific for Spring MVC. It is called SpringRateLimitter. The library is very tiny ,works in runtime. The idea is to annotate entire rest controller or specific method , than count the number of incoming requests for the annotated URI and based on the values check if we exceed the allowed number of calls. In case of exceeding an HTTP error code 429 is thrown and after the throttling period is over , the endpoint is available again.

So How does it look like. As first step Maven dependency must be added


As second step package with the controllers must be specified applicaiton.yaml or application.properties

  package: org.abondar.industrial.widgetstack.controller

As first example let's see how annotation works on controller level.

public class WidgetController {

In such scenario all the calls of under /widget are throttled. We haven't specified any value for the number of requests or the period of throttling ,so the default values of 1000 requests and 1 millisecond will be used.

As second example let's annotate a specific method.

 @GetMapping(path = "/many",produces ="application/json")
 @RateLimit(requests = 3, period = 5000)
 public ResponseEntity<List<Widget>> 

As you can see you can see specific number of requests and throttling period were set,so they will be used. If controller has its own values, method ones will ignore it. Negative values are not possible and in case of setting them ,compilation will fail.

Instead of conclusion

As you can see the library is tiny and easy to use. This is an experimental version of it, multiple features are missing, so fill free to suggest improvements or new ideas either here or on my Github. The repository of the library is https://github.com/abondar24/SpringRateLimitter

Any feedback will be extremely useful.

Total votes 3: ↑3 and ↓0 +3
Views 2.7K
Comments Leave a comment