All the basics

What is a cache?

A cache is a small, fast place that keeps the answers asked for most, so you stop visiting the slow place.

Sticky notes on your monitor: the answers you need most sit within arm's reach, so you rarely walk to the filing room.

Press Play to watch it run · 4 steps

A crowd asks the same questions. Without a cache every one reaches the slow database. With a cache most are answered by a fast copy.

A request with and without a cacheA crowd asks the same questions. Without a cache every one reaches the slow database. With a cache most are answered by a fast copy.1 check2 if missingUsersYour appFast copy~1 msSlow database~50 ms

Tap any box to see what it is and what it talks to.

  • Client
  • Service
  • Cache
  • Database
Open in playground
Text description
  1. A crowd asks for the same product page.
  2. Every single request goes to the slow database.
  3. Each answer takes 50 ms, and the line keeps growing.
  4. Users wait, though the answer was the same every time.
  • A cache is a fast copy kept close to the app.
  • Most requests are answered without touching the slow database.
  • It trades freshness for speed: the copy can be old.

Reach for it when

When the same read repeats thousands of times a minute and the answer rarely changes. Not for data nobody asks twice.

The catch

Someone changes a price, but the copy still shows the old one, and shoppers see it until the copy expires.

Say it in an interview“I would cache hot reads in front of the database. It cuts latency and load, and I would use expiry or delete on write to limit stale data.”