Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

RD
Redis
0/10 completed
Beginner
What is Redis?Strings, numbers and keysExpiry and TTL
Intermediate
Lists and hashesSets and sorted setsCaching patterns
Advanced
Rate limiters, locks and transactionsPub/Sub and Streams
Pro
Memory, eviction and persistenceReplication, Sentinel and Cluster
Tracks / Redis / Beginner

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

redis-cli (simulator) · editable

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 DEBUG command is disabled by default (Redis 7+), and you'd simply wait.

redis-cli (simulator) · editable

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
redis-cli (simulator) · editable

Updating a key removes its expiry

A common bug: SET without EX on an existing key clears its expiry. Use KEEPTTL to keep it:

redis-cli (simulator) · editable

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):

redis-cli (simulator) · editable

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:

redis-cli (simulator) · editable

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:

  1. Lazy: when you access a key, Redis checks whether it has expired and deletes it if so.
  2. 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:

redis-cli (simulator) · editable

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

  1. Store a one-time password otp:you that expires in 120 seconds. Check its TTL, move the clock forward 121 seconds, and check again.
  2. Create a key with no expiry, add a 1-hour expiry, then remove it with PERSIST.
  3. Show that SET without KEEPTTL clears an expiry.
redis-cli (simulator) · editable
✎ Check your understanding

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?

← Strings, numbers and keys