Expiry and TTL
Much of what you store in Redis is temporary: a cache entry, a login session, a one-time password, a rate-limit counter. Redis can delete keys automatically after a time you choose. That's called expiry, and the remaining lifetime is the key's TTL (time to live).
Setting an expiry
TTL replies:
| Reply | Meaning |
|---|---|
| A positive number | Seconds until the key disappears |
-1 |
The key exists but never expires |
-2 |
The key doesn't exist (or already expired) |
Watching a key expire
Waiting in a lesson is boring, so this simulator has a shortcut: DEBUG SLEEP n moves its clock forward n seconds.
On a real server, the
DEBUGcommand is disabled by default (Redis 7+), and you'd simply wait.
All the ways to expire
| Command | Effect |
|---|---|
SET key value EX 60 |
Set with a 60-second expiry (PX for milliseconds) |
SETEX key 60 value |
Older form of the same |
EXPIRE key 60 |
Add or change the expiry of an existing key |
PEXPIRE key 1500 |
Same, in milliseconds |
EXPIREAT key 1767225600 |
Expire at a Unix timestamp |
PERSIST key |
Remove the expiry: keep forever |
GETEX key EX 60 |
Read and reset the expiry at once |
TTL / PTTL |
Remaining seconds / milliseconds |
EXPIRETIME key |
The absolute Unix time it expires at |
Updating a key removes its expiry
A common bug: SET without EX on an existing key clears its expiry. Use KEEPTTL to keep it:
Commands that modify a value in place, like INCR, APPEND or LPUSH, keep the expiry. Only overwriting the whole key (SET, GETSET, RENAME over it…) resets it.
Conditional expiry
EXPIRE takes options: NX (only if no expiry yet), XX (only if it has one), GT (only if the new expiry is later), LT (only if earlier):
The second EXPIRE … NX returned 0: the window keeps its original deadline instead of being pushed back on every request. You'll use exactly this for rate limiting.
Sliding sessions
A "sliding" session expires after 30 minutes of inactivity. On every request, reset the TTL:
GETEX reads the session and extends it in one command.
How Redis deletes expired keys
Redis doesn't run a timer per key. It uses two strategies:
- Lazy: when you access a key, Redis checks whether it has expired and deletes it if so.
- Active: about 10 times per second, Redis samples a few keys with expiries and deletes the expired ones.
So an expired key may still use memory for a short while, but you'll never read it. The expired_keys statistic counts deletions:
Expiring parts of a hash
Expiry normally applies to a whole key. Since Redis 7.4, individual hash fields can expire too, with HEXPIRE and HTTL. (The simulator doesn't support these.)
Practice
- Store a one-time password
otp:youthat expires in 120 seconds. Check its TTL, move the clock forward 121 seconds, and check again. - Create a key with no expiry, add a 1-hour expiry, then remove it with
PERSIST. - Show that
SETwithoutKEEPTTLclears an expiry.
1.What does TTL key return for a key with no expiry?
2.You SET token abc EX 60, then later SET token xyz. What's the TTL now?
3.How do you extend a session's lifetime on each request?