A rate limiter helps control how often a user or system can perform an action. Using Redis commands INCR and EXPIRE makes it simple and fast.
Rate limiter with INCR and EXPIRE in Redis
INCR key EXPIRE key seconds
INCR increases the number stored at key by 1. If the key does not exist, it is set to 0 before incrementing.
EXPIRE sets a time-to-live (TTL) in seconds for the key. After this time, the key is deleted automatically.
INCR user:123:requests EXPIRE user:123:requests 60
INCR api:clientA:limit
EXPIRE api:clientA:limit 3600This design uses Redis to count user requests in a 60-second window. The first request sets the expiration. Each request increments the count. If the count goes above 5, the server blocks further requests until the count resets.
1. Client sends a request. 2. Server runs INCR on key 'user:42:requests'. 3. If the returned count is 1, server sets EXPIRE on the key for 60 seconds. 4. Server checks if count exceeds limit (e.g., 5 requests). 5. If limit exceeded, server rejects request; else, processes it. Data flow: - User request -> Server - Server -> Redis INCR 'user:42:requests' - Redis returns count - Server -> Redis EXPIRE 'user:42:requests' 60 (only if count == 1) - Server decides to allow or block request based on count - Server responds to user
Always set EXPIRE only when the count is 1 to avoid resetting the TTL on every request.
This method works well for simple fixed-window rate limiting but may cause bursts at window edges.
For more precise control, consider sliding window or token bucket algorithms.
Use INCR to count actions and EXPIRE to reset counts automatically.
Set EXPIRE only once when the count starts to avoid TTL reset issues.
This approach is simple, fast, and suitable for many rate limiting needs.
Practice
INCR command in a Redis rate limiter?Solution
Step 1: Understand the role of INCR in counting
TheINCRcommand increments the integer value stored at a key by one, which is used to count user actions.Step 2: Differentiate from other commands
Commands likeEXPIREset timeouts, andDELdeletes keys, butINCRspecifically increases counts.Final Answer:
To increase the count of actions performed by a user -> Option AQuick Check:
INCR counts actions = A [OK]
- Confusing INCR with EXPIRE
- Thinking INCR resets counts
- Assuming INCR deletes keys
Solution
Step 1: Recall EXPIRE command syntax
The correct syntax isEXPIRE key seconds, where seconds is an integer.Step 2: Evaluate options
EXPIRE key 60 matches the syntax. EXPIRE 60 key reverses arguments. SETEX key 60 value is a different command combining SET and EXPIRE. EXPIRE key '60s' uses invalid string format for seconds.Final Answer:
EXPIRE key 60 -> Option CQuick Check:
EXPIRE key seconds = D [OK]
- Swapping key and seconds arguments
- Using string with units for seconds
- Confusing SETEX with EXPIRE
INCR user:123 EXPIRE user:123 10 INCR user:123 TTL user:123
What will the
TTL user:123 command return immediately after these commands?Solution
Step 1: Analyze command sequence
FirstINCRcreates or increments the key. ThenEXPIREsets TTL to 10 seconds. AnotherINCRincrements the count but does not reset TTL.Step 2: Understand TTL behavior
TTL counts down from 10 seconds after EXPIRE. The second INCR does not affect TTL, so TTL will be less than or equal to 9 seconds depending on execution time.Final Answer:
9 or less -> Option BQuick Check:
TTL after EXPIRE and INCR = B [OK]
- Assuming INCR resets TTL
- Expecting TTL to remain exactly 10
- Confusing TTL -1 (no expire) with positive TTL
INCR and EXPIRE, but the count never resets after the expiration time. What is the most likely cause?Solution
Step 1: Identify why count doesn't reset
If the count never resets, it means the key never expired, so the expiration was not set properly.Step 2: Evaluate other options
INCR does not reset TTL, manual deletion would remove the key, and out of memory causes errors but not this behavior.Final Answer:
EXPIRE was never set on the key -> Option DQuick Check:
No EXPIRE means no reset = C [OK]
- Assuming INCR resets TTL
- Ignoring that EXPIRE must be set
- Blaming Redis memory issues incorrectly
INCR and EXPIRE. Which approach is best to avoid resetting the expiration time on every request?Solution
Step 1: Understand TTL reset problem
CallingEXPIREevery time resets TTL, causing the window to slide indefinitely, which is incorrect for fixed window rate limiting.Step 2: Best practice for setting EXPIRE
SetEXPIREonly when the key is created (whenINCRreturns 1), so TTL counts down properly without resetting.Final Answer:
Set EXPIRE only when INCR returns 1 -> Option AQuick Check:
Set EXPIRE once at first INCR = A [OK]
- Resetting EXPIRE on every request
- Using SETEX which overwrites count
- Manually deleting keys instead of TTL
Ready to prove it?
Pass the test and this pattern moves unknown
