LESSON 05 · Faster reads

Cache repeated work

Keep frequently requested results close to the web tier.

SYSTEM VIEW

The architecture

Diagram view
Reads—
Cache hits—
Database queries—

Watch one product page get answered twice: once the slow way, then from memory.

  1. A visitor asks for a product page. The web server needs that product’s details before it can answer.

    What changed

    • GET /product/42
  2. In the cache-aside pattern, the application looks in the cache before the database. This result isn’t stored yet, so the lookup is a miss.

    What changed

    • Cache lookup: miss
  3. On a miss, the application reads from the database, which still holds the source of truth.

    What changed

    • 1 database query
  4. The application stores the result in the cache with an expiration time, then returns the page.

    What changed

    • product:42 in the cache, with a TTL
  5. Another visitor asks for the same page. The cache returns the stored result, and the database isn’t queried at all.

    What changed

    • Cache hit
    • Database not queried
  6. At 10,000 reads a second, a 95% hit rate leaves the database 500 queries a second. At 90% it sees 1,000. If the cache fails, all 10,000 arrive at once.

    Hit rate

Space plays · ← → step · Home resets

When the cache disappears.

A cache reduces database reads. What happens to that protection when it goes offline? Set the workload, predict the result, then explore.

10,000 /s
95%

Applies while the cache is healthy. An unavailable cache serves no hits.

Cache state

Example database capacity 3,000 reads/s

Application10,000 reads/s
Cache? reads/sUnavailable · no hits
Database? reads/sRequested from database

The application checks the cache and handles database reads on a miss or fallback.

With these settings, where will database demand land?

Make a prediction to reveal the database load.

Model assumptions and source
  • Each request is a read. Each cache miss or fallback causes one database read request. The result shows demand, not completed reads.
  • The hit rate is a steady-state average. An unavailable cache serves zero hits.
  • The 3,000 reads/s limit is chosen for this exercise. It is not a measured database capacity or latency prediction.
  • Writes, retries, request coalescing, queues, cache warmup, and the cost of individual queries are outside this model.

Amazon describes how cache outages can surge traffic to dependencies and why fallback needs safeguards. Read Caching challenges and strategies (opens in a new tab)↗

THE IDEA

How it works

A cache is fast, usually in-memory storage for results you expect to need again. In the common cache-aside pattern, a hit returns a stored result without querying the database. On a miss, the application reads from the database, stores the result for later, and returns it. Frequently read, rarely changed data is a strong candidate. Important data still belongs in persistent storage.

The hit rate, the share of lookups answered by the cache, is what protects the database. With an example load of 10,000 reads per second, a 95% hit rate leaves the database 500 queries per second. At 90% it sees 1,000: five points of hit rate doubled its load. If a cache outage drops the rate to zero, all 10,000 arrive at once. Multiple cache nodes, spare capacity, and a deliberate fallback help prevent a speed optimization from becoming an availability problem. Redis and Memcached are the usual tools.

An expiration time, or time to live (TTL), that is too short causes repeated database work; too long risks stale answers. Updates may need explicit cache invalidation or replacement. When memory fills, an eviction rule such as least recently used, least frequently used, or first in first out chooses what to remove.

Do not cache what must be exactly current, such as a balance checked before a payment, or what is rarely requested twice. Each cache adds a system to run and a new way to show wrong data.

DECISION POINT

A product page is popular, but its price changes throughout the day.

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • A cache speeds repeated reads but may serve stale data.
  • Expiration, invalidation, eviction, and failure handling are separate decisions.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 5
RecallQUESTION 01

On a cache miss, what should the application normally do?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 5 checkpoints answered correctly to complete this lesson.