What if you could stop request overloads with just two simple Redis commands?
Why Rate limiter with INCR and EXPIRE in Redis? - Purpose & Use Cases
Imagine you run a popular website where many users send requests at the same time. You try to count each user's requests manually using a simple counter in your app code.
When too many requests come in quickly, your manual counting gets messy and slow.
Counting requests manually means you must store and update counters yourself, which can be slow and cause errors if many users act at once.
You also have to write extra code to reset counters after some time, making your system complex and unreliable.
Using Redis commands INCR and EXPIRE together lets you count requests automatically and reset counts after a set time.
This means Redis handles counting and timing efficiently, so your app stays fast and simple.
if user_id in counters: counters[user_id] += 1 else: counters[user_id] = 1 # Need extra code to reset counters after time
redis.incr(user_key) redis.expire(user_key, window_seconds)
This lets you easily limit how many requests a user can make in a time window, protecting your system from overload.
A social media app limits users to 100 posts per hour to prevent spam, using Redis INCR and EXPIRE to track and reset counts automatically.
Manual counting is slow and error-prone under heavy load.
Redis INCR and EXPIRE automate counting and timing efficiently.
This approach keeps your system fast, simple, and scalable.
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
