Skip to content

Conversation

@mikeroll
Copy link
Contributor

@mikeroll mikeroll commented Aug 6, 2025

For #754

Some Redis deployments may not support the WATCH command so the current implementation of the RateLimiter backend cannot work with those. Notably these are RedisClusters - some implementations support WATCH with caveats, some others (like AWS ElastiCache Serverless) ban it altogether.

This PR replaces the usages WATCH-MULTI-EXEC client-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 keys WATCH. This way the mentioned Redis environments are (mostly) covered.

For full compatibility, the incr_and_sum use 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.

@mikeroll mikeroll marked this pull request as ready for review August 20, 2025 12:28
@mikeroll mikeroll force-pushed the ratelimits-lua branch 2 times, most recently from 532cce5 to 3d39837 Compare August 20, 2025 14:04
@mikeroll mikeroll changed the title Rewrite Redis RateLimiterBackend with Lua scripts rate_limits: Rewrite the Redis backend with Lua scripts Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant