Course contents

06. Caching Lesson 3 of 8

Read Through

Move cache loading out of the application: see who talks to the database with read-through, how it enables coalescing, and why the cache becomes critical.

13 minIntermediate8 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Read-through: applications ask only the cache; on a miss the cache loads from the database and keeps the result.
  • The loading logic, key format and TTL policy live in one place instead of in every service.
  • Hit ratio and database load are the same as cache aside for the same size, keys and TTL.
  • Central loading makes per-key coalescing easy, which tames hot-key expiries.
  • The cache becomes a required dependency: if it is down, reads fail unless there is a fallback.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

In a read-through cache, the application asks only the cache for data. If the cache has the value, it returns it. If not, the cache itself loads it from the underlying store (calls a loader function or queries a configured data source), keeps it, and returns it. The application never queries the database for that data.

Compare cache aside, where the application checks the cache, queries the database on a miss and writes the result into the cache. The read traffic is the same; what moves is the loading responsibility.

Why it exists

With one service, cache aside is simple. With many services reading the same data, each carries its own copy of the loading code, and copies drift: different key formats (so entries are never shared), different TTLs, different error handling, and every reader needs database credentials and knowledge of the schema. Read-through moves that logic into one place, the cache layer, and makes the database an implementation detail behind it.

How it works

  1. The application calls get(key) on the cache.
  2. Hit: the cache returns the value from memory.
  3. Miss: the cache calls its loader (for example SELECT … WHERE id = ?), stores the result with its TTL and returns it.

Where it lives:

  • In-process libraries with a loader function, for example a "loading cache" whose get takes the key and computes missing values. These are local to each instance.
  • Cache products and managed services configured with a data source or read-through provider. Some managed caches sit in front of a specific database and serve reads through themselves (for example DynamoDB Accelerator in front of DynamoDB). A generic Redis or Memcached server does not load from your database on its own; read-through there means a client library or a service wrapping it.

Same hits, different caller. For the same cache size, keys and TTL, read-through misses exactly when cache aside would. The simulation shows the same hit ratio and number of database queries in both set-ups; only the source of the queries changes.

Coalescing. Because all loads go through one component, it can keep one load in flight per key: concurrent misses for a key wait for the running load instead of each querying the database (often called single-flight). Several loading-cache libraries do this per key out of the box. It helps exactly when misses for the same key overlap, which is the hot-key expiry case; it does nothing for rarely read keys. In the lesson's flash-sale scenario it removes about a quarter of the queries.

When to use it

  • Many services or code paths read the same data and should share one loading policy.
  • You want readers to have no direct dependency on (or credentials for) the database.
  • Hot keys expire under load and you want coalescing in one place.
  • Your platform offers a read-through cache for your data store.

When not to

  • A single service with simple needs: cache aside is less infrastructure.
  • Data whose loading differs per caller (different projections, permissions), which a generic loader cannot express well.
  • When reads must keep working through a cache outage and you cannot afford a highly available cache tier.

Trade-offs

Choice You gain You pay
Read-through instead of cache aside One loading policy, no DB access in readers, easy coalescing Cache is a critical dependency; loader must know every data shape
Coalescing Fewer duplicate loads on hot keys Waiters share the leader's latency and its failure
Application fallback to the database Reads survive cache outages Reintroduces DB access in readers; can overload the DB

Alternatives

  • Cache aside: the application loads; the cache is optional in the read path.
  • Write-through (often combined with read-through): writes also go through the cache, keeping it current.
  • Refresh-ahead: the cache reloads popular keys before they expire, hiding reload latency.
  • Read replicas or materialised views: scale reads at the database layer.

Failure modes

  • Cache outage: readers without a database path fail completely. Run the cache replicated; if you add a fallback, rate-limit it.
  • Loader errors: a failing loader turns every miss into an error; consider serving stale values while the source is down.
  • Hot-key stampede without coalescing: concurrent misses each run the same query.
  • Stale data: read-through does not invalidate anything; pair it with write-through, invalidation or TTLs.
  • Slow loads hold cache capacity: every in-flight miss occupies the cache tier while it waits for the database.

In production

Treat the read-through tier like a database in availability planning: replicas, health checks, capacity for in-flight loads. Enable per-key coalescing for hot data. Set TTLs with jitter, and decide what happens when the loader fails (error, stale value, default). Monitor hit ratio, load latency, load errors and in-flight loads.

Common misconceptions

  • "Read-through has a better hit ratio than cache aside." Same size, keys and TTL give the same hits.
  • "Read-through keeps data fresh." It only changes who loads; freshness needs a write strategy.
  • "Redis is a read-through cache." Only with a library or service that loads on miss.
  • "The database is protected from every outage." If the cache is down, reads fail or fall back to it.

Interview angle

Explain the read path in one sentence, then the trade: centralised loading and coalescing versus the cache becoming critical infrastructure. Mention that it is commonly paired with write-through, and that hit ratio and database load do not change from cache aside. A strong answer adds how the tier is made highly available and what readers do when it is not.

Model assumptions

The simulation is a simplified model, not a benchmark. Users read 10,000 products, Zipf(1) popularity (Zipf 1.5 in the flash-sale steps), Poisson arrivals. The database has 8 connections and a 40 ms mean read (lognormal, cv 0.4). The cache memory is an LRU of 1,000 keys answering in about 1 ms. In the read-through steps the cache is drawn and simulated as two parts: a cache service (a server model with 64 workers, each held while a request is served, including while it waits for the database) and its memory; the service checks memory, loads misses from the database and stores them. Coalescing lets misses for a key wait for a load already in flight on the cache service. The Search API and the slow-motion walk-throughs are scripted packets, not simulated traffic. A failed cache service refuses requests, and the Shop API has no fallback. The hit-ratio tile counts every lookup since the simulation started, including warm-up. Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. Starting point: the app loads the cache

    This is cache aside, from the previous lesson. The Shop API checks the cache; on a miss the API itself queries the database and writes the row into the cache.

    Watch the packets: the API talks to both the cache and the database. The loading logic (key format, query, TTL, what to do on errors) lives in the application.

    Scene: Users call the Shop API, which checks the cache and, on a miss, queries the database itself and fills the cache. The API has connections to both the cache and the database.

    Key idea: In cache aside the application knows about both stores and does the loading.

  2. 2. Every service repeats the loading code

    A second service, the Search API, also needs products. It gets its own copy of the cache-aside code: look up, query on a miss, fill the cache. The animation shows its miss path.

    Copies drift. One team uses the key product:42, another prod-42, so they never share entries. One sets a TTL, the other forgets. Each service needs database credentials and knowledge of the schema. This walk-through is a scripted illustration; the live traffic is the Shop API only.

    Scene: A Search API is added with its own connections to the cache and the database. A scripted packet shows it checking the cache, querying the database on a miss and filling the cache itself. The Shop API keeps serving live traffic.

    Key idea: With many readers, cache-aside spreads loading logic, credentials and key conventions across services.

  3. 3. Let the cache load from the database

    Now the cache sits between the applications and the database. Applications only ask the cache. On a miss the cache queries the database, keeps the row in its memory and returns it. This is read-through.

    Watch the packets: the Shop API only talks to the cache, and every database query comes from the cache service. The loading code exists once, in the cache layer (a loader function in a caching library, or a cache product configured with a data source).

    The hit ratio and the number of database queries are about the same as before: read-through changes who loads, not how often.

    Scene: A read-through cache service now sits between the APIs and the database, with its memory beside it. The Shop API and the Search API only talk to the cache; every database query comes from the cache service. Hit ratio is about the same as before.

    Key idea: Read-through: the application asks only the cache; the cache loads misses from the database itself.

  4. 4. A miss and a hit in slow motion

    Live traffic is paused so you can follow single requests. First a miss: the API asks the cache, the cache checks its memory, finds nothing, queries the database, stores the row in memory and answers the API. Then a hit for the same product: memory has it, and the database is never involved.

    From the API’s point of view both look the same: one call to the cache. Only the time differs. This walk-through is scripted.

    Scene: Live traffic is paused. A scripted request goes from the API to the cache, which checks its memory, misses, queries the database, stores the row and answers. A second request for the same product is a hit: API to cache, cache to memory and back, no database.

    Key idea: For the caller a miss and a hit are the same call; the cache hides the database behind it.

  5. 5. Hot keys expire under load

    A flash sale: traffic returns at 600 requests per second and a handful of products get most of the views (a steeper skew, Zipf 1.5). Entries now get a 0.5 second TTL (short, to make the effect visible).

    Every time a hot product expires, the requests that arrive during its 40 ms reload all miss, and each of those misses runs its own identical database query. The database does one query per miss, duplicates included.

    Scene: Traffic is back at 600 requests per second with a few very popular products, and cache entries expire after half a second. Every miss, including concurrent misses for the same product, sends its own query to the database, which is busy.

    Key idea: When a hot key expires, concurrent misses each reload it: duplicate queries for the same row.

  6. 6. One load per key

    Because all loading happens in one place, the cache can coalesce it: if a load for product 42 is already running, later misses for 42 wait for that result instead of sending another query. Caching libraries with a loader do this per key; it is often called single-flight.

    The database now runs about a quarter fewer queries than there are misses and is clearly less busy. The saving comes from the hottest keys, exactly the ones that hurt when they expire; misses for rarely read products are not concurrent, so nothing is saved there.

    Scene: The cache service now coalesces loads: concurrent misses for the same product share one database query. The database runs fewer queries than there are misses and is less busy.

    Key idea: A read-through cache can collapse concurrent misses for one key into a single database load.

  7. 7. The cache is now in the path

    One second in, the cache service crashes. In cache aside, a cache failure was just a miss: the app went to the database. Here the application has no database connection any more, so every read fails until the cache is back.

    That is the price of read-through: the cache becomes a required dependency, with the same availability needs as the database. Run it as a replicated cluster, or give the application an explicit (and load-limited) fallback path.

    Scene: One second in the cache service goes offline. The Shop API has no connection to the database, so every request fails.

    Key idea: With read-through the cache is on the critical path: if it is down, reads fail.

  8. 8. When to choose read-through

    The cache service is back. Read-through puts loading logic in one place, makes coalescing easy and keeps applications ignorant of the database. In exchange the cache must be reachable for every read, must know how to load each kind of data, and usually needs to be paired with a write strategy (write-through or invalidation) to stay fresh.

    The hit ratio, TTL trade-off and cold-start risk are the same as with cache aside.

    Scene: The read-through cache service is back with coalescing and a half-second TTL. The Shop API reads only through the cache and nothing fails.

    Key idea: Read-through centralises loading and enables coalescing; it makes the cache a critical, highly available dependency.