0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · Lesson

Cache Eviction and Expiration

Master techniques for managing cache size, setting expiration times (TTL), and handling cache invalidation.

Cache Eviction and Expiration is a free Redis Caching & Messaging (Pub/Sub, Streams) lesson on CoddyKit — lesson 3 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 Cache Expiration Matters

Caching is great for speed, but cached data can become stale, meaning it no longer reflects the true source of information. Also, caches have limited memory.

This lesson explores how to manage these issues using expiration and eviction techniques in Redis.

Introducing Time To Live (TTL)

Time To Live (TTL) defines how long a cached item remains valid. Once its TTL expires, Redis automatically removes the key.

  • Prevents stale data from being served indefinitely.
  • Frees up memory for newer, more relevant data.

Setting TTL with EXPIRE

The EXPIRE command sets an expiration time, in seconds, on an existing key. After the set time, the key will be automatically deleted.

Try setting a key and then giving it a 10-second expiration:

SET myapp:user:1 "John Doe"
EXPIRE myapp:user:1 10
TTL myapp:user:1

SETEX: Set and Expire Together

Instead of two commands (SET then EXPIRE), you can use SETEX to set a key's value and its expiration time atomically (in one go).

This is generally preferred for new keys as it's more efficient.

SETEX myapp:product:5 20 "LaptopX"
TTL myapp:product:5

Checking TTL & Persistence

You can check a key's remaining TTL with the TTL command. It returns the remaining seconds, or -2 if the key doesn't exist, -1 if it exists but has no expiration.

The PERSIST command removes the expiration from a key, making it permanent again.

SET temp:token "abc123"
EXPIRE temp:token 5
TTL temp:token
PERSIST temp:token
TTL temp:token

Millisecond Precision with PEXPIRE

For more precise control, PEXPIRE and PSETEX work just like their second-based counterparts, but accept expiration times in milliseconds.

This can be useful for very short-lived caches or specific timing requirements.

SET mskey:data "important info"
PEXPIRE mskey:data 5000
PTTL mskey:data

Automatic Eviction Policies

What happens when your Redis cache runs out of memory? Redis can be configured to automatically evict (remove) keys to free up space.

This behavior is controlled by the maxmemory setting and an eviction policy.

Common Eviction Policies

Redis offers several eviction policies configured via maxmemory-policy:

  • noeviction: Returns errors on write operations when memory limit is reached.
  • allkeys-lru: Evicts least recently used (LRU) keys from all keys.
  • volatile-lru: Evicts LRU keys *only* from those with an explicit TTL set.
  • allkeys-random: Evicts random keys from all keys.

Manual Cache Invalidation

Sometimes, data changes unexpectedly, or you need to ensure a cache entry is immediately removed. In such cases, you can manually invalidate a key.

The DEL command removes one or more specified keys from Redis immediately.

SET cache:user:1 "user_data_old"
GET cache:user:1
DEL cache:user:1
GET cache:user:1

Quick Check: Expiration

You have a Redis key named session:user:123. You want it to expire in exactly 30 minutes from now. Which command would achieve this?

Recap: Eviction & Expiration

In this lesson, you mastered key techniques for managing cached data in Redis:

  • Using EXPIRE, SETEX, and PEXPIRE to set time-based expirations.
  • Understanding TTL and PERSIST to manage key lifetimes.
  • Learning about automatic eviction policies to handle memory limits.
  • Implementing manual cache invalidation with DEL for immediate updates.

These skills are crucial for maintaining fresh data and efficient memory usage in your Redis cache!

Frequently asked questions

Is the “Cache Eviction and Expiration” lesson free?

Yes — the full text of “Cache Eviction and Expiration” 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 “Cache Eviction and Expiration”?

Master techniques for managing cache size, setting expiration times (TTL), and handling cache invalidation. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Cache Eviction and Expiration” 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

  1. Why Cache? Introduction to Caching
  2. Implementing Basic Cache Patterns
  3. Cache Eviction and Expiration
  4. Preventing Cache Stampedes and Thundering Herds
← Back to Redis Caching & Messaging (Pub/Sub, Streams)