Sometimes, we have to limit the application request, it’s called the TPS throttling.

If we use the application with the spring boot, we can’t throttle the TPS.
However, if we use the Nginx which is located in front of the spring boot application and is named the reverse proxy, the Nginx can throttle the TPS.

There is a module of the limit_req_zone on the Nginx, generally, it is used to protect a web server from the DDOS.

I tried to apply the module for regulating the TPS.

Let’s look at this configuration.

limit_req_zone $binary_remote_addr zone=my_zone:10m rate=30r/s;

This configuration is meaning of making a limit zone, it’s name of my_zone, and client ip address used for key, and the keys are saving to maximum 10M of memory.

And when request of clients are more than 30 times per second, it is blocked.

Let’s apply the zone to our configuration.

server {
  location / {
    limit_req zone=my_zone burst=10 nodelay;
    proxy_pass http://spring-boot-application;
  }
}

In this way, it can use the my_zone configuration to apply spring-boot-application.

There is one more thing, you should also check the Burst keyword.
It means making 10 queues, when server is over limit of client request.

But, this configuration is not the result I want.

What’s the problem?

Let’s look it again.

limit_req_zone $binary_remote_addr zone=my_zone:10m rate=30r/s;

This $binary_remote_addr is built-in variable of the Nginx, it has client ip address.

So, change the code to the $request_uri which is also built-in variable of the Nginx, changed code has the URI of client request.

So, we can throttle client request per the URI.

If the same URI requested is over limit, the Nginx will be return to client the 503 http status.

If the other Nginx is placed in front of the Proxy-Nginx, the Proxy-Nginx is to return the 503 status.
At this time, the request of client redirect to the another Proxy_Nginx by the front Nginx.

And finally, I will explain overall by using system configuration map.

image

image

image

image