Bird
Raised Fist0
Redisquery~20 mins

Key expiry for memory management in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Expiry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this Redis command sequence?
Consider the following Redis commands executed in order:

SET session:12345 user1
EXPIRE session:12345 10
TTL session:12345

What will the TTL command return immediately after setting the expiry?
Redis
SET session:12345 user1
EXPIRE session:12345 10
TTL session:12345
A0
B-1
C-2
D10
Attempts:
2 left
💡 Hint
TTL returns the remaining time to live in seconds for a key with expiry.
🧠 Conceptual
intermediate
2:00remaining
Which Redis key expiry behavior is correct?
In Redis, what happens when a key with an expiry time reaches zero?
AThe key remains until a client tries to access it, then it is deleted.
BThe key is deleted only during periodic eviction cycles.
CThe key is immediately deleted from memory.
DThe key's expiry time resets automatically to prevent deletion.
Attempts:
2 left
💡 Hint
Redis uses lazy expiration to save CPU resources.
📝 Syntax
advanced
2:00remaining
Which command correctly sets a key to expire in 5 minutes?
Choose the correct Redis command to set the key cache:item to expire in 5 minutes.
ASETEX cache:item 5 value
BEXPIRE cache:item 300
CSETEX cache:item 300 value
DEXPIRE cache:item 5m
Attempts:
2 left
💡 Hint
SETEX sets a key with a value and expiry in seconds.
❓ optimization
advanced
2:00remaining
How to efficiently manage memory with many expiring keys?
You have millions of keys with expiry in Redis. Which approach optimizes memory and CPU usage best?
ASet expiry and use Redis active expiration cycle to remove expired keys regularly.
BSet expiry on all keys and rely on Redis lazy expiration only.
CManually delete keys with a background script instead of using expiry.
DNever set expiry and let keys accumulate to avoid CPU overhead.
Attempts:
2 left
💡 Hint
Redis combines lazy and active expiration for efficiency.
🔧 Debug
expert
2:00remaining
Why does this Redis key never expire?
A developer runs these commands:

SET temp:data 100
EXPIRE temp:data 60
TTL temp:data

But the TTL command returns -1. What is the most likely cause?
ARedis does not support expiry on string keys.
BThe key was overwritten by another SET command without expiry after EXPIRE was set.
CThe EXPIRE command syntax is incorrect and was ignored.
DThe TTL command was run on a different key by mistake.
Attempts:
2 left
💡 Hint
Setting a key again removes its expiry unless set with SETEX or EXPIRE after.

Practice

(1/5)
1. What does the EXPIRE command do in Redis?
easy
A. Sets a time limit after which a key will be automatically deleted
B. Immediately deletes a key from the database
C. Shows the remaining time before a key expires
D. Removes the expiration time from a key

Solution

  1. Step 1: Understand the purpose of EXPIRE

    The EXPIRE command sets a countdown timer on a key, after which Redis deletes it automatically.
  2. Step 2: Differentiate from other commands

    Unlike DEL which deletes immediately, EXPIRE schedules deletion after a set time.
  3. Final Answer:

    Sets a time limit after which a key will be automatically deleted -> Option A
  4. Quick Check:

    EXPIRE = set auto-delete timer [OK]
Hint: EXPIRE sets timer; key deletes after time [OK]
Common Mistakes:
  • Confusing EXPIRE with DEL command
  • Thinking EXPIRE shows time left instead of setting it
  • Assuming EXPIRE removes expiration instead of adding it
2. Which of the following is the correct syntax to set a key session to expire in 120 seconds?
easy
A. SETEX session 120
B. EXPIRE 120 session
C. TTL session 120
D. EXPIRE session 120

Solution

  1. Step 1: Recall EXPIRE command syntax

    The correct syntax is EXPIRE key seconds, so EXPIRE session 120 sets expiry to 120 seconds.
  2. Step 2: Check other options

    EXPIRE 120 session reverses arguments, A is incomplete syntax for SETEX, D is invalid usage of TTL.
  3. Final Answer:

    EXPIRE session 120 -> Option D
  4. Quick Check:

    EXPIRE key seconds = EXPIRE session 120 [OK]
Hint: EXPIRE syntax: EXPIRE key seconds [OK]
Common Mistakes:
  • Swapping key and seconds in EXPIRE
  • Using TTL to set expiry instead of check
  • Confusing SETEX syntax
3. Given the commands:
SET user:1 "Alice"
EXPIRE user:1 10
TTL user:1

What will the TTL user:1 command return immediately after setting expiry?
medium
A. -1
B. Error
C. 10
D. 0

Solution

  1. Step 1: Understand TTL after EXPIRE

    After setting EXPIRE to 10 seconds, TTL returns the remaining time in seconds, which is 10 immediately.
  2. Step 2: Interpret other options

    -1 means no expiry, 0 means expired, Error is invalid here.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    TTL after EXPIRE = expiry seconds [OK]
Hint: TTL shows seconds left after EXPIRE set [OK]
Common Mistakes:
  • Expecting TTL to return -1 immediately
  • Thinking TTL returns 0 right after EXPIRE
  • Confusing TTL with DEL command
4. You run the command EXPIRE mykey 30 but TTL mykey returns -2. What is the likely problem?
medium
A. The key mykey has no expiry set
B. The key mykey does not exist
C. The EXPIRE command syntax is incorrect
D. The Redis server is down

Solution

  1. Step 1: Understand TTL return values

    TTL returns -2 if the key does not exist in Redis.
  2. Step 2: Analyze the scenario

    Since TTL returns -2, it means mykey is missing, so EXPIRE had no effect.
  3. Final Answer:

    The key mykey does not exist -> Option B
  4. Quick Check:

    TTL = -2 means key missing [OK]
Hint: TTL -2 means key missing, not syntax error [OK]
Common Mistakes:
  • Assuming -2 means no expiry set
  • Thinking EXPIRE syntax is wrong
  • Ignoring that key might be deleted
5. You want to keep a Redis key cache:data but remove its expiry to prevent automatic deletion. Which command should you use?
hard
A. PERSIST cache:data
B. EXPIRE cache:data 0
C. DEL cache:data
D. TTL cache:data

Solution

  1. Step 1: Identify command to remove expiry

    The PERSIST command removes the expiry time from a key, making it permanent.
  2. Step 2: Check other options

    EXPIRE with 0 deletes immediately, DEL deletes key, TTL only checks expiry time.
  3. Final Answer:

    PERSIST cache:data -> Option A
  4. Quick Check:

    PERSIST removes expiry timer [OK]
Hint: Use PERSIST to cancel expiry, keep key [OK]
Common Mistakes:
  • Using EXPIRE 0 which deletes key immediately
  • Confusing DEL with removing expiry
  • Using TTL expecting it to remove expiry
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