Bird
Raised Fist0
Redisquery~10 mins

Key expiry for memory management in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Key expiry for memory management
Set Key with Expiry
↓
Key Stored in Memory
↓
Time Passes
↓
Check Expiry Time
↓
Key Accessible
↩Back to Time Passes
This flow shows how Redis stores a key with an expiry time, checks if the key has expired over time, and deletes it to free memory if expired.
Execution Sample
Redis
SET session:user123 "data" EX 5
GET session:user123
(wait 6 seconds)
GET session:user123
Set a key with 5 seconds expiry, get it before and after expiry to see memory management in action.
Execution Table
StepCommandActionResultMemory State
1SET session:user123 "data" EX 5Store key with 5s expiryOKsession:user123 stored with expiry=5s
2GET session:user123Check key existence and expiry"data"Key exists, not expired
3(wait 6 seconds)Time passes beyond expiryN/AExpiry time reached
4GET session:user123Check key existence and expiry(nil)Key deleted from memory due to expiry
💡 Key expired after 5 seconds, so GET returns nil and key is removed from memory
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
session:user123does not existexists with expiry=5sexists with expiry=5sexpiry reacheddeleted
Key Moments - 2 Insights
Why does the GET command return nil after waiting 6 seconds?
Because the key's expiry time of 5 seconds has passed (see execution_table step 3), Redis automatically deletes the key to free memory.
Does the key get deleted exactly at 5 seconds?
Redis checks expiry lazily on access or via periodic cleanup, so the key is deleted when accessed after expiry or during cleanup (step 4 shows deletion on GET).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value returned by GET?
A"data"
Bnil
CError
D"" (empty string)
💡 Hint
Check the 'Result' column at step 2 in execution_table
At which step does the key get deleted from memory?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'Memory State' column in execution_table for when the key is deleted
If the expiry was set to 10 seconds instead of 5, what would happen at step 4?
AGET returns nil because key is expired
BGET returns "data" because key is not expired
CGET returns error
DKey is deleted immediately at step 1
💡 Hint
Refer to variable_tracker and execution_table timing of expiry
Concept Snapshot
SET key with EX seconds sets expiry time
Key remains accessible until expiry
After expiry, key is deleted automatically
GET returns nil if key expired
Expiry helps free memory by removing unused keys
Full Transcript
This visual execution shows how Redis manages memory by expiring keys. First, a key is set with a 5-second expiry. The key is stored in memory and accessible immediately. After waiting 6 seconds, the expiry time is reached. When the key is accessed again, Redis detects the expiry and deletes the key from memory, returning nil. This automatic deletion frees memory by removing unused keys. Redis expiry is checked lazily on access or during periodic cleanup.

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