GCRA.LIMIT
GCRA.LIMIT key period burst [count]
- Group
- Rate limiting
- Complexity
- O(1)
- Arity
- -4 (at least 4 words, counting the command name)
- Flags
- write, fast
- Keys
- first 1, last 1, step 1
- HotKV Cloud
- All plans
- On-Premise
- no license needed
GCRA.LIMIT decides whether a request identified by key is allowed under a
limit of burst requests per period milliseconds, and records it if it is.
The check and the update happen in one step, so concurrent clients cannot
both pass on the same state.
The limit refills smoothly rather than in fixed windows: a client that has
been idle can send up to burst requests at once, after which one more
request is allowed every period / burst milliseconds. For example, a
period of 60000 and a burst of 10 allows 10 requests straight away and
then one every 6 seconds.
The key expires on its own once the limit has fully refilled. Use a dedicated key for each limited client or resource.
Arguments
Section titled “Arguments”| Argument | Meaning |
|---|---|
period |
The time window in milliseconds, from 1 to 1000000000000. |
burst |
The number of requests allowed per period, a positive integer. |
count |
How many requests this call stands for (default 1). A count above burst is always denied. |
Return value
Section titled “Return value”An array of five integers, the same in RESP2 and RESP3:
| Element | Meaning |
|---|---|
| 1 | 0 if the request is allowed, 1 if it is denied. |
| 2 | The limit (burst). |
| 3 | Requests still available right now (0 when denied). |
| 4 | Milliseconds to wait before retrying, or -1 when allowed. |
| 5 | Milliseconds until the limit is fully refilled. |
A denied request changes nothing.
Error replies:
ERR invalid period: must be a positive integer (milliseconds)ERR invalid burst: must be a positive integerERR invalid count: must be a positive integerWRONGTYPEif the key holds a value that is not a string, or a string that is not a number (so a limiter never overwrites a value of yours). The key is left as it is.
Examples
Section titled “Examples”127.0.0.1:6379> GCRA.LIMIT ratelimit:user:42 60000 101) (integer) 02) (integer) 103) (integer) 94) (integer) -15) (integer) 6000127.0.0.1:6379> GCRA.LIMIT ratelimit:user:42 60000 101) (integer) 02) (integer) 103) (integer) 84) (integer) -15) (integer) 12000127.0.0.1:6379> GCRA.LIMIT ratelimit:user:42 60000 10 101) (integer) 12) (integer) 103) (integer) 04) (integer) 120005) (integer) 12000The millisecond values depend on the time between calls.
Compatibility
Section titled “Compatibility”HotKV-only command.
See also
Section titled “See also”Was this page helpful?
Thanks for the feedback.