How to build a distributed throttling system with Nginx, Lua, and Redis

How to build a distributed throttling system with Nginx, Lua, and Redis

Nginx has a rate limiting feature that is easy to use:

This nginx configuration creates a zone called mylimit that limits a user, based on its IP, to be able to only do a single request per minute. To test this, save this config file as nginx.conf and run the command:

We can use curl to test its effectiveness:

As you can see, our first request was just fine, right at the start of the minute 50, but then our next two requests failed because we were restricted by the nginx limit_req directive that we setup to accept only 1 request per minute. To store the counters we used three simple (O(1)) redis operations:

Now it’s the time to integrate the lua rate sliding window algorithm into nginx.

Source: leandromoreira.com.br