1. Courses
  2. System Design
  3. Scaling Building Blocks
  4. Stop 3

40 minIntermediate

Caching

A cache keeps a copy of data that is slow to get somewhere that is fast to read. Done well, it cuts latency and database load by ten times or more. Done badly, it shows users wrong prices. This lesson covers both sides.

By the end you can

  • Name the layers where caches live and what each one is good for
  • Implement cache-aside with ioredis, a TTL with jitter, and delete-on-write
  • Explain a cache stampede and stop one with a Redis lock
  • Set HTTP Cache-Control headers that are safe for public and private data
  • Decide, per feature, how stale the data may be

The everyday picture

In a neighbourhood grocery store, the fastest-selling items sit on the shelf right next to the counter: water, chips, soft drinks, mobile top-up cards. The cashier reaches them in two seconds. Everything else is on the main floor, the storeroom at the back holds more, and the distribution centre (DC) holds the rest. Walking to the storeroom takes five minutes. Ordering from the DC takes a day.

The front shelf is small, so the manager must choose what goes there (the fast movers), when to remove things (expired or slow-selling items), and what to do when the price changes (replace the price tag, or the cashier charges the old price). A software cache has exactly the same three questions: what to keep, when to throw it away, and how to keep it correct.

Where caches live

A can sit at any layer between the user and the database. The closer to the user, the faster it is, and the harder it is to clear.

LayerRough read timeGood forHard part
Browser or app (HTTP cache, local storage)~0 (no network)Images, product lists, settingsYou cannot clear it remotely; only expire it
CDN edge (Files, object storage and CDNs)a few to tens of msStatic files, public API responsesNever put private data here
API process memorywell under 1 msSmall, rarely changing lookups (VAT rates, config)Each copy has its own; clearing one clears only one
Redis~0.5 to 1 ms on the same networkShared cache for all API copiesAnother system to run; invalidation
Postgres buffer cachepart of every queryHot table and index pages, automaticallyNot under your control (sized by shared_buffers, Running Postgres in production)

Two words you will hear all the time. A means the data was in the cache. A means it was not, so you went to the source. The share of requests that hit is the .

Quick check

Which of these caches can you not clear from your side once a copy is stored there?

Loading the rest of the lesson…