Read-Through Cache Pattern Explained: How It Works, When to Use It & How to Implement It

Read-Through Cache Pattern Explained: How It Works, When to Use It & How to Implement It

Series — 7 Cache Patterns (2 of 8). Previous: cache-aside. This post covers read-through. Next: write-through. Overview hub publishes last.

Introduction

In cache-aside, your application code checks the cache, and on a miss it queries the database and fills the cache. That works well—but loading logic can scatter across services, controllers, and jobs.

Read-through flips the responsibility: the cache layer loads from the database when a key is missing. The application only says “get me this key.” It never writes the miss-fill path by hand. Libraries like Caffeine’s LoadingCache are built for this.

This guide explains read-through with diagrams, a real “app settings” example, when to choose it over cache-aside, and practical implementation sketches.

What is read-through?

Read-through means:

  1. The app asks the cache for a key.
  2. If the value exists → return it (hit).
  3. If it is missing → the cache (via a loader you registered once) reads the database, stores the value, and returns it.

From the app’s point of view, there is only one API: get(key). Miss handling lives in one place—the loader.

Diagram: app only talks to the cache

Hit

Read-through hit: App calls get on Cache and receives the value; Database stays idle
Hit — the app only talks to the cache.

Miss (loader runs inside the cache layer)

Read-through miss: Cache loader reads from Database, stores the value, and returns it to the App
Miss — the loader inside the cache layer fills from the database.

Compare with cache-aside: there the app would call the DB, then call set on the cache. Here the app never sees that branch.

Real-world example: tenant / app settings

Many SaaS apps load settings on almost every request:

  • feature.checkout_v2 enabled?
  • Default currency, support email, branding colors
  • Per-tenant flags stored in a settings table

Dozens of classes need the same values. If each class does its own Cache::remember, you duplicate keys and loaders. Read-through keeps one loader:

  1. Register: “for key settings:{tenantId}, run this DB query.”
  2. Anywhere in the app: $settings = settingsCache.get(tenantId).
  3. First caller pays for the DB read; everyone else hits the cache.
  4. When an admin saves settings, you invalidate that key (or refresh it).

This is ideal when loading code should live in one place and many call sites just need “give me the value.”

How to implement it

Caffeine LoadingCache (classic read-through)

LoadingCache<String, TenantSettings> settingsCache = Caffeine.newBuilder()
    .maximumSize(10_000)
    .expireAfterWrite(Duration.ofMinutes(5))
    .build(tenantId -> settingsRepository.findByTenantId(tenantId));

// App code — no miss handling here
TenantSettings settings = settingsCache.get(tenantId);

On a miss, Caffeine calls the lambda, caches the result, and returns it. That is read-through.

Laravel-style read-through wrapper

Laravel’s Cache facade is cache-aside by default, but you can wrap it so call sites look like read-through:

class SettingsCache
{
    public function get(int $tenantId): TenantSettings
    {
        return Cache::remember(
            "settings:{$tenantId}",
            now()->addMinutes(5),
            fn () => TenantSettings::query()
                ->where('tenant_id', $tenantId)
                ->firstOrFail()
        );
    }

    public function forget(int $tenantId): void
    {
        Cache::forget("settings:{$tenantId}");
    }
}

// Controllers / jobs only talk to SettingsCache
$settings = app(SettingsCache::class)->get($tenantId);

Under the hood this still uses remember (cache-aside mechanics), but architecturally it is read-through: callers never touch the DB or raw cache keys. That is the practical way to get the same benefit in Laravel.

Invalidate after admin updates

public function update(Request $request, int $tenantId)
{
    // ... save settings to DB ...

    app(SettingsCache::class)->forget($tenantId);

    return back()->with('status', 'Settings saved');
}

Read-through vs cache-aside

  Cache-aside Read-through
Who loads on miss? Application code Cache / loader
Call sites Often repeat remember/query logic One loader; many simple gets
Best when Custom load logic per feature Same load path reused everywhere
Typical tools Laravel remember, Spring @Cacheable Caffeine LoadingCache, custom cache service

They are cousins. Read-through is mostly about where the miss logic lives and how clean call sites stay.

When to use read-through

  • You want loading code in one place (settings, permissions, rate-limit configs, reference data)
  • Many modules need the same keys with the same TTL rules
  • You use an in-process cache (Caffeine, Guava) with a built-in loader API
  • You want junior-friendly call sites: “just call get”

When not to use it

  • Load logic is highly custom per request (joins, user-specific shaping, A/B variants)—cache-aside in the use-case may be clearer
  • You need fine-grained “sometimes skip cache” paths scattered in business logic
  • Write path must stay perfectly in sync on every update—pair with write-through or careful eviction
  • Hot-key stampedes at expiry—still add request coalescing (Caffeine often helps here for in-process loads)

Common mistakes

  • Loader that is slow and unbounded — a read-through loader that does heavy reports will block every miss; keep loaders small
  • Caching mutable entities without eviction — same stale-data risk as cache-aside
  • Hiding DB failures — decide how loader errors surface (fail the request vs short negative cache)
  • Giant in-process caches — Caffeine is per JVM/node; multi-server apps still need Redis (or accept per-node warm-up)

How to verify it works

  1. Cold start: first get hits the DB (or repository).
  2. Second get for the same key does not.
  3. Update settings + invalidate: next get loads again.
  4. Confirm no other class queries settings directly for the same data.

Summary

Read-through = the app only talks to the cache; on a miss the cache loads from the database. Use it when you want one loader and many clean call sites—settings, flags, and shared reference data are classic fits. Caffeine LoadingCache is the textbook API; in Laravel, a small cache service around remember gives you the same architecture.

Next: Write-through — every write updates cache and database together.

Topics
Development Laravel Database