Conversation
🤖 Augment PR SummarySummary: This PR adds a built-in GCRA (Generic Cell Rate Algorithm) rate limiter to Redis via a new Changes:
Technical Notes: The implementation uses microsecond time internally ( 🤖 Was this summary useful? React with 👍 or 👎 |
shahsb
left a comment
There was a problem hiding this comment.
Could you explain how TTL is set on the rate‑limiter key? Does it automatically expire after some idle time, or is it managed differently? What happens if a user manually deletes the key?
TTL is set to the amount of time until the next TAT, i.e we don't worry for managing it. After the TTL has passed the next TAT has arrived so we don't need the stored TAT info hence why we automatically delete it. User should be careful to preserve the key before that if the key is to be used for rate limiting again. |
|
Hey, @brandur since this implementation is heavily based on redis-cell we though you might want to take a look |
|
@minchopaskal Thanks Mincho! I read through this. My C-fu is weak enough that I'm not likely to catch any major problems, but it looks good to me — thanks for keeping the same command usage as redis-cell as that'll enable an easy migration for anyone who'd been using that. A ton of people are going to find Redis built-in rate limiting really useful including multiple companies I've worked for in the past. |
add comments, more minor overflow issues
Co-authored-by: debing.sun <debing.sun@redis.com>
Co-authored-by: debing.sun <debing.sun@redis.com>
Co-authored-by: debing.sun <debing.sun@redis.com>
* Implement GRCA (redis/redis#14826) * Flag [SER006]; remove [SER001] * release notes * Update ReleaseNotes.md

What
Implement rate limiting functionality via GCRA algorithm.
Introduce a new command
GCRAto facilitate it.The implementation is heavily based on the popular redis-cell module (by brandur) with small changes in the API.
Why
Rate limiting is a very common use case of redis and GCRA is one of the most popular algorithms used because of its simplicity and speed. Currently rate limiting with GCRA is possible via lua scripts or even client libraries via the relatively recent
SET IFEQ/DIGESTcommands (redis-py example). Implementing it directly inside redis gives us even faster performance.API
Description
Rate limit via GCRA.
requests_per_periodare allowed perperiodat a sustained rate. Thus we have a minimum spacing(emission interval) ofperiod/requests_per_periodseconds between each request.max_burstallows for occasional spikes by granting up tomax_burstadditional requests to be consumed at once. See more in the GCRA wiki.Options
KEY - key related to specific rate limiting case
MAX_BURST - maximum number of tokens allowed as a burst (in addition to the sustained rate). Min: 0
REQUESTS_PER_PERIOD - number of requests allowed per PERIOD. Min: 1
PERIOD - period in seconds as floating point number used for calculating the sustained rate. Min: 1.0
NUM_REQUESTS - cost (or weight) of this rate-limiting request. A higher cost drains the allowance faster. Default: 1
Note
In redis-cell module and most other modules that are based on it PERIOD is given in seconds as integer. We decided to use floating point for greater flexibility. Internally time periods are calculated in microsecond granularity.
Reply
Reply is identical to reply of redis-cell
Note
Medium Risk
Adds a new write command that mutates keys and rewrites itself for replication/expiry, which could affect correctness under replication and edge cases (time/overflow/type handling). Scope is contained but touches core command execution and persistence semantics.
Overview
Introduces a new
GCRAcommand for server-side rate limiting using the Generic Cell Rate Algorithm, with support for fractionalperiodand optionalNUM_REQUESTScost.The implementation stores the theoretical arrival time in a string key, sets an expiry based on the computed next-allowed time, and rewrites the command to a
SET ... PXATfor replication safety; it also adds command metadata (commands.def+ newcommands/gcra.json), wires the new object into the server build (Makefile,server.h), and includes a dedicated unit test suite covering validation, limiting behavior, overflow handling, wrong-type errors, and replication rewriting.Written by Cursor Bugbot for commit 4c3101c. This will update automatically on new commits. Configure here.