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.
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 3600All lessons in this course
- Introduction to Redis
- Redis CLI Basics
- Core Redis Data Structures
- Key Expiration and TTL in Redis