For developersAtomicity with Lua
#Improving atomicity and performance with Lua
One way to improve our implementation is by moving the responsibility of performing the
INCR and EXPIRE operations from the incrAndExpireKey method, to a Lua script.#Rate Limiting Lua Script
Redis has the ability to execute Lua scripts on the server side. Lua scripts are executed atomically, that is, no other script or command will run while a script is running, which gives us the same transactional semantics as
MULTI / EXEC.Below is a simple Lua script to encapsulate the rate limiting logic. The script returns
true if the request is to be rejected or false otherwise:Place the script under
src/main/resources/scripts. Now, Let's break it down:- Lua scripts in Redis work with keys (
KEYS[]) and arguments (ARGV[]) in our case we are expecting onekeyinKEYS[1](Lua arrays are 1-based) - We retrieve the quota for the key in
requestsby making acallto theGETcommand, returning-1if the key does not exist, and converting the value to a number. - The quota is passed as the first argument (
ARGV[1]) and stored inmax_requests, the expiry in seconds is the second argument and stored inexpiry - The
ifstatement checks whether the request is the first request in the time window or if the number of requests have not exceeded the quota, in which case we run theINCR-EXPIREcommands and retunrfalse(meaning we are not rate limiting and allowing the request through). - If they've exceeded the quote, then we rate limit by returning
true
If you want to learn more about Lua, see Programming in Lua.
#Redis Lua Scripts in Spring Data Redis
Spring Data Redis supports Lua scripting via the class
RedisScript. It handles serialization and intelligently uses the Redis script cache. The cache is populated using the SCRIPT LOAD command. The default ScriptExecutor uses EVALSHA using the SHA1 of the script and falling back to EVAL if the script has not yet been loaded into the cache.Let's add the bean annotated method
script() to load our script from the classpath:#Modifying the Filter to use Lua
Next, we'll modify the filter to include the script as well as the quota; the value that we need to pass to the script:
Now we can modify the
filter method to use the script. Scripts are run using the execute methods of RedisTemplate or ReactiveRedisTemplate. The execute methods use a configurable ScriptExecutor/ReactiveScriptExecutor that inherits the key and value serialization setting of the template to run the scripts:Let's break down the method additions:
- The
filtermethod uses the templateexecutemethod passing the script, keys and arguments. - We expect a
singleresult (trueorfalse). Thesinglemethod takes a default value to be returned in case we get an empty result. - Finally, we use the
flatMapmethod to grab the value:- If it is
truewe reject the request with an HTTP 429. - If it is
falsewe handle the request
- If it is
#Applying the filter
Let's add a configurable
@Value annotated Long value to the FixedWindowRateLimiterApplication to hold the request quota.In our
application.properties we'll set it to a max of 20 request per minute:To invoke the filter we use the newly modified constructor, passing the template, the script, and the
maxRequestPerMinute value:#Testing with curl
Using our trusty curl loop:
You should see the 21st request being rejected:
If we run Redis in monitor mode, we should see the Lua calls to
EVALSHA, followed by the call to GET for a rejected request, and the same plus calls to INCR and EXPIRE for an allowed request:The complete code for this implementation is under the branch
with_lua.