StackPractices
intermediate By Mathias Paulenko

Implement the Cache-Aside Pattern with Redis

Use the cache-aside pattern to read and write data through Redis, handling cache misses, stale reads, and write-through invalidation

Overview

Of all the caching patterns, cache-aside (a.k.a. lazy loading) is the one you’ll meet most often. The app checks Redis first; on a miss it loads from the database, writes the result back, and returns it. On writes, it updates the database and deletes the cache entry. Below is a working implementation in Python, including thundering herd protection and write-through invalidation.

When to Use This

  • Read-heavy workloads where the same data is accessed repeatedly
  • Data that changes infrequently but is expensive to fetch
  • You want the cache to be optional — the system works without it
  • You’re fine with a bounded staleness window between a write and the next read
  • Skip it for write-heavy data or data you must always read fresh — the misses will cost more than the hits save

Prerequisites

  • Python 3.10+
  • redis package (pip install redis)
  • A database client (SQLAlchemy, Psycopg, or similar)

Solution

1. Install Dependencies

pip install redis

2. Implement Cache-Aside Read

import json
import logging
from redis import Redis

logger = logging.getLogger(__name__)


class CacheAside:
    def __init__(self, redis_client: Redis, default_ttl: int = 300):
        self.redis = redis_client
        self.default_ttl = default_ttl

    def get_or_load(
        self,
        key: str,
        loader: callable,
        ttl: int | None = None,
    ) -> dict | None:
        """Read from cache, or load from source and cache the result.

        Args:
            key: Cache key.
            loader: Function that loads data from the source on cache miss.
            ttl: Override default TTL in seconds.

        Returns:
            Cached or freshly loaded data.
        """
        try:
            cached = self.redis.get(key)
            if cached is not None:
                return json.loads(cached)
        except Exception as e:
            logger.warning("Cache read failed for %s: %s", key, e)

        result = loader()
        if result is None:
            return None

        try:
            self.redis.setex(key, ttl or self.default_ttl, json.dumps(result, default=str))
        except Exception as e:
            logger.warning("Cache write failed for %s: %s", key, e)

        return result

    def invalidate(self, key: str) -> None:
        """Remove a key from the cache."""
        try:
            self.redis.delete(key)
        except Exception as e:
            logger.warning("Cache invalidation failed for %s: %s", key, e)

    def invalidate_pattern(self, pattern: str) -> int:
        """Delete all keys matching a pattern (e.g., 'user:*')."""
        count = 0
        try:
            for key in self.redis.scan_iter(match=pattern, count=100):
                self.redis.delete(key)
                count += 1
        except Exception as e:
            logger.warning("Pattern invalidation failed for %s: %s", pattern, e)
        return count

3. Implement Write-Through Invalidation

from myapp.database import db

cache = CacheAside(redis_client, default_ttl=300)

def get_user(user_id: str) -> dict | None:
    return cache.get_or_load(
        key=f"user:{user_id}",
        loader=lambda: db.users.find_by_id(user_id),
        ttl=120,
    )

def update_user(user_id: str, data: dict) -> dict:
    user = db.users.update(user_id, data)
    cache.invalidate(f"user:{user_id}")
    return user

def delete_user(user_id: str) -> None:
    db.users.delete(user_id)
    cache.invalidate(f"user:{user_id}")
    cache.invalidate_pattern(f"user_posts:{user_id}:*")

4. Thundering Herd Protection

When a cache entry expires, multiple requests may simultaneously trigger the loader, causing a database spike. A lock prevents this:

import time
import uuid

class CacheAsideWithLock(CacheAside):
    def get_or_load(
        self,
        key: str,
        loader: callable,
        ttl: int | None = None,
        lock_timeout: int = 10,
    ) -> dict | None:
        # Try cache first
        try:
            cached = self.redis.get(key)
            if cached is not None:
                return json.loads(cached)
        except Exception:
            pass

        # Acquire lock to prevent thundering herd
        lock_key = f"lock:{key}"
        lock_token = str(uuid.uuid4())

        acquired = self.redis.set(lock_key, lock_token, nx=True, ex=lock_timeout)
        if acquired:
            try:
                result = loader()
                if result is not None:
                    self.redis.setex(key, ttl or self.default_ttl, json.dumps(result, default=str))
                return result
            finally:
                # Release lock only if we still own it
                self._release_lock(lock_key, lock_token)
        else:
            # Wait and retry
            for _ in range(5):
                time.sleep(0.1)
                cached = self.redis.get(key)
                if cached is not None:
                    return json.loads(cached)
            # Lock timed out — load anyway as fallback
            return loader()

    def _release_lock(self, lock_key: str, token: str) -> None:
        """Release lock using Lua script to prevent releasing someone else's lock."""
        script = """
        if redis.call("get", KEYS[1]) == ARGV[1] then
            return redis.call("del", KEYS[1])
        else
            return 0
        end
        """
        self.redis.eval(script, 1, lock_key, token)

5. Cache Warming

Pre-populate the cache before traffic hits:

def warm_user_cache(user_ids: list[str]) -> None:
    users = db.users.find_many({"id": {"in": user_ids}})
    for user in users:
        cache_key = f"user:{user['id']}"
        redis_client.setex(cache_key, 300, json.dumps(user, default=str))
    logger.info("Warmed cache for %d users", len(users))

How It Works

  1. Cache read — get_or_load checks Redis first. On a hit, it returns the cached value without touching the database.
  2. Cache miss — the loader function fetches from the database. The result is written to Redis with a TTL so it auto-expires.
  3. Write-through invalidation — after any database write, invalidate deletes the cache entry so the next read loads fresh data.
  4. Thundering herd lock — on a cache miss, only one request acquires the lock and loads from the database. Others wait and retry the cache read.
  5. Pattern invalidation — invalidate_pattern uses SCAN to find and delete keys matching a glob pattern, useful for invalidating all entries for a user.
flowchart diagram: Request

Race Conditions and Consistency

Cache-aside has a classic race: a writer updates the database and deletes the cache key, but a reader that already missed is still running its loader — and writes the stale result back afterwards. The cache then holds old data until the TTL expires.

Mitigations, in order of effort:

  • Delete after the write, not before: update the database first, then remove the key. The stale window shrinks to the loader’s runtime.
  • Keep TTLs short on hot keys: even if a race slips through, the entry self-corrects on expiry.
  • Version your values: store a counter inside each record and only write back if the loaded version is still the latest.
  • Delayed double delete: for critical data, delete the key again a few hundred milliseconds after the write. Cheap, and it kills most races.

There’s no way to make cache-aside fully consistent without distributed transactions — the pattern accepts a bounded staleness window in exchange for simplicity. If you need tighter guarantees, look at write-through or CDC-based invalidation (Debezium, DynamoDB Streams).

Monitoring Cache Health

Three metrics tell you whether the cache is earning its memory:

  • Hit rate: hits / (hits + misses) per key prefix. Sustained hit rates below ~80% on hot keys usually mean the TTL is too short for the data’s change rate — or you’re caching data nobody re-reads.
  • Evictions: if Redis evicts keys before their TTL (allkeys-lru under memory pressure), hit rate drops and misses look random. Watch evicted_keys and either raise maxmemory or cache less.
  • Loader latency: every miss triggers a load — and a slow loader makes misses expensive right when traffic spikes. Time it and alert on p99, not the mean.

Log a counter on hit/miss inside get_or_load — one line of instrumentation beats guessing.

Key Design

Key naming looks trivial until it isn’t. A few conventions that pay off:

  • Prefix by entity: user:{id}, order:{id}, not bare IDs. Prefixes make invalidate_pattern("user:*") possible and keep Redis inspection readable.
  • Include the tenant in multi-tenant apps: tenant:{tid}:user:{id} keeps tenants’ cached rows from leaking into each other.
  • Version the format, not just the value: when the serialized shape changes, bump the key prefix (user:v2:{id}). Rolling deploys then read the new format immediately instead of deserializing garbage.
  • Keep keys short but human: Redis keys live in memory; u:42 saves bytes but user:42 saves debugging time. With a few million keys the difference is real but rarely the bottleneck — pick readability first.

Don’t embed user input or timestamps in key names. Every key you can’t predict is a key you can’t invalidate.

Variants

Read-Through with Refresh-Ahead

Refresh the cache before it expires:

def get_or_refresh(key: str, loader: callable, ttl: int = 300) -> dict | None:
    cached = redis_client.get(key)
    if cached:
        result = json.loads(cached)
        # Check if entry is near expiry (within 10% of TTL)
        ttl_remaining = redis_client.ttl(key)
        if ttl_remaining < ttl * 0.1:
            # Refresh in background
            threading.Thread(target=lambda: cache.get_or_load(key, loader, ttl), daemon=True).start()
        return result
    return cache.get_or_load(key, loader, ttl)

Write-Behind (Write-Back)

Write to the cache first, then asynchronously persist to the database:

def write_behind_update(user_id: str, data: dict) -> dict:
    cache_key = f"user:{user_id}"
    redis_client.setex(cache_key, 300, json.dumps(data, default=str))
    redis_client.lpush("write_queue:user", json.dumps({"id": user_id, "data": data}))
    return data

# Background worker processes the queue
def process_write_queue():
    while True:
        item = redis_client.brpop("write_queue:user", timeout=10)
        if item:
            payload = json.loads(item[1])
            db.users.update(payload["id"], payload["data"])

Best Practices

  • Set a TTL on every cache entry — prevents stale data from persisting indefinitely
  • Invalidate on writes — update the database first, then delete the cache entry (not the other way around)
  • Handle Redis failures gracefully — the cache is an optimization; the loader should still work without it
  • Use short TTLs for mutable data — 30-120 seconds for user data, longer for reference data

Common Mistakes

  • Updating cache before database — if the database write fails, the cache has stale data
  • Not handling cache misses — if the loader returns None, don’t cache it (or cache it with a short TTL to prevent repeated misses)
  • Using KEYS instead of SCAN — KEYS blocks Redis; SCAN is non-blocking and safe for production
  • Caching too much — cache only hot data; cold data wastes memory and increases invalidation overhead

Troubleshooting

  • Cache and database are out of sync: define a TTL or invalidation policy.
  • Hit rate dropped after a deployment: check cache key generation and serialization changes — a new version may be writing to different keys.
  • Cold cache causes thundering herd: use cache warming, request coalescing, or single-flight patterns for hot keys.
  • Memory usage grows uncontrollably: set max memory policies, eviction thresholds, and key expiration. Audit large values.
  • Stale data served to users: implement cache invalidation on write and cache-bust URLs for static assets.

Further Reading

Frequently Asked Questions

What's the difference between cache-aside and read-through?

In cache-aside, the application manages cache reads and writes itself. With read-through, that plumbing lives inside the cache library instead. Cache-aside gives you more control; read-through is simpler to wire up.

Should I update the cache or invalidate it on writes?

Invalidate — delete the key and let the next read load fresh data. Updating the cache risks inconsistency if the database write fails halfway.

Should I cache negative results?

When the loader returns None because the row genuinely doesn't exist, caching that miss (with a short TTL, e.g. 30-60s) stops repeated lookups for non-existent keys — a common abuse vector. Keep the TTL short so newly created rows appear quickly.

How do I handle cache stampede?

Use the lock pattern shown above, or request coalescing — only one request loads data while the rest wait and retry the cache read.

Should all keys share the same TTL?

No. Add a small random jitter (±10-20%) so large batches of keys don't expire simultaneously and slam the database together. Mutable user data wants 30-120s; reference data can live for hours.

Does the cached value have to be JSON?

No — JSON is just the easiest to debug. For hot paths, MessagePack or Protocol Buffers serialize faster and use less memory. Whatever you pick, keep the format versioned so a schema change doesn't poison every cached entry.