Design patterns and optimization · Intermediate

Read-through caching

Centralize cache-miss loading while making concurrency, negative results and origin failures explicit.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Move the loading responsibility
  2. Coalesce one hundred simultaneous misses
  3. Separate absence from failure
  4. Measure the miss path as well as hits
  5. Ask Logeia about this articleCheck your understanding

Move the loading responsibility

In read-through caching, the application asks a cache abstraction for a value, and that abstraction invokes a loader on a miss. Cache-aside instead leaves miss detection and origin loading in application code. The distinction is responsibility, not the physical location of a particular process. A shared loader reduces duplicated retrieval logic, but it also becomes a common dependency whose timeout, exception and cancellation behavior must be defined for every caller.

Coalesce one hundred simultaneous misses

Imagine one hundred requests arriving just after product 42 expires. A naive read-through wrapper may still issue one hundred origin reads. Sharing an in-flight load for that key can reduce duplicate work while keeping other keys independent. A single global lock around all loading would prevent duplicates but unnecessarily serialize product 43 behind product 42. Per-key coordination also needs cleanup after exceptions so that a failed future does not permanently poison the key.

Separate absence from failure

A confirmed missing product may be negatively cached for a short interval if the application accepts delayed discovery after creation. A database timeout is not evidence that the product does not exist. Caching both as the same null value can extend an outage into a false absence. Permission-dependent values also need a suitable cache identity: sharing one product key across users is unsafe when the representation includes personalized prices or restricted fields.

Measure the miss path as well as hits

Read-through loading does not automatically keep values synchronized with origin writes. Choose TTLs, explicit invalidations or another publication mechanism according to acceptable staleness. A high hit ratio can coexist with very slow misses, so report their latency distributions separately. Observe active loads, origin requests and failure rates rather than only cache size. The pattern centralizes loading policy, while the application still owns its freshness and access-control requirements.

Even a short negative-cache lifetime creates a delay after a previously absent object is created. Either invalidate that absence during creation or accept a documented discovery delay. Centralizing reads does not remove this product decision. Test miss behavior after creation as well as after deletion, and include concurrent creation and loading in the scenarios used to validate the cache abstraction.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 12

Choose a learning path