10 · Caching Strategies & Architecture Patterns

Part 3 · Caching & edge · ⏱️ ~18 min read · Prerequisites: 01 · Client–server networking, 06 · SQL vs NoSQL


🏢 Real-world case studies: how Facebook TAO, Netflix EVCache and Bigtable cache data, in REAL_WORLD_EXAMPLES.md.

🧒 The 30-second version

Fetching data from a database on a hard drive takes about 15 milliseconds. Reading data from a computer's RAM memory takes about 100 nanoseconds—over 100,000× faster.

A cache is a high-speed in-memory storage layer (like Redis or Memcached) sitting between your application and your database. Instead of making the database re-calculate the same expensive SQL query 10,000 times a second, the application checks the cache first. If the answer is already in memory (Cache Hit), it returns it in under 1 millisecond.

Trail Notes exercise

In the load balancing lesson, two surviving servers can handle 1,800 requests per second, while users send 2,000. Trail Notes adds a cache in front of those servers for public guide pages. Assume all incoming requests are eligible for caching, a cache hit never reaches a backend, and the origin can sustain the same rate for cache misses.

Predict. With a 70% hit rate, how many requests per second go to the backends? What happens if the cache stops answering and the site sends every request through?

Reveal the answer

The cache handles 1,400 requests per second, and the backends see 600. If all traffic bypasses the cache, the backends see 2,000 again and the 200-request-per-second shortfall returns. Plan for cold caches and cache failures. For example, limit concurrent work sent to the origin and decide whether an older cached guide is acceptable during an outage.

Next exercise · Move photo processing off the request path.


🍕 Everyday analogy: The Kitchen Counter vs The Basement Pantry

Imagine you are cooking dinner in your kitchen:

In the kitchen analogy In caching architecture
Basement Pantry Persistent Database on Disk (PostgreSQL, MySQL)
Kitchen Counter In-Memory Cache in RAM (Redis, Memcached)
Grabbing salt from counter in 1 second Cache Hit (< 0.5 ms)
Walking to basement when counter is empty Cache Miss (≈ 15 ms disk latency)
Throwing away expired milk from counter Time-To-Live (TTL) Eviction
Clearing counter when counter is full Least Recently Used (LRU) Eviction

🧩 The problem it solves

  1. Database CPU Exhaustion: A standard PostgreSQL instance can handle roughly 1,000 to 3,000 complex queries/second. If a celebrity tweets a link to a product page and 50,000 users click it simultaneously, the database runs out of connection pools and crashes. An in-memory Redis cluster handles 100,000+ reads/second per node with ease.
  2. Slow Sub-Query Latencies: A dashboard showing user profile + order history + account tier requires 3 table joins and takes 40 ms. Caching the assembled JSON response reduces response time to 0.4 ms, eliminating 99% of database load.

🖼️ Picture it

Cache-Aside Pattern Animation

flowchart TD
    subgraph PatternA["1. Cache-Aside (Lazy Loading - Most Common)"]
        App1["📱 App"] <-->|1. Check Cache| Redis1["⚡ Redis"]
        App1 -.->|2. On Miss: Query DB| DB1[("💾 DB")]
        App1 -->|3. Populate Cache with TTL| Redis1
    end

    subgraph PatternB["2. Write-Through (Strong Freshness)"]
        App2["📱 App"] -->|1. Write Data| Redis2["⚡ Cache"]
        Redis2 -->|2. Synchronously write to DB| DB2[("💾 DB")]
    end

    subgraph PatternC["3. Write-Behind / Write-Back (High Write Speed)"]
        App3["📱 App"] -->|"1. Fast Write (< 0.1ms)"| Redis3["⚡ Cache"]
        Redis3 -.->|2. Async Batch Flush in background| DB3[("💾 DB")]
    end

🔢 Walk-through with real numbers

The Impact of Cache Hit Ratio on User Latency

Suppose your database query takes 20 ms and your Redis cache lookup takes 0.5 ms:

Effective Latency = (Hit Ratio × Cache Latency) + ((1 - Hit Ratio) × DB Latency)
Cache Hit Ratio Calculation Average Request Latency Database Load Reduction
0% (No Cache) 1.0 × 20 ms 20.0 ms 0%
80% (0.80 × 0.5) + (0.20 × 20) 4.4 ms 80%
95% (Target) (0.95 × 0.5) + (0.05 × 20) 1.47 ms 95%
99% (Elite) (0.99 × 0.5) + (0.01 × 20) 0.69 ms 99%

💡 Takeaway: Pushing your hit ratio from 80% to 95% cuts average user latency by 66% and reduces database query traffic by a factor of 4!


⚙️ How it works: The 4 Core Caching Access Patterns

1. Cache-Aside (Lazy Loading)


2. Write-Through Pattern


3. Write-Behind (Write-Back) Pattern


4. Write-Around Pattern


5. Redis vs Memcached

Dimension Redis Memcached
Data Structures Rich: Strings, Hashes, Lists, Sets, Sorted Sets (ZSET), Bitmaps, HyperLogLogs Simple key-value strings and raw blobs only
Thread Architecture Single-threaded event loop (epoll / kqueue) with I/O threads Multi-threaded (scales across multiple CPU cores on 1 machine)
Persistence to Disk ✅ Yes: RDB snapshots and AOF (Append-Only File) logs ❌ No: Pure in-memory only (cache wiped on reboot)
Clustering & Replication Built-in Redis Sentinel (failover) and Redis Cluster (sharding) Client-side consistent hashing across nodes
Lua Scripting / PubSub ✅ Yes: Atomic Lua scripts and Pub/Sub message channels ❌ No

⚖️ Caching Patterns Comparison Matrix

Pattern Write Latency Read Latency Data Freshness Risk of Data Loss
Cache-Aside Fast (Writes DB) Ultra-Fast on Hit Eventual (Bounded by TTL) None
Write-Through Slower (Writes both) Ultra-Fast Strong / Immediate None
Write-Behind Blazing Fast (< 0.1ms) Ultra-Fast Eventual High (Power crash)
Write-Around Fast Slower on 1st read Strong None

🚫 Common mistakes & production pitfalls

  1. Updating the Cache instead of Deleting it on Write:

    • The Trap: When updating a user profile in the DB, you also execute cache.set(user_id, new_data).
    • Why it causes bugs (Race Condition): Two concurrent updates arrive: Thread A writes DB, Thread B writes DB. Due to network jitter, Thread B's cache update executes before Thread A's cache update. The cache now holds stale Thread A data indefinitely!
    • The Fix: Always invalidate (delete) the cache key on mutation (cache.delete(user_id)). The next read will cleanly re-populate the cache from the database.
  2. Caching without a Time-To-Live (TTL):

    • Why it hurts: If a cache invalidation bug occurs in code, data without a TTL remains stale in memory forever until a human engineer manually flushes the cluster.
    • The Fix: Always set a TTL (e.g. 5 minutes or 1 hour) as a safety net.
  3. Caching Giant Monolithic JSON Blobs:

    • Why it hurts: Storing a 5 MB JSON document in Redis. Redis is single-threaded; transferring and serializing a 5 MB string blocks the Redis event loop for 10 ms, stalling thousands of other concurrent requests.
    • The Fix: Cache granular records (< 10 KB) or split objects into Redis Hashes (HSET / HGET).

💻 Try it yourself

Run the interactive simulator in this folder:

cd 10-caching-patterns
python demo.py

What to explore in demo.py:

  1. Watch the Cache-Aside pattern: first read triggers a 15 ms disk query, and immediate second read returns in 0.2 ms from RAM!
  2. Observe Write-Through: synchronously updating both tiers for 100% data freshness.
  3. Observe Write-Behind: updates acknowledged instantly in memory, then flushed to disk in an asynchronous batch by a background worker.

🌍 Where you'll see it in production


🎯 Top Interview Questions & Deep Answers

Q1: "When modifying data in the database, should you UPDATE the cached value or DELETE (invalidate) it? Why?"

💡 Click to reveal the ideal senior engineering answer

In almost all production scenarios, you should DELETE (invalidate) the cache key rather than updating it:

  1. Prevents Concurrent Race Conditions:
    • Suppose Thread 1 updates row X=10 in the DB.
    • Thread 2 updates row X=20 in the DB.
    • If both threads try to update the cache directly, Thread 2's cache update might execute first, followed by Thread 1's cache update arriving late due to network latency.
    • Result: Database has 20, but Cache has 10! The cache is permanently corrupted with stale data.
    • By deleting the key on mutation, neither thread leaves stale data in the cache. The next read query will fetch the authoritative state directly from the DB.
  2. Resource Efficiency (Lazy Evaluation):
    • If a record is updated 10 times in 1 minute, but nobody reads it, updating the cache 10 times wastes CPU cycles and serialization overhead. Deleting it costs almost nothing, and the cache is only recomputed when someone actually asks for it.

Q2: "What is the difference between Write-Through and Write-Behind, and when is Write-Behind worth the risk of data loss?"

💡 Click to reveal the ideal senior engineering answer
  • Write-Through:
    • The application writes to the cache, and the cache synchronously writes to the database before returning OK.
    • Trade-off: High write latency, zero data loss, 100% fresh reads.
  • Write-Behind (Write-Back):
    • The application writes only to the cache, which returns OK in < 0.1 ms. A background worker batches queued writes and updates the database asynchronously.
    • When is Write-Behind worth the data loss risk?
      1. Extremely High-Frequency Increment Counters: (e.g. YouTube video view counts, live stream like buttons, TikTok analytics). If 100,000 users click "like" in 5 seconds, sending 100,000 individual UPDATE queries to PostgreSQL will lock the row and crash the DB. Write-Behind batches them in Redis memory and flushes 1 single UPDATE likes = likes + 100000 query to Postgres every 10 seconds.
      2. Telemetry & Sensor Logging: Where losing the last 3 seconds of GPS coordinates during an unexpected crash is completely acceptable to the business.

Q3: "How do you choose between Redis and Memcached in a modern distributed system design?"

💡 Click to reveal the ideal senior engineering answer
  • Choose Memcached when:
    • The data model is strictly simple key-value blobs (e.g. HTML fragments, rendered web templates).
    • You are running on multi-core servers with massive RAM and want multi-threaded memory scaling with zero background disk persistence overhead.
  • Choose Redis (The Industry Standard for 95% of Use Cases) when:
    • You need rich data structures: Sorted Sets (ZSET) for real-time leaderboards, Sets for unique visitor tracking, Hashes for user objects, Lists for queues.
    • You need disk persistence (RDB/AOF) to avoid cold-cache stampedes when a cache node reboots.
    • You need atomic operations via Lua scripting (e.g., distributed rate limiters, distributed locks via Redlock).
    • You need automated failover (Redis Sentinel) and clustering (Redis Cluster).

Q4: "What is the 80/20 Rule (Pareto Principle) in caching, and how does it determine cluster memory sizing?"

💡 Click to reveal the ideal senior engineering answer
  • The Principle: In almost all internet applications, 20% of the keys generate 80% of the read traffic (e.g., top trending tweets, viral products, active users).
  • Memory Sizing Math:
    • You never need enough RAM to store your entire database in cache!
    • If your total database size is 10 Terabytes, sizing your Redis cluster to hold the hottest 20% of data (2 TB of RAM across 32 nodes of 64 GB each) allows you to achieve an 80% to 90% Cache Hit Ratio, reducing database load by up to 90% at a fraction of the hardware cost!

✅ Check yourself

  1. An application uses the Cache-Aside pattern. Key user:10 has a TTL of 60 seconds. A client updates user:10 in the database, but forgets to delete it from Redis. For how long will users see stale data?
View Answer

For at most 60 seconds. After 60 seconds, Redis's TTL expires, the key is evicted, and the next read query experiences a Cache Miss, fetching the updated record from the database and healing the cache. (This is why setting a TTL is mandatory as a safety net!).


  1. Why does Write-Through caching have higher write latency than Cache-Aside?
View Answer

Because Write-Through requires a synchronous write to two distinct systems (first to the cache, then across the network to the database disk) before returning an HTTP response to the user. Cache-Aside writes to the database and issues an instantaneous in-memory delete command.


  1. In Redis, what is the time complexity of an HGET on a Hash compared to querying a relational database?
View Answer

An HGET in Redis is O(1) in memory (< 0.1 ms). In a relational database, even with a B-Tree index, a query is O(\log N) and requires traversing tree pages on disk (1–15 ms).


📚 Go deeper