Bird
Raised Fist0
Redisquery~10 mins

TTL-based expiry in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - TTL-based expiry
Set Key with TTL
↓
Start Countdown Timer
↓
Key Exists?
No→Key Expired and Deleted
Yes↓
Allow Access to Key
↓
Repeat Check Until TTL Ends
This flow shows how Redis sets a key with a time-to-live (TTL), counts down, and deletes the key when TTL expires.
Execution Sample
Redis
SET user:1 "Alice" EX 5
GET user:1
(wait 6 seconds)
GET user:1
Set a key 'user:1' with value 'Alice' that expires after 5 seconds, then get it before and after expiry.
Execution Table
StepCommandActionKey StateOutput
1SET user:1 "Alice" EX 5Store key with TTL 5 secondsuser:1 = "Alice" (TTL=5s)OK
2GET user:1Check key existence and TTLuser:1 = "Alice" (TTL=4s)"Alice"
3(wait 6 seconds)TTL expires, key deleteduser:1 = <deleted>N/A
4GET user:1Key not found after expiryuser:1 = <deleted>(nil)
💡 After 5 seconds, TTL expires and Redis deletes the key automatically.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
user:1<not set>"Alice" (TTL=5s)"Alice" (TTL=4s)<deleted><deleted>
Key Moments - 2 Insights
Why does GET return "Alice" at step 2 but nil at step 4?
At step 2, the key still exists with TTL counting down (see execution_table row 2). By step 4, TTL expired and Redis deleted the key (row 3), so GET returns nil.
Does Redis delete the key exactly at TTL expiry or later?
Redis deletes the key as soon as TTL expires or shortly after during internal checks (row 3). So the key is not accessible after TTL ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of GET user:1 at step 2?
A"Alice"
Bnil
CError
D"Bob"
💡 Hint
Check execution_table row 2 under Output column.
At which step does the key user:1 get deleted due to TTL expiry?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 where key state changes to .
If the TTL was set to 10 seconds instead of 5, what would be the output at step 4?
AError
Bnil
C"Alice"
D"Bob"
💡 Hint
Refer to variable_tracker and execution_table timing of TTL expiry.
Concept Snapshot
TTL-based expiry in Redis:
- Use SET key value EX seconds to set TTL
- Redis counts down TTL internally
- Key is accessible until TTL expires
- After TTL, key is deleted automatically
- GET returns nil if key expired
Full Transcript
This visual execution shows how Redis handles TTL-based expiry. First, a key 'user:1' is set with a value 'Alice' and a TTL of 5 seconds. Redis stores the key and starts counting down the TTL. When we GET the key before TTL expires, Redis returns 'Alice'. After waiting 6 seconds, the TTL expires and Redis deletes the key automatically. A subsequent GET returns nil because the key no longer exists. This demonstrates how TTL controls automatic key expiry in Redis.

Practice

(1/5)
1. What does the Redis command EXPIRE key 60 do?
easy
A. Renames the key to '60'
B. Deletes the key immediately
C. Sets the key to expire and be deleted after 60 seconds
D. Returns the current TTL of the key

Solution

  1. Step 1: Understand the EXPIRE command

    The EXPIRE command sets a time-to-live (TTL) in seconds for a key in Redis.
  2. Step 2: Apply the command to the example

    Using EXPIRE key 60 means the key will be deleted automatically after 60 seconds.
  3. Final Answer:

    Sets the key to expire and be deleted after 60 seconds -> Option C
  4. Quick Check:

    EXPIRE sets TTL = expire after time [OK]
Hint: EXPIRE sets countdown in seconds for key deletion [OK]
Common Mistakes:
  • Thinking EXPIRE deletes key immediately
  • Confusing EXPIRE with TTL command
  • Assuming EXPIRE renames the key
2. Which of the following is the correct syntax to check the remaining TTL of a key named session?
easy
A. TTL session
B. EXPIRE session
C. GETTTL session
D. PERSIST session

Solution

  1. Step 1: Identify the command to check TTL

    The Redis command to check remaining time-to-live is TTL followed by the key name.
  2. Step 2: Match the syntax

    TTL session correctly checks the remaining TTL of the key 'session'. Other options are either invalid or perform different actions.
  3. Final Answer:

    TTL session -> Option A
  4. Quick Check:

    TTL command returns remaining expiry time [OK]
Hint: Use TTL key to see remaining expiry time in seconds [OK]
Common Mistakes:
  • Using EXPIRE to check TTL instead of setting it
  • Using PERSIST which removes expiry, not checks it
  • Assuming GETTTL is a valid Redis command
3. Consider the following Redis commands executed in order:
SET temp 100
EXPIRE temp 10
TTL temp
What is the expected output of the last command?
medium
A. -1
B. 0
C. Error
D. 10

Solution

  1. Step 1: Set key and expiry

    The key 'temp' is set with value '100' and then given a TTL of 10 seconds.
  2. Step 2: Check TTL immediately

    Immediately after setting expiry, TTL temp returns the remaining time, which should be close to 10 seconds.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    TTL after EXPIRE shows seconds left [OK]
Hint: TTL right after EXPIRE shows the set seconds [OK]
Common Mistakes:
  • Expecting TTL to return -1 (means no expiry)
  • Thinking TTL returns 0 immediately after EXPIRE
  • Assuming TTL command causes error
4. You run the following commands:
SET user123 "data"
EXPIRE user123 30
PERSIST user123
TTL user123
But the TTL command returns -1. What is the issue?
medium
A. Key user123 does not exist
B. PERSIST removed the expiry, so TTL returns -1
C. TTL command syntax is wrong
D. EXPIRE was not set correctly

Solution

  1. Step 1: Understand PERSIST effect

    PERSIST removes the expiry from a key, making it permanent.
  2. Step 2: Check TTL after PERSIST

    After PERSIST, TTL returns -1 indicating no expiry is set on the key.
  3. Final Answer:

    PERSIST removed the expiry, so TTL returns -1 -> Option B
  4. Quick Check:

    PERSIST removes TTL, TTL returns -1 [OK]
Hint: PERSIST removes expiry; TTL then returns -1 [OK]
Common Mistakes:
  • Thinking TTL returns error if expiry removed
  • Assuming EXPIRE failed without checking
  • Confusing TTL -1 with key missing
5. You want to set a key cache_item with a value and ensure it expires exactly after 5 minutes. Which sequence of commands correctly achieves this and verifies the expiry?
hard
A. SET cache_item "value"\nEXPIRE cache_item 300\nTTL cache_item
B. SET cache_item "value"\nTTL cache_item 300\nEXPIRE cache_item
C. EXPIRE cache_item 300\nSET cache_item "value"\nTTL cache_item
D. SET cache_item "value"\nPERSIST cache_item\nTTL cache_item

Solution

  1. Step 1: Set the key with value

    Use SET to create the key 'cache_item' with the desired value.
  2. Step 2: Set expiry to 300 seconds (5 minutes)

    Use EXPIRE cache_item 300 to set TTL to 5 minutes.
  3. Step 3: Verify TTL

    Use TTL cache_item to check remaining time, which should be close to 300 seconds.
  4. Final Answer:

    SET cache_item "value"\nEXPIRE cache_item 300\nTTL cache_item -> Option A
  5. Quick Check:

    SET then EXPIRE then TTL to verify expiry [OK]
Hint: SET key, then EXPIRE seconds, then TTL to check [OK]
Common Mistakes:
  • Using TTL to set expiry (wrong command)
  • Setting EXPIRE before SET (key must exist first)
  • Using PERSIST which removes 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