Reading preferences
Concept

Caching and Freshness

Reuse a nearby copy while making its freshness limits explicit.

system design, caching, freshness

Keep a useful copy

A library desk may keep a recent printed catalogue so it can answer common questions quickly. A computer cache similarly keeps a copy near the reader. The authoritative source remains elsewhere. A copy can be faster, but it may be out of date.

Library catalogue: a cache-aside example

  1. A reader asks for a title; the application checks its cache.
  2. On a miss it reads the catalogue source, returns the result, and stores a copy.
  3. A later request hits that copy. If the source changes, the copy can be stale until invalidated or expired.

See the reasoning

Follow the example one step at a time

Follow a catalogue request through a cache miss, a hit, and a source update. This is a prepared teaching demonstration, not live code execution.

Miss

request → cache: absent
        → source: title A

The application reads the source and stores a copy.

Product details: agree on freshness

A shop might cache descriptions for a few minutes, with a time-to-live (TTL), while checking price or stock against an appropriately current source at checkout. TTL is a retention rule, not a universal freshness guarantee: delayed updates and races can still matter. Invalidate or version entries when appropriate, and state what stale information users may see.

Trade-offs and failure

Caches consume memory and make invalidation and permissions more complicated. If the cache fails or many entries expire together, the source may receive a sudden surge. Limit concurrent refills or provide a safe fallback. A cache is not a durable backup and should not be used where stale data violates the need.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What is a cache hit?
2. What happens on a cache-aside miss?
3. What does a time-to-live limit?
4. What can happen when a popular cache entry expires?

References and further reading