Key Expiration and TTL in Redis
Learn how Redis lets you attach time-to-live values to keys so they expire automatically, the commands that control TTL, and how Redis decides when to remove expired keys.
Key Expiration and TTL in Redis is a free Redis Caching & Messaging (Pub/Sub, Streams) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Redis Caching & Messaging (Pub/Sub, Streams) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Keys Need to Expire
Redis often holds temporary data — sessions, one-time codes, cached responses. Expiration lets a key delete itself after a set time so memory stays lean.
Setting a TTL with EXPIRE
EXPIRE attaches a time-to-live in seconds to an existing key. Once the clock runs out, the key vanishes. See the example below.
SET session:42 "active"
EXPIRE session:42 3600Set and Expire in One Step
Set value and TTL together with SET ... EX — safer than two separate commands, since there's no window where the key has no expiry.
SET otp:99 "123456" EX 300Checking Remaining Time
TTL returns the seconds left on a key. It returns -1 if the key has no expiry, and -2 if the key doesn't exist at all.
TTL session:42Millisecond Precision
For finer control, PEXPIRE and PTTL work in milliseconds instead of seconds. The code below sets and reads a millisecond TTL.
PEXPIRE otp:99 1500
PTTL otp:99Expiring at a Specific Time
EXPIREAT takes a Unix timestamp, so a key can die at an exact wall-clock moment — like precisely at midnight.
EXPIREAT daily:report 1893456000Removing an Expiry
PERSIST strips a key's TTL, making it permanent again — handy when temporary data gets promoted to durable.
PERSIST session:42
TTL session:42How Redis Deletes Expired Keys
Redis removes expired keys two ways: lazy deletion checks on access, and active deletion samples keys in a background task and purges expired ones.
Writes Can Clear TTL
Watch out: a plain SET (no EX) overwrites a value and clears its existing TTL. Modifying commands like APPEND or INCR keep the TTL intact.
Common Use Cases
TTL powers real patterns: auto-expiring sessions, rate-limit counters, one-time passwords and verification tokens, and caches that refresh after N seconds.
Keyspace Notifications on Expiry
Redis can publish an event when a key expires via keyspace notifications, so your app can react. Enable expired-key events, then subscribe. See below.
CONFIG SET notify-keyspace-events Ex
SUBSCRIBE __keyevent@0__:expiredQuick Check
Test your expiration knowledge.
Recap
Recap: EXPIRE and SET ... EX set a TTL, TTL reads it (-1 none, -2 missing), PEXPIRE/EXPIREAT give finer control, PERSIST removes it, plain overwrites clear it.
Frequently asked questions
Is the “Key Expiration and TTL in Redis” lesson free?
Yes — the full text of “Key Expiration and TTL in Redis” is free to read here on the web, and the Redis Caching & Messaging (Pub/Sub, Streams) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Redis Caching & Messaging (Pub/Sub, Streams) course, upgrade to CoddyKit PRO.
What will I learn in “Key Expiration and TTL in Redis”?
Learn how Redis lets you attach time-to-live values to keys so they expire automatically, the commands that control TTL, and how Redis decides when to remove expired keys. You practise Redis Caching & Messaging (Pub/Sub, Streams) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Redis Caching & Messaging (Pub/Sub, Streams)?
No prior experience is required. Redis Caching & Messaging (Pub/Sub, Streams) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Key Expiration and TTL in Redis” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Redis Caching & Messaging (Pub/Sub, Streams) lesson?
Yes. Every Redis Caching & Messaging (Pub/Sub, Streams) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Redis
- Redis CLI Basics
- Core Redis Data Structures
- Key Expiration and TTL in Redis