Bird
Raised Fist0
Redisquery~10 mins

Temporary data with TTL in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Temporary data with TTL
Set key with value
↓
Assign TTL (time to live)
↓
Wait for TTL countdown
↓
TTL expires?
No→Key still exists
Yes↓
Key auto-deleted
↓
Key no longer accessible
This flow shows how a key is set with a value and a time limit (TTL). When the TTL ends, the key is automatically removed.
Execution Sample
Redis
SET session123 "user_data"
EXPIRE session123 5
GET session123
(wait 6 seconds)
GET session123
Set a key with a value and TTL 5 seconds, then get it before and after TTL expires.
Execution Table
StepCommandActionKey Exists?Output
1SET session123 "user_data"Store key 'session123' with value 'user_data'YesOK
2EXPIRE session123 5Set TTL 5 seconds on 'session123'Yes1
3GET session123 (immediately)Retrieve value of 'session123'Yes"user_data"
4(Wait 6 seconds)TTL countdown ends, key expiresNonil
5GET session123 (after wait)Try to get expired keyNonil
💡 After 5 seconds, TTL expires and Redis deletes the key automatically.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
session123does not exist"user_data""user_data" (TTL 5s)deleteddoes not exist
Key Moments - 2 Insights
Why does GET return nil after waiting even though we set the key before?
Because the TTL expired after 5 seconds (see execution_table step 4), Redis automatically deleted the key.
What does the EXPIRE command return and why?
It returns 1 to confirm TTL was set successfully (see execution_table step 2). If key did not exist, it would return 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of GET session123 immediately after setting TTL?
A"user_data"
Bnil
CError
D1
💡 Hint
Check execution_table row 3 under Output column.
At which step does the key 'session123' get deleted automatically?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at execution_table step 4 Action and Key Exists? columns.
If we set TTL to 10 seconds instead of 5, what changes in the execution_table?
AEXPIRE returns 0
BGET returns nil immediately
CKey expires at step 4 after 10 seconds instead of 5
DKey never expires
💡 Hint
TTL controls when the key expires, see execution_table step 4.
Concept Snapshot
SET key value - stores data
EXPIRE key seconds - sets TTL
GET key - retrieves data
After TTL ends, key auto-deletes
GET returns nil if key expired
Full Transcript
This visual trace shows how Redis handles temporary data using TTL (time to live). First, a key 'session123' is set with a value 'user_data'. Then, a TTL of 5 seconds is assigned. Immediately after, getting the key returns the stored value. After waiting 6 seconds, the TTL expires and Redis deletes the key automatically. Trying to get the key now returns nil because it no longer exists. This demonstrates how TTL helps manage temporary data that expires automatically.

Practice

(1/5)
1. What does the EX option do when used with the Redis SET command?
easy
A. Deletes the key immediately after setting it
B. Sets a time-to-live (TTL) in seconds for the key, after which it expires
C. Sets the key to be persistent and never expire
D. Renames the key to a new name

Solution

  1. Step 1: Understand the SET command with EX option

    The SET command stores a key-value pair. The EX option specifies the expiration time in seconds.
  2. Step 2: Effect of EX on the key

    When EX is used, the key will automatically be deleted after the given time passes.
  3. Final Answer:

    Sets a time-to-live (TTL) in seconds for the key, after which it expires -> Option B
  4. Quick Check:

    EX = TTL in seconds [OK]
Hint: EX sets expiration time in seconds for keys [OK]
Common Mistakes:
  • Thinking EX makes the key permanent
  • Confusing EX with immediate deletion
  • Assuming EX renames the key
2. Which of the following is the correct syntax to set a key session with value abc123 that expires in 120 seconds in Redis?
easy
A. SET session abc123 PX 120
B. SET session EX 120 abc123
C. SET EX 120 session abc123
D. SET session abc123 EX 120

Solution

  1. Step 1: Recall Redis SET command syntax with EX

    The correct syntax is SET key value EX seconds.
  2. Step 2: Match the syntax with options

    SET session abc123 EX 120 matches the correct order: key, value, EX, seconds.
  3. Final Answer:

    SET session abc123 EX 120 -> Option D
  4. Quick Check:

    SET key value EX seconds [OK]
Hint: Remember: SET key value EX seconds order [OK]
Common Mistakes:
  • Placing EX before the key or value
  • Using PX instead of EX for seconds
  • Mixing order of arguments
3. Given the commands below, what will be the output of TTL user:123 immediately after execution?
SET user:123 "John" EX 60
TTL user:123
medium
A. 60
B. -1
C. -2
D. 0

Solution

  1. Step 1: Understand the SET with EX 60

    The key user:123 is set with a TTL of 60 seconds.
  2. Step 2: Check TTL command output

    The TTL command returns the remaining time to live in seconds. Immediately after setting, it should be 60.
  3. Final Answer:

    60 -> Option A
  4. Quick Check:

    TTL after SET with EX 60 = 60 [OK]
Hint: TTL shows seconds left, right after SET with EX it's full time [OK]
Common Mistakes:
  • Expecting TTL to return -1 (no expiration)
  • Confusing -2 (key does not exist) with TTL
  • Thinking TTL returns 0 immediately
4. You run the following commands:
SET temp_key "data" EX 30
EXPIRE temp_key 60
TTL temp_key
What will TTL temp_key return?
medium
A. 60
B. 30
C. -1
D. -2

Solution

  1. Step 1: SET with EX 30 sets TTL to 30 seconds

    The key temp_key initially has 30 seconds TTL.
  2. Step 2: EXPIRE command updates TTL to 60 seconds

    The EXPIRE temp_key 60 resets the TTL to 60 seconds.
  3. Step 3: TTL command returns updated TTL

    So, TTL temp_key returns 60 seconds remaining.
  4. Final Answer:

    60 -> Option A
  5. Quick Check:

    EXPIRE updates TTL, TTL shows new value [OK]
Hint: EXPIRE resets TTL, TTL shows latest expiration [OK]
Common Mistakes:
  • Assuming TTL stays at original 30 seconds
  • Confusing TTL -1 (no expiration) with updated TTL
  • Thinking EXPIRE does not change TTL
5. You want to store a temporary login token in Redis that expires after 5 minutes. Which sequence of commands correctly sets the token and checks its remaining TTL after 3 minutes?
hard
A. SET login_token abc EX 300 SLEEP 180 EXISTS login_token
B. SET login_token abc PX 300 WAIT 180 EXISTS login_token
C. SET login_token abc EX 300 SLEEP 180 TTL login_token
D. SET login_token abc EX 300 WAIT 180 EXISTS login_token

Solution

  1. Step 1: Use SET with EX 300 for 5 minutes expiration

    The command SET login_token abc EX 300 sets the token with 300 seconds TTL.
  2. Step 2: Wait 180 seconds (3 minutes) and check TTL

    Using SLEEP 180 simulates waiting. Then TTL login_token shows remaining time, confirming token still exists.
  3. Step 3: EXISTS only returns 1 or 0, TTL shows time left

    Checking TTL is better to confirm token is still valid and how long it lasts.
  4. Final Answer:

    SET login_token abc EX 300 SLEEP 180 TTL login_token -> Option C
  5. Quick Check:

    SET with EX + wait + TTL check = correct flow [OK]
Hint: Use SET with EX, wait, then TTL to check remaining time [OK]
Common Mistakes:
  • Using PX instead of EX for seconds
  • Using WAIT instead of SLEEP (WAIT waits for replicas, not sleep)
  • Using EXISTS which only returns existence, not 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