Implementing rate limiting

There are so many simple and direct ways you can implement rate limiting. One of the most common and easy ways to do so is to use internal caching on the server.

Another implementation we can use is Redis, which utilizes rate limit patterns as follows:

FUNCTION LIMIT_API_CALL(ip) 
ts = CURRENT_UNIX_TIME() 
keyname = ip+":"+ts 
current = GET(keyname) 
IF current != NULL AND current > 10 THEN 
    ERROR "too many requests per second" 
ELSE 
    MULTI 
        INCR(keyname,1) 
        EXPIRE(keyname,10) 
    EXEC 
    PERFORM_API_CALL() 
END 

Basically, we have a counter for every IP, for every seconds. But these counters are always incremented, setting an expiry time of 10 seconds, so that they'll be removed by Redis automatically when the current second changes.

You can also use the interceptor to implement rate limiting for APIs in your microservice project. There are many algorithms available for implementing rate limiting, either for fixed or distributed systems. You can also use the Kong API to quickly set up rate limiting for APIs.

Let's see the KONG API Management tool in the following section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.122.82