Skip to main content
Intermediate Architecture & Engineering 11 min read Redis 7 / High-Level Design

System Design: Caching Strategies & Cache Stampede Defense

Design low-latency read caches safely without serving stale financial balances or collapsing your database during hot key expiry.

Written by Ravi Shankar Mishra (Founder & Lead Systems Architect, MockAttempt Academy) Reviewed by MockAttempt Engineering Editorial Desk
Last updated: Verification Policy

Learning Objectives

  • Compare Cache-Aside, Read-Through, Write-Through, and Write-Back strategies by latency and consistency trade-offs.
  • Prevent Cache Stampede (Thundering Herd) and Cache Penetration outages.

Prerequisites

  • REST API architecture and SQL relational database familiarity

Matching Caching Strategy to Consistency Needs

Adding Redis in front of PostgreSQL cuts read P99 latency from tens of milliseconds down to sub-millisecond memory reads — unless cache invalidation races or simultaneous expirations overload your database. The dominant industry default for web catalogs is Cache-Aside (Lazy Loading) paired with short jittered TTLs and explicit write invalidation.

PatternRead PathWrite PathBest Fit
Cache-AsideApp checks cache; on miss queries DB & populates cacheApp updates DB, deletes cache keyGeneral read-heavy catalogs (courses, jobs)
Write-ThroughCache sits inlineWrites hit cache + DB synchronouslyPredictable hot reads, moderate writes
Write-BehindFast cached readsAsync batch flush to DB later (risk of loss on crash)View counters, non-critical telemetry

Strengthen your architecture readiness with SQL Query Optimization or join our Java Backend & HLD Career Program.

Practical Programming Examples

Single-Flight Coalescing & Jittered TTL Pseudo-Implementation (Python)

Coalesce concurrent cache misses for the same hot key so only one DB fetch runs instead of N concurrent DB queries.

Python 3.12 Conceptual Pattern
import random

def compute_jittered_ttl(base_seconds: int = 300, max_jitter: int = 45) -> int:
    return base_seconds + random.randint(0, max_jitter)

cache_store: dict[str, str] = {}

def get_course_summary(slug: str, db_fetch) -> str:
    if slug in cache_store:
        return cache_store[slug]
    fresh = db_fetch(slug)  # Acquire per-key lock / single-flight in multi-worker prod
    cache_store[slug] = fresh
    return fresh

print("Sample jittered TTL (s):", 300 <= compute_jittered_ttl() <= 345)
Expected Output
Sample jittered TTL (s): True
Step-by-Step Execution
  1. Adding random jitter spreads expirations smoothly over 45 seconds instead of synchronized spikes.
  2. Cache-Aside deletes stale keys cleanly on write updates before readers warm the next snapshot.

Common Mistakes to Avoid

  • Caching strongly consistent financial wallet balances inside stale TTL caches.
  • Allowing non-existent IDs (`/courses/nonexistent-999`) to bypass caching repeatedly (Cache Penetration — cache negative misses briefly).

Production Best Practices

  • Always pair Redis caching with `allkeys-lru` / `volatile-lfu` maxmemory policies and single-flight guards on hot keys.

Technical Interview Relevance

Classic SDE-1/SDE-2 System Design round deep-dive (bookmyshow, flipkart catalog, URL shortener).

Practice & Self-Check Questions

When modifying a record in Cache-Aside, should you delete the cache key or overwrite it with the new value?

Deleting (invalidating) the key is usually safer than overwriting under concurrent updates, preventing slower older writers from overwriting newer values inside Redis.

Optional Next Step

Have Questions About Learning System Design Structured Cohorts?

Request the JBCP syllabus & weekend batch schedule from an Academic Counselor.

View JBCP Details →
Learn. Practise. Prove. Get Career-Ready.

Ready to Build Your IT Career?

Explore industry-led programmes and develop practical skills through training, assessments and interview preparation.