What if your app could forget old data all by itself, perfectly on time?
Why EXPIRE and TTL for time-to-live in Redis? - Purpose & Use Cases
Imagine you have a huge list of temporary coupons written on paper. You need to check each day which coupons are still valid and throw away the expired ones manually.
Manually tracking expiration is slow and easy to forget. You might give out expired coupons or waste time checking each one every day.
Using EXPIRE and TTL commands, Redis automatically removes expired data and tells you how long keys will last, so you don't have to track it yourself.
Check date for each coupon and delete if expired
SET coupon "SAVE10" EX 86400 # expires in 1 day
You can store temporary data confidently, knowing it will disappear exactly when it should without extra work.
An online store uses EXPIRE to keep discount codes valid only for 24 hours, automatically removing old codes so customers never use expired offers.
Manual expiration is slow and error-prone.
EXPIRE and TTL automate data lifetime management.
This keeps your data fresh and your app efficient.
Practice
EXPIRE key 60 do?Solution
Step 1: Understand the EXPIRE command
The EXPIRE command sets a countdown timer on a key in seconds.Step 2: Apply the command to the example
Here,EXPIRE key 60sets the key to expire after 60 seconds.Final Answer:
Sets the key to expire and be deleted after 60 seconds. -> Option CQuick Check:
EXPIRE sets countdown = A [OK]
- Thinking EXPIRE deletes immediately
- Confusing EXPIRE with TTL command
- Assuming EXPIRE renames keys
session in Redis?Solution
Step 1: Identify the command to check TTL
The Redis command to check remaining time isTTL key.Step 2: Match the syntax with the key name
For keysession, the correct command isTTL session.Final Answer:
TTL session -> Option AQuick Check:
TTL command syntax = D [OK]
- Using EXPIRE to check TTL
- Assuming GETTTL is a Redis command
- Using TIMELEFT which does not exist
SET temp "data" EXPIRE temp 10 TTL temp
What will the
TTL temp command return immediately after setting the expire?Solution
Step 1: Set key and expire time
The keytempis set with value "data" and an expire time of 10 seconds.Step 2: Check TTL immediately after EXPIRE
Immediately after setting expire, TTL returns the remaining seconds, which is 10.Final Answer:
10 -> Option DQuick Check:
TTL after EXPIRE = 10 [OK]
- Expecting TTL to return -1 (no expire)
- Thinking TTL returns 0 immediately
- Assuming TTL causes an error if key exists
SET user "Alice" EXPIRE user -5 TTL user
What is the result of
TTL user and why?Solution
Step 1: Understand EXPIRE with negative time
Setting EXPIRE with a negative value deletes the key immediately.Step 2: Check TTL after immediate deletion
TTL returns -2 when the key does not exist.Final Answer:
-2 because key is deleted immediately -> Option AQuick Check:
Negative EXPIRE deletes key, TTL = -2 [OK]
- Thinking negative expire removes expiration but keeps key
- Expecting TTL to return -1 for no expire
- Assuming negative expire causes error
cache with a value and ensure it expires in 30 seconds only if it does not already have an expiration. Which Redis command sequence achieves this safely?Solution
Step 1: Understand the requirement
We want to set the key and only add expiration if none exists (TTL = -1).Step 2: Analyze options
SET cache "value" TTL cache EXPIRE cache 30 if TTL is -1 checks TTL first, then sets EXPIRE only if TTL is -1, ensuring no overwrite of existing expiration.Final Answer:
SET cache "value" TTL cache EXPIRE cache 30 if TTL is -1 -> Option BQuick Check:
Check TTL before EXPIRE to avoid overwriting [OK]
- Using NX option which only affects key existence
- Setting EXPIRE twice unnecessarily
- Not checking TTL before setting EXPIRE
Ready to prove it?
Pass the test and this pattern moves unknown
