System Design Academy

Saving…

Lesson 1 of 9 in Module 40 of 54 lessons complete

What Is Caching?

Module 4, lesson 1Concept10 min

Definition

Cache

In plain English
A cache is a small, fast store that keeps copies of data you'll probably need again.
Think of it like
It is like keeping your most-used spices on the counter instead of in the cellar.

Cache hit and miss

In plain English
A cache hit means the data was found in the cache, while a cache miss means it had to be fetched from the slower source.
Think of it like
It is like finding milk in your fridge versus having to go to the shop.

Why it exists

Your database needs 40 ms to answer a query. Thousands of people keep asking for the same vote total.

A nearby memory store can answer much faster. Reusing one answer leaves the database free for other work.

How it works

Hit ratio

In plain English
The hit ratio is the share of requests the cache answers without going to the source.
Think of it like
Like how often the spice you want is already on the counter.

Stale data

In plain English
Stale data is a cached copy that no longer matches the source.
Think of it like
Like an old timetable pinned to a fridge.
Layer, from user outwardWhat it keeps
Browser cacheFiles on your device
Worldwide delivery cachePublic files near users; lesson 4 names this network
Application memoryCopies inside one running application
Shared cache serverCopies available to several application servers
Database memoryRecently used database pages

The app checks the cache, returns hits, and loads misses from the database before saving and returning them.

A miss loads the source and saves a copy.

The app checks the cache, returns hits, and loads misses from the database before saving and returning them.

  • Assume each cache lookup takes 1 ms and each database read takes 40 ms.
  • With 90% hits, average time is 0.9 × 1 + 0.1 × 41 = 5 ms.
  • This excludes other request work and includes the cache lookup on a miss.

Examples

Tv voting

During a live final, vote totals receive one million reads per minute. Viewers accept totals up to one second old.

One shared total, refreshed once per second, needs about 60 database reads per minute.

  • This assumes one coordinated refresh for that total.
  • Separate totals or independent caches need additional reads.

The insight

A tiny freshness allowance removes huge load.

Trade-offs

Pros

  • Faster repeated reads.
  • Fewer database queries.
  • More read capacity.

Cons

  • Copies can become stale.
  • Another store needs care.
  • An empty cache sends reads to the database.

Use when

  • Data is read far more often than it changes.
  • Users accept a defined freshness delay.

Avoid when

  • A transfer needs the authoritative balance.
  • Reads rarely repeat.

Quick check

A cache answers 90 of every 100 requests. What is its hit ratio?
Which data is the worst fit for caching?

Takeaways

  • A cache keeps a fast nearby copy.
  • Hit ratio measures the share of requests answered by the cache.
  • Choose acceptable staleness before choosing a cache.

Try the quick check above whenever you feel ready.

Previous: Object / Blob Storage