Interview's MateSystem Design
Interview's Mate

System Design

Architecture & trade-offs

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
System Design topics
Lesson 11/ 31
Storage, Caching & Search

Caching Strategies

Cut read latency from 20 ms to <1 ms while avoiding stale data and cache stampedes.

IntermediateCache-Aside · Eviction · Stampede3 quiz questions0/2 subtopics done
Practice in AI HLD
In plain wordsDiagramTheorySubtopics (2)Step by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice
Deep-dive subtopics (2) - 0/2 completed
Cache-Aside, Write-Through & Write-Back

Compare the three core read/write caching strategies and when to pick each.

Beginner
Cache Stampede, Penetration & Avalanche

Stop thundering herds, fake-key floods, and synchronized TTL expirations from crashing your DB.

Intermediate

In plain words

A cache is a small, very fast memory that keeps answers you need often, so you don’t have to fetch them from the slow database every time.

Think of it like…

Keeping your daily tools on your desk instead of walking down to the basement storage room every time you need a pen.

Where you’ve seen it

A news site caches its homepage in Redis. Millions of visitors get it in about 1 ms, and the database is queried only when the cache expires.

Words you’ll hear

Cache hit / miss
A hit means the answer was in the cache; a miss means you had to fetch it.
TTL
How long an item stays in the cache before it expires.
Eviction (LRU)
Removing the least recently used items when the cache is full.
Cache-aside
The app checks the cache first and, on a miss, loads from the DB and fills the cache.

Visual diagram

Loading interactive diagram
Loading interactive exercise

Theory & deep dive

Why caching is the single highest-ROI scaling tool

Reading from Redis takes ~0.5 ms vs. ~5–20 ms for a database query - and RAM easily handles 100,000+ ops/sec per node. Offloading 90% of reads to a cache instantly multiplies your database’s lifespan by 10×.

The two hard problems of caching are staleness (cached value is out of date) and failure modes (what happens when a million keys expire at once).

Key takeaway: Caches make reads ~10–50× faster, as long as you pair them with clear TTLs and invalidation rules.

Cache Patterns: Aside, Write-Through, Write-Back

Cache-Aside lets the app read/populate the cache lazily - simple, resilient if Redis restarts, and only caches data people actually ask for.

Write-Through updates the cache and DB together on writes (always fresh, slightly slower writes). Write-Back writes only to cache and flushes asynchronously to the DB later (blazing-fast writes, but risks losing dirty data if cache crashes).

How it works, step by step

Step 1 of 4
Application & Cache

1. Choose read/write pattern (Cache-Aside default)

On read: check cache → on miss, read DB, populate cache with TTL, return. On write: update DB, then delete (invalidate) the cache key.

Delete on write rather than updating to reduce race conditions
Key numbers at this step
Read Hit
<1 ms
Cache Miss
DB RTT + fill

When to use it & trade-offs

What it is

Caching stores frequently accessed data in fast memory (Redis, Memcached, or local RAM) to avoid repeating expensive database queries or computations.

Why it matters: Most applications are heavily read-skewed (80/20 rule); caching drops P99 latency dramatically and protects the database from read overload.

When to reach for it

  • Read-heavy profiles, product catalogs, permissions, and rendered feeds
  • Distributed counters, leaderboards (Redis Sorted Sets), and session stores
  • Coalescing repeated expensive computations or external API calls

When to avoid or be careful

  • Write-back caching for financial balances or irreversible orders without durable WAL
  • Setting identical TTLs on 10M keys seeded at midnight (causes synchronized expiry avalanche)

Caching Read/Write Strategies

Cache-Aside (Lazy Loading)
Best for: General-purpose read-heavy apps (default choice)

Strengths: Only requested keys occupy RAM; cache outage degrades gracefully to DB.

Watch out: Cold-miss penalty; brief stale window between DB write and cache delete.

Write-Through
Best for: Read-heavy data needing strong freshness in cache

Strengths: Cache is always warm and in sync with the DB on every write.

Watch out: Higher write latency (2 hops) and unused writes pollute cache RAM.

Write-Back (Write-Behind)
Best for: High-frequency counters, likes, analytics buffering

Strengths: Ultra-fast writes (batched asynchronously to DB); coalesces repeated updates.

Watch out: Risk of data loss if the cache node crashes before flushing to disk/DB.

Interview signals & numbers to know

When this topic comes up

  • 80/20 reads
  • sub-millisecond reads
  • hot key
  • thundering herd
  • Redis

Benchmarks & rules of thumb

Redis latency (VPC) -
0.3–1 ms
Single Redis throughput -
~100k ops/sec
Target cache hit ratio -
85–99%
TTL jitter range -
±10–20%

Common mistakes & follow-ups

Common interview mistakes

  • Updating cache before DB or updating both concurrently (out-of-order writes leave stale cache forever)
  • Letting a hot key expire without single-flight coalescing, crashing the DB under a stampede
  • Ignoring memory fragmentation and oversized values (>1 MB keys slow Redis single-threaded loop)

Likely interviewer follow-ups

  • Why is deleting a cache key on write usually safer than updating it?
  • Explain the difference between cache stampede, cache penetration, and cache avalanche.
  • How do you keep an L1 in-process cache synced across 50 app servers?
Interview tip: Always mention why you invalidate (delete) on DB write and add TTL jitter + single-flight locking for hot keys.

Knowledge check

Loading quiz

Put it into practice

Design full architectures with AI feedback

Apply Caching Strategies inside real High-Level Design prompts or review Q&A flashcards.

Open AI HLD Practice →SD Interview Q&A
Previous topic
Replication & Partitioning (Sharding)
Next topic
Blob & Object Storage