Bird
Raised Fist0
Redisquery~20 mins

Rate limiter with INCR and EXPIRE in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Rate Limiter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
How does the INCR command help in rate limiting?

In Redis, the INCR command is often used in rate limiting. What is the primary role of INCR in this context?

AIt resets the counter to zero after each request.
BIt increments a counter for each request to track usage.
CIt deletes the key after a certain number of requests.
DIt sets an expiration time for the key automatically.
Attempts:
2 left
💡 Hint

Think about how you count the number of times something happens.

❓ Architecture
intermediate
1:30remaining
What is the role of EXPIRE in a Redis rate limiter?

In a Redis-based rate limiter, why is the EXPIRE command used alongside INCR?

ATo automatically reset the counter after a fixed time window.
BTo increase the counter value by a fixed amount.
CTo permanently store the counter without expiration.
DTo block requests once the limit is reached.
Attempts:
2 left
💡 Hint

Consider how to limit requests within a time frame.

❓ scaling
advanced
2:00remaining
How to handle race conditions when using INCR and EXPIRE for rate limiting?

When multiple clients send requests simultaneously, what is the best way to ensure atomicity of INCR and EXPIRE commands in Redis rate limiting?

AIgnore race conditions as Redis handles them automatically.
BSend INCR and EXPIRE commands separately without any atomic guarantees.
CUse a client-side lock to prevent concurrent requests.
DUse a Redis transaction (MULTI/EXEC) or Lua script to combine INCR and EXPIRE atomically.
Attempts:
2 left
💡 Hint

Think about how to make multiple commands run as one indivisible operation.

❓ tradeoff
advanced
2:00remaining
What is a limitation of using INCR and EXPIRE for rate limiting in Redis?

Which of the following is a known limitation when implementing rate limiting using only INCR and EXPIRE in Redis?

AIt cannot handle sliding window rate limiting precisely.
BIt requires complex client-side logic to increment counters.
CIt automatically blocks requests beyond the limit without configuration.
DIt stores user data permanently without expiration.
Attempts:
2 left
💡 Hint

Consider the difference between fixed window and sliding window rate limiting.

❓ estimation
expert
3:00remaining
Estimate Redis memory usage for rate limiting 1 million users

You want to rate limit 1 million users with a 1-minute window using Redis keys with INCR and EXPIRE. Each key stores a counter as a 64-bit integer. Estimate the approximate memory usage in megabytes (MB) for storing all counters simultaneously.

AAbout 1 GB
BAbout 10 MB
CAbout 100 MB
DAbout 500 MB
Attempts:
2 left
💡 Hint

Consider key overhead, value size, and approximate Redis memory usage per key.

Practice

(1/5)
1. What is the main purpose of using the INCR command in a Redis rate limiter?
easy
A. To increase the count of actions performed by a user
B. To set the expiration time for a key
C. To delete a key from Redis
D. To reset the count to zero

Solution

  1. Step 1: Understand the role of INCR in counting

    The INCR command increments the integer value stored at a key by one, which is used to count user actions.
  2. Step 2: Differentiate from other commands

    Commands like EXPIRE set timeouts, and DEL deletes keys, but INCR specifically increases counts.
  3. Final Answer:

    To increase the count of actions performed by a user -> Option A
  4. Quick Check:

    INCR counts actions = A [OK]
Hint: INCR always adds 1 to a key's value [OK]
Common Mistakes:
  • Confusing INCR with EXPIRE
  • Thinking INCR resets counts
  • Assuming INCR deletes keys
2. Which Redis command syntax correctly sets a key to expire after 60 seconds?
easy
A. EXPIRE key '60s'
B. EXPIRE 60 key
C. EXPIRE key 60
D. SETEX key 60 value

Solution

  1. Step 1: Recall EXPIRE command syntax

    The correct syntax is EXPIRE key seconds, where seconds is an integer.
  2. 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.
  3. Final Answer:

    EXPIRE key 60 -> Option C
  4. Quick Check:

    EXPIRE key seconds = D [OK]
Hint: EXPIRE command uses key then seconds [OK]
Common Mistakes:
  • Swapping key and seconds arguments
  • Using string with units for seconds
  • Confusing SETEX with EXPIRE
3. Given the following Redis commands executed in order:
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?
medium
A. 10
B. 9 or less
C. -1
D. 0

Solution

  1. Step 1: Analyze command sequence

    First INCR creates or increments the key. Then EXPIRE sets TTL to 10 seconds. Another INCR increments the count but does not reset TTL.
  2. 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.
  3. Final Answer:

    9 or less -> Option B
  4. Quick Check:

    TTL after EXPIRE and INCR = B [OK]
Hint: INCR does not reset TTL, so TTL decreases over time [OK]
Common Mistakes:
  • Assuming INCR resets TTL
  • Expecting TTL to remain exactly 10
  • Confusing TTL -1 (no expire) with positive TTL
4. You implemented a rate limiter using INCR and EXPIRE, but the count never resets after the expiration time. What is the most likely cause?
medium
A. The key was deleted manually before expiration
B. INCR command resets the TTL unintentionally
C. Redis server is out of memory
D. EXPIRE was never set on the key

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    EXPIRE was never set on the key -> Option D
  4. Quick Check:

    No EXPIRE means no reset = C [OK]
Hint: Always set EXPIRE once to enable automatic reset [OK]
Common Mistakes:
  • Assuming INCR resets TTL
  • Ignoring that EXPIRE must be set
  • Blaming Redis memory issues incorrectly
5. You want to implement a rate limiter that allows 100 requests per user per minute using Redis with INCR and EXPIRE. Which approach is best to avoid resetting the expiration time on every request?
hard
A. Set EXPIRE only when INCR returns 1
B. Call EXPIRE every time after INCR
C. Use SETEX instead of INCR
D. Delete the key manually after 60 seconds

Solution

  1. Step 1: Understand TTL reset problem

    Calling EXPIRE every time resets TTL, causing the window to slide indefinitely, which is incorrect for fixed window rate limiting.
  2. Step 2: Best practice for setting EXPIRE

    Set EXPIRE only when the key is created (when INCR returns 1), so TTL counts down properly without resetting.
  3. Final Answer:

    Set EXPIRE only when INCR returns 1 -> Option A
  4. Quick Check:

    Set EXPIRE once at first INCR = A [OK]
Hint: Set EXPIRE only on first INCR to avoid TTL reset [OK]
Common Mistakes:
  • Resetting EXPIRE on every request
  • Using SETEX which overwrites count
  • Manually deleting keys instead of TTL
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard