TTL Patterns & Expiration

Time-To-Live (TTL) is the heartbeat of Redis caching. When you set a TTL on a key, Redis automatically deletes it when the timer expires. This keeps your cache fresh and memory efficient without manual cleanup.

SET with EXPIRE

Store a product cache entry and set it to expire in 1 hour (3600 seconds):

Redis CLIshell
OK
(integer) 1
(integer) 3600

Breakdown:

  • SET stores the JSON product data.
  • EXPIRE sets the key to expire in 3600 seconds (1 hour).
  • TTL returns the seconds remaining. Returns -2 if the key doesn't exist, -1 if it has no expiration.

Session Management with Sliding Window TTL

In a real application, sessions often need renewal on each access (sliding window). Here's how:

Redis CLIshell
(integer) 2
(nil)
(integer) 1
(integer) 1800

By calling EXPIRE again on each user action, the session timer resets. This is called a sliding window โ€” the session lives as long as the user keeps accessing the app.

Cache-Aside Pattern (Lazy Loading)

The cache-aside pattern is the most popular. The logic is simple:

  1. Check the cache (Redis)
  2. If miss, query the database (PostgreSQL)
  3. Store the result in cache with TTL
  4. Return to client

This puts control in the application's hands. Let's walk through a real ShopStream example:

Demonstrating Cache-Aside in Redis CLI

Redis CLIshell
(nil)
OK
(integer) 1
{"id":50,"name":"Cotton T-Shirt","category":"apparel","price":899,"stock":120}
(integer) 3600

Real-world scenario: ShopStream shows product details for 10 million requests/day. Without cache-aside, each request hits the database. With cache, 95% of requests hit Redis (sub-millisecond response) and only 5% hit PostgreSQL (cache misses or TTL expiry).

Cache Invalidation Strategies

The hardest problem in caching is knowing when to invalidate the cache. Two main approaches:

1. TTL-Based (Automatic) Invalidation

Pros: Simple, no manual work. Cons: Stale data until expiry.

Redis CLIshell
OK
(integer) 1

2. Event-Based (Manual) Invalidation

When product stock changes in the database, invalidate the cache immediately:

Redis CLIshell
OK
OK
OK
(integer) 1
1) "product:100:details"
2) "product:101:details"
3) "product:102:details"
(integer) 3

Using UNLINK for Better Performance

UNLINK is like DEL but non-blocking. If you're deleting large keys or many keys, UNLINK is faster:

Redis CLIshell
(integer) 2

Best Practice: Combine both. Use TTL for automatic freshness (e.g., 1 hour for product details) and event-based invalidation for critical updates (e.g., stock changes, price changes).

Write-Through vs. Write-Behind Patterns

Write-Through Pattern

Flow: Client โ†’ Cache โ†’ Database

Write to cache first, then database. Guarantees cache and DB are always in sync. Slower (must write to both), but safe.

Pythonpython

Write-Behind Pattern

Flow: Client โ†’ Cache โ†’ (async) Database

Write to cache immediately, DB is updated asynchronously. Fast for client, but data loss risk if cache fails before DB flush.

Pythonpython

Use Write-Through for: Financial data, orders, critical updates where consistency is non-negotiable.

Use Write-Behind for: Counters, logs, product views where a small lag is acceptable and speed matters.

Pub/Sub: Real-Time Messaging

Redis Pub/Sub enables real-time messaging between services. One service publishes events, other services subscribe and react instantly.

Pub/Sub Basics

Use case: ShopStream order notifications. When an order is placed, notify warehouse and customer instantly.

Publisher: Broadcast Order Created Event

Redis CLIshell
(integer) 0

Subscriber: Listen for Order Events

In a real system, the warehouse service subscribes to orders:new and processes them:

Redis CLI (Subscriber)shell

Pattern Subscription (PSUBSCRIBE)

Subscribe to multiple channels using patterns:

Redis CLIshell

Important: Pub/Sub is fire-and-forget. If no subscribers are listening when you PUBLISH, the message is lost. For persistent messaging, use Redis Streams (Lesson 31).

Python redis-py Integration

Now let's implement all these patterns in production Python code for ShopStream.

Cache-Aside Function

Pythonpython
Cache miss, querying DB for product 100
Cached product 100 for 3600 seconds
{"id": 100, "name": "Wireless Earbuds", "price": 2499}
Cache hit: {"id": 100, "name": "Wireless Earbuds", "price": 2499}
{"id": 100, "name": "Wireless Earbuds", "price": 2499}

Caching Decorator

Wrap any function with caching using a decorator:

Pythonpython

Rate Limiting with Redis INCR

Limit API requests per user. Increment counter, check if over limit:

Pythonpython
Request 1: Request 1/10 allowed
Request 2: Request 2/10 allowed
...
Request 10: Request 10/10 allowed
Request 11: Rate limit exceeded: 11/10
Request 12: Rate limit exceeded: 12/10

Batch Pipeline for High-Throughput Caching

Cache multiple products at once using pipeline:

Pythonpython
Cached 5 products via pipeline

Challenge

Build a cache invalidation system for ShopStream inventory updates:

  1. Create a function that caches product details with 1-hour TTL
  2. When stock is updated in the database, invalidate the product cache
  3. Implement a batch invalidation for multiple products
  4. Bonus: Use PUBLISH to notify other services of the inventory change
Show hint
Pythonpython

Test Your Knowledge

Question 1 of 5
What are the 3 steps of the cache-aside pattern?
Question 2 of 5
What is the difference between TTL-based and event-based cache invalidation?
Question 3 of 5
How would you implement rate limiting using Redis INCR?
Question 4 of 5
What does Redis Pub/Sub do?
Question 5 of 5
How do you renew a session's TTL when a user accesses your app?
0
/5
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You're mastering caching at scale. Go deeper.

Join the Best AI Program: The AI-Era Data Engineer โ€” 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Data Engineer โ†’