-
Notifications
You must be signed in to change notification settings - Fork 327
DEV: (cmds) add GCRA command page and general info. #2943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+526
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eab435c
DEV: (cmds) add GCRA command page and general info.
dwdougherty 8afb220
Apply suggestion from @mich-elle-luna
dwdougherty e0fc990
Address Lior's comments
dwdougherty e456042
Small commit to remove superfluous frontmatter
dwdougherty 34fba09
Address more of Lior's comments
dwdougherty 17baee5
Address yet more of Lior's concerns
dwdougherty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| --- | ||
| acl_categories: | ||
| - '@string' | ||
| - '@write' | ||
| arguments: | ||
| - key_spec_index: 0 | ||
| name: key | ||
| type: key | ||
| - name: max-burst | ||
| type: integer | ||
| - name: tokens-per-period | ||
| type: integer | ||
| - name: period | ||
| type: double | ||
| - name: count | ||
| optional: true | ||
| token: TOKENS | ||
| type: integer | ||
| arity: -5 | ||
| categories: | ||
| - docs | ||
| - develop | ||
| - stack | ||
| - oss | ||
| - rs | ||
| - rc | ||
| - oss | ||
| - kubernetes | ||
| - clients | ||
| command_flags: | ||
| - write | ||
| - denyoom | ||
| - fast | ||
| complexity: O(1) | ||
| description: Rate limit via GCRA (Generic Cell Rate Algorithm). | ||
| group: string | ||
| hidden: false | ||
| key_specs: | ||
| - begin_search: | ||
| index: | ||
| pos: 1 | ||
| find_keys: | ||
| range: | ||
| lastkey: 0 | ||
| limit: 0 | ||
| step: 1 | ||
| flags: | ||
| - rw | ||
| - access | ||
| - update | ||
| linkTitle: GCRA | ||
| since: 8.8.0 | ||
| summary: Rate limit via GCRA (Generic Cell Rate Algorithm). | ||
| syntax_fmt: "GCRA key max-burst tokens-per-period period [TOKENS\_count]" | ||
| title: GCRA | ||
| --- | ||
|
|
||
| Performs rate limiting using the [Generic Cell Rate Algorithm (GCRA)](https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm). | ||
|
|
||
| GCRA is a popular rate limiting algorithm known for its simplicity and speed. Each request (a single call to this command) consumes a number of tokens; the default cost is one token per request. The sustained rate is `tokens-per-period` tokens per `period` seconds, with a minimum spacing (emission interval) of `period / tokens-per-period` seconds between requests. The `max_burst` parameter allows for occasional spikes by granting up to `max_burst` additional tokens that can be consumed at once beyond the sustained rate. | ||
|
|
||
| The implementation is based on the popular [redis-cell](https://github.com/brandur/redis-cell) module with small changes in the API. Unlike redis-cell and most other implementations where `period` is given as an integer number of seconds, this command accepts `period` as a floating-point number for greater flexibility. Internally, time periods are calculated with microsecond granularity. | ||
|
|
||
| The `GCRA` command is used either to establish a new rate limiter (if the key doesn't exist) or use an existing one (if the key exists). | ||
| All the parameters need to be repeated on each call, and clients don't need to validate that the key exists before using a rate limiter. | ||
| Under normal usage, `max-burst`, `tokens-per-period`, and `period` should not change between calls, though this command supports such changes. | ||
|
|
||
| In a typical deployment, the application server calls `GCRA` on each end user's request. Based on the response, the application server either fulfills the end user's request or rejects it. | ||
|
|
||
| See the [rate limiting docs]({{< relref "/develop/using-commands/rate-limiting" >}}) for more information. | ||
|
|
||
| ## Required arguments | ||
|
|
||
| <details open><summary><code>key</code></summary> | ||
|
|
||
| is the key associated with a specific rate limiting case. The key stores the internal state needed for the GCRA algorithm to track request timing. | ||
|
|
||
| </details> | ||
|
|
||
| <details open><summary><code>max-burst</code></summary> | ||
|
|
||
| The maximum number of additional tokens allowed as a burst, above the sustained rate. This controls how many tokens can be consumed at once before rate limiting starts. The total token capacity is `max_burst + 1`. Minimum value: `0`. | ||
|
|
||
| </details> | ||
|
|
||
| <details open><summary><code>tokens-per-period</code></summary> | ||
|
|
||
| The number of tokens replenished per `period`, which defines the sustained rate of the rate limiter. Minimum value: `1`. | ||
|
|
||
| </details> | ||
|
|
||
| <details open><summary><code>period</code></summary> | ||
|
|
||
| The time period in seconds, specified as a floating-point number, over which `tokens-per-period` tokens are replenished. The emission interval (minimum spacing between single-token requests) is `period / tokens-per-period`. Minimum value: `1.0`. | ||
|
|
||
| </details> | ||
|
|
||
| ## Optional arguments | ||
|
|
||
| <details open><summary><code>TOKENS count</code></summary> | ||
|
|
||
| The number of tokens consumed by this request. A higher value drains the token allowance faster, which is useful when different operations have different costs. Default value: `1`. | ||
|
|
||
| </details> | ||
|
|
||
| ## Examples | ||
|
|
||
| Rate limit an API endpoint to 10 tokens per 60 seconds with a burst of 5: | ||
|
|
||
| ``` | ||
| 127.0.0.1:6379> GCRA api:user:123 5 10 60 | ||
| 1) (integer) 0 | ||
| 2) (integer) 6 | ||
| 3) (integer) 5 | ||
| 4) (integer) -1 | ||
| 5) (integer) 30 | ||
| ``` | ||
|
|
||
| The response shows: the request is allowed (`0`), the total token capacity is `6` (`max_burst + 1`), `5` tokens are available immediately, retry-after is `-1` (the request was allowed; no need to retry), and the full token allowance will be restored after `30` seconds. | ||
|
|
||
| After exhausting the token allowance: | ||
|
|
||
| ``` | ||
| 127.0.0.1:6379> GCRA api:user:123 5 10 60 | ||
| 1) (integer) 1 | ||
| 2) (integer) 6 | ||
| 3) (integer) 0 | ||
| 4) (integer) 6 | ||
| 5) (integer) 36 | ||
| ``` | ||
|
|
||
| This time the request is denied (`1`), `0` tokens remain, and the application server should retry after `6` seconds. | ||
|
|
||
| Using the `TOKENS` option to assign a higher cost to a request: | ||
|
|
||
| ``` | ||
| 127.0.0.1:6379> GCRA api:user:123 5 10 60 TOKENS 3 | ||
| 1) (integer) 0 | ||
| 2) (integer) 6 | ||
| 3) (integer) 2 | ||
| 4) (integer) -1 | ||
| 5) (integer) 24 | ||
| ``` | ||
|
|
||
| This request consumes 3 tokens instead of the default 1. | ||
|
|
||
| ## Return information | ||
|
|
||
| {{< multitabs id="return-info" | ||
| tab1="RESP2" | ||
| tab2="RESP3" >}} | ||
|
|
||
| One of the following: | ||
|
|
||
| - Returns an [array]({{< relref "/develop/reference/protocol-spec#arrays" >}}) with exactly 5 elements: | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): `0` if the request is allowed, `1` if the request is denied. | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): The maximum number of tokens that can be requested if no previous requests have been made, or if earlier requests are no longer within the relevant time window. Always equal to `max_burst` + 1 (+1 to allow requests when `max_burst` is 0). | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): the number of remaining tokens that can be requested immediately (the remaining burst). | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): the number of milliseconds until the caller can retry, or -1 if the request was allowed. | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): the number of milliseconds until the full burst will be allowed again. | ||
| - A [simple error reply]({{< relref "/develop/reference/protocol-spec#simple-errors" >}}) for the following cases: wrong number of arguments, | ||
| incorrect value for `max_burst`, `tokens_per_period` <= 1, `period` <= 1, or `tokens` <= 1. | ||
|
|
||
| -tab-sep- | ||
|
|
||
| One of the following: | ||
|
|
||
| - Returns an [array]({{< relref "/develop/reference/protocol-spec#arrays" >}}) with exactly 5 elements: | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): `0` if the request is allowed, `1` if the request is blocked. | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): the maximum number of tokens that can be requested (if no previous requests were made, or if they were made long enough ago). Always equal to `max_burst` + 1 (+1 to allow requests when `max_burst` is 0). | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): the number of remaining tokens that can be requested immediately (the remaining burst). | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): the number of milliseconds until the caller can retry, or -1 if the request was allowed. | ||
| 1. [Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}): the number of milliseconds until the full burst will be allowed again. | ||
| - A [simple error reply]({{< relref "/develop/reference/protocol-spec#simple-errors" >}}) for the following cases: wrong number of arguments, | ||
| incorrect value for `max_burst`, `tokens_per_period` <= 1, `period` <= 1, or `tokens` <= 1. | ||
|
|
||
| {{< /multitabs >}} | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.