(1+readRegionCount) * writeCommandCount + readCommandCount
and plus 1 if analytics is enabled.
The Rate Limit SDK minimizes Redis calls to reduce latency overhead and cost. Number of commands executed by the Rate Limit algorithm depends on the chosen algorithm, as well as the state of the algorithm and the caching.
ip1
. You can imagine that there is a state for every identifier. We name these states in the following manner for the purpose of attributing costs to each one:
State | Success | Explanation |
---|---|---|
First | true | First time the Ratelimit was called with identifier ip1 |
Intermediate | true | Second or some other time the Ratelimit was called with identifier ip1 |
Rate-Limited | false | Requests with identifier ip1 which are rate limited. |
ip1
, PEXPIRE
is called so that the key expires after some time. In the following calls, we still use the same script but don’t call PEXPIRE
. In the rate-limited state, we may avoid using Redis altogether if we can make use of the cache.
ip1
is found in cache, resulting in a “hit” and the case when the identifier ip1
is not found in the cache, resulting in a “miss”. The cache only exists in the runtime environment and is independent of the Redis database. The state of the cache is especially relevant for serverless contexts, where the cache will usually be empty because of a cold start.
Result | Explanation |
---|---|
Hit | Identifier ip1 is found in the runtime cache |
Miss | Identifier ip1 is not found in cache or the value in the cache doesn’t block (rate-limit) the request |
limit()
Cache Result | Algorithm State | Command Count | Commands |
---|---|---|---|
Hit/Miss | First | 3 | EVAL, INCR, PEXPIRE |
Hit/Miss | Intermediate | 2 | EVAL, INCR |
Miss | Rate-Limited | 2 | EVAL, INCR |
Hit | Rate-Limited | 0 | utilized cache |
Cache Result | Algorithm State | Command Count | Commands |
---|---|---|---|
Hit/Miss | First | 5 | EVAL, GET, GET, INCR, PEXPIRE |
Hit/Miss | Intermediate | 4 | EVAL, GET, GET, INCR |
Miss | Rate-Limited | 3 | EVAL, GET, GET |
Hit | Rate-Limited | 0 | utilized cache |
Cache Result | Algorithm State | Command Count | Commands |
---|---|---|---|
Hit/Miss | First/Intermediate | 4 | EVAL, HMGET, HSET, PEXPIRE |
Miss | Rate-Limited | 2 | EVAL, HMGET |
Hit | Rate-Limited | 0 | utilized cache |
getRemaining()
Algorithm | Command Count | Commands |
---|---|---|
Fixed Window | 2 | EVAL, GET |
Sliding Window | 3 | EVAL, GET, GET |
Token Bucket | 2 | EVAL, HMGET |
resetUsedTokens()
SCAN
command and deletes every key that matches with DEL
commands:
Algorithm | Command Count | Commands |
---|---|---|
Fixed Window | 3 | EVAL, SCAN, DEL |
Sliding Window | 4 | EVAL, SCAN, DEL, DEL |
Token Bucket | 3 | EVAL, SCAN, DEL |
blockUntilReady()
limit()
.
limit
call.
Values passed in identifier
, ip
, userAgent
and country
are checked with a single SMISMEMBER
command.
The other command is TTL which is for checking the status of the current ip deny list to figure out whether
it is expired, valid or disabled.
If Auto IP deny list is enabled,
the Ratelimit SDK will update the ip deny list everyday, in the first limit
invocation after 2 AM UTC.
This will consume 9 commands per day.
If a value is found in the deny list at redis, the client saves this value in the cache and denies
any further requests with that value for a minute without calling Redis (except for analytics).
limit
will result in 1 more command since ZINCRBY
will be called to update the analytics.