LogoMasst Docs

Write-Through

Understanding the write-through caching pattern.

What is Write-Through?

Write-Through is a caching pattern where writes go to both the cache and the database synchronously. The cache acts as the main interface for both reads and writes.


How It Works

Application                Cache                Database
     │                       │                     │
     │─── Write(key, data) ─►│                     │
     │                       │                     │
     │                       │─── Write ──────────►│
     │                       │                     │
     │                       │◄── OK ─────────────│
     │                       │                     │
     │◄── OK ────────────────│                     │

Both cache and database are updated before returning success.


Read Flow

Application                Cache                Database
     │                       │                     │
     │─── Read(key) ────────►│                     │
     │                       │                     │
     │◄── Data (if cached) ──│                     │
     │                       │                     │
     │        OR if miss:    │                     │
     │                       │─── Query ──────────►│
     │                       │◄── Data ───────────│
     │◄── Data ──────────────│                     │

Advantages

BenefitDescription
Cache always consistentWrites update cache immediately
No stale dataCache reflects DB state
Simple readsAlways read from cache
Data durabilityDB always has latest

Disadvantages

DrawbackDescription
Higher write latencyMust write to both
Cache may have unused dataWrites cached even if never read
Write amplificationEvery write hits DB and cache

Use Cases

Good for:

  • Data that's frequently read after write
  • Strong consistency requirements
  • Applications where write latency is acceptable

Not ideal for:

  • Write-heavy workloads
  • Data rarely read after write
  • Latency-sensitive writes

Comparison

AspectWrite-ThroughCache-Aside
Write latencyHigherLower
Cache consistencyAlways consistentMay be stale briefly
Cache populationOn every writeOn read (lazy)
ComplexityCache handles writesApp manages both

Interview Tips

  • Explain synchronous write to both cache and DB
  • Discuss latency trade-off vs consistency benefit
  • Compare with write-behind (async) pattern
  • Mention when write-through is appropriate