stackbook.io
🔍
← Back to Syllabus
Day 6Level 1 — Building Blocks

Caching: Trading Freshness for Speed

Learn: 35 minMap: 25 minDesign: 40 min

Core Concepts (5)

  • 1.A cache keeps a nearby copy of expensive data
  • 2.Caches trade freshness for speed and load reduction
  • 3.TTL and explicit invalidation control staleness
  • 4.Hit ratio determines whether a cache pays off
  • 5.A cache is an optimisation, never the source of truth

Map to Your Experience

Have you ever seen outdated information on a page until you refreshed? What was cached, and who owned the copy?

Design Brief: Cache a Product Page

Learn

A cache stores a copy of data in a place that is faster to reach than the original. Reading from memory is thousands of times quicker than a database query, and a copy held in the browser avoids the network entirely. Caching is the most common way to make systems faster and cheaper.

Caches can sit at many layers. The browser caches files. A content delivery network, or CDN, caches them near users around the world. An application cache such as Redis holds computed results in memory. The database has its own cache of recent pages. The closer the cache is to the user, the faster the answer and the harder it is to control.

The price of speed is staleness. A cached copy may differ from the real data. The simplest control is a time to live, or TTL: each entry expires after, say, 60 seconds and is fetched again. A short TTL means fresher data but fewer hits. A long TTL means more savings but older data. You can also invalidate explicitly, deleting the entry when the source changes, which is fresher but needs more code and can be missed.

The number that tells you whether a cache is worth it is the hit ratio: the share of requests answered from the cache. If 95 percent hit, the database sees only one request in twenty. If only 10 percent hit, you have added complexity for little gain.

Common patterns include cache-aside, where the application checks the cache first, loads from the database on a miss, and stores the result. Writes then either update or delete the cached entry.

Two hazards deserve attention. A stampede happens when a popular entry expires and many requests hit the database at once. And if the system only works when the cache is warm, a cache failure becomes an outage. Treat the cache as an optimisation, never as the source of truth, and test what happens when it disappears.

Map

For a news site, list three things you would cache and the TTL for each: the article text, the comment count, and the homepage headlines. Say what a user would notice if each were five minutes stale.

Design

Design caching for a read-heavy product page that receives 2,000 reads per second and 2 price changes per minute. State the layers used, the cache key, the TTL, and the invalidation approach for price changes. Estimate the hit ratio and the resulting database load, then describe what happens if the cache cluster goes down.

Requirements

  • •Say what is cached and where
  • •Choose a TTL and justify it
  • •Describe invalidation on price change

Constraints

  • •Price changes must show within one minute
  • •Database must survive a cache outage

Must Cover

cache-placementttlinvalidation

Trade-off Axis

freshness vs speed

Referenced Patterns

Competencies Advanced

data state → L1reliability ops → L1

Ready to Design?

Open this day in the Design Studio. The studio follows this track: its days, concepts, patterns and trade-offs are loaded, and you keep one design across the whole track.

Open in Studio →