TTL and Expiration in Redis
Learn how Redis handles key expiration: setting TTLs, the EXPIRE and SETEX commands, how lazy and active expiry work, and how the maxmemory eviction policies interact with TTLs.
Why Expire Cache Keys?
Cached data goes stale. Time To Live (TTL) lets Redis automatically remove a key after a set duration, so your cache stays reasonably fresh without manual cleanup.
Setting a TTL with EXPIRE
The EXPIRE command sets a timeout in seconds on an existing key. After that time, Redis deletes the key automatically.
SET user:1 "Alice"
EXPIRE user:1 60
TTL user:1All lessons in this course
- Introduction to Redis Caching
- Redis Data Structures for Cache
- Basic Redis Cache Operations
- TTL and Expiration in Redis