2

We are writing a brand new spring-boot-2 application in our organization.We are exploring the options how can we implement throttling on top of our api?What are some of the frameworks available to implement throttling with spring boot?Can we use resilence4j or Netflix Zuul RateLimiter to achieve this?

Rocky4Ever
  • 828
  • 3
  • 12
  • 35

3 Answers3

4

Without using external dependencies you can implement it as a Filter:

public class RpsFilter extends GenericFilterBean {
//...
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        if (rpsGreaterThanMax()) {
            responseSystemBusy((HttpServletResponse) response);
        }
        else {
            chain.doFilter(request, response);
        }
    }
//...
}

And then add it to the filter chain:

@Bean
public FilterRegistrationBean<RpsFilter> rpsFilter(){
    FilterRegistrationBean<RpsFilter> registrationBean = new FilterRegistrationBean<>();
    int maxRps = getMaxRequestsPerSecond(); // from properties file
    registrationBean.setFilter(new RpsFilter(maxRps));
    registrationBean.addUrlPatterns("/api/v1/*"); // url to apply the throttling
    return registrationBean;    
}
Marc
  • 2,738
  • 1
  • 17
  • 21
2

resilience4j RateLimiter is a good option as resilience4j is a light-weight and modular library. There's a minimal configuration. And, it will also publish the metrics to meter registry.

resilience4j is easily integrated with Spring Webflux too. It provides Reactor operators for it.

Documentation: resilience4j RateLimiter

Liquidpie
  • 1,589
  • 2
  • 22
  • 32
  • but my assumption is resilence4j is for external api calls.Can we use on top of our controller too? – Rocky4Ever Jul 10 '20 at 13:04
  • other modules of resilience4j like circuit breaker and retry are for external api calls, but rateLimiter can be used for controller methods/endpoints. It's the same configuration – Liquidpie Jul 10 '20 at 14:13
  • Check if this article helps too throw some light ? https://medium.com/teamarimac/implementing-throttling-in-java-spring-boot-ec4723cfce9f#:~:text=weddini%2Fspring%2Dboot%2Dthrottling%20Library,-When%20we%20consider&text=You%20can%20implement%20throttling%20by,1%20method%20call%20per%20second. – Harsh Apr 27 '22 at 10:17
1

By using weddini dependency

  <dependency>
    <groupId>com.weddini.throttling</groupId>
    <artifactId>spring-boot-throttling-starter</artifactId>
    <version>0.0.9</version>
  </dependency>

you can just use @Throttling annotation on service method to enable throttling with default or custom implementation. for example,

//this is the default implementation
@Throttling
public void serviceMethod() {}

//annotation above is equivalent to this
@Throttling(type = ThrottlingType.RemoteAddr, limit = 1, timeUnit = TimeUnit.SECONDS)
public void serviceMethod() {}
Muhammad Taha
  • 137
  • 1
  • 6