Springboot: Implementing throttling

Viewed 3204

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?

2 Answers

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;    
}

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

Related