← System Design
Core Concepts

What is Caching? (and Where Caches Live)

Watch: "Caching in System Design Interviews w/ Meta Staff Engineer" — Hello Interview

Caching means keeping a copy of data somewhere faster or closer than its source, so repeated reads don't pay the full cost. Memory is roughly a thousand times faster than an SSD read and far cheaper than recomputing a result or calling another service, so a cache that answers most reads can cut latency and database load dramatically.

In simple words

You keep the things you use daily (phone, keys, wallet) on the table by the door instead of in the storeroom. Grabbing them is instant. The catch: if something in the storeroom changes, the copy on the table can be out of date.

Cache-aside read pathClientApp serverCache (Redis)Database1. get · 3. set2. on miss
1. Look in the cache. 2. On a miss, read the database. 3. Put the value in the cache for next time.

Why It Exists

Most systems are read-heavy and their access is skewed: a small fraction of items (popular products, a viral post, today's feed) gets most of the reads. Keeping that hot set in memory serves most traffic cheaply.

Where Caches Live

browser cache -> CDN edge -> reverse proxy / gateway cache
   -> application in-process cache (per server, tiny, fastest)
   -> distributed cache (Redis / Memcached, shared by all servers)
   -> database buffer pool (the DB's own page cache)

How It Works: Common Patterns

cache-aside read:
  v = cache.get(k)
  if v is None:
      v = db.read(k)
      cache.set(k, v, ttl=300)
  return v

on update:  db.write(k, new); cache.delete(k)   # delete, don't set

The Hard Parts

Key Pointers

Follow-up Questions

Why delete the cache entry on update instead of writing the new value?
Two concurrent updates can race so the cache ends up holding the older value indefinitely. Deleting makes the next read load the current value from the DB (a small race still exists, which a TTL bounds).
The Redis cluster restarts empty. What happens?
Every read misses and hits the DB at once, which may not survive the load. Mitigate with gradual warm-up, request coalescing, rate limiting to the DB, and replicas so a single node failure doesn't empty the cache.
In-process cache or Redis?
In-process is fastest but per-server (duplicated, inconsistent across servers). Redis is shared and consistent across servers at a network hop's cost. Many systems use a tiny local cache for the hottest keys in front of Redis.

Related