rate_limits: Rewrite the Redis backend with Lua scripts #755
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For #754
Some Redis deployments may not support the
WATCHcommand so the current implementation of the RateLimiter backend cannot work with those. Notably these are RedisClusters - some implementations supportWATCHwith caveats, some others (like AWS ElastiCache Serverless) ban it altogether.This PR replaces the usages
WATCH-MULTI-EXECclient-side pattern with Lua scripts that have the same logic - and are also atomic by design, so we don't have to manually "lock" the keysWATCH. This way the mentioned Redis environments are (mostly) covered.For full compatibility, the
incr_and_sumuse case received special treatment. It's a multi-key command, which, when run against a Cluster, requires that all the keys it operates on belong to the same hash slot. This can be achieved by using Hash tags.To keep this trick specific to the Redis backend, I've delegated the key naming bits from WindowRateLimiter to the backends, in a backwards-compatible way.