Archprimer
PatternIntermediate 6 min read

Write-Back Cache

Writes go to the cache and are acknowledged immediately, then flushed to the database asynchronously — fast writes at the risk of loss on crash.

write-backwrite-behindcachingasynccache

By Ratish Jain · Updated

Diagram with 3 components: App, Cache, Database. Connections: App → Cache (1 write); Cache → App (2 ack fast); Cache → Database (3 flush async).
ServiceCacheDatabase
Cache acks now, flushes to DB later

Definition

In a write-back (write-behind) cache, writes go to the cache and are acknowledged immediately. The cache then flushes the changes to the backing database asynchronously — batched, on a timer, or on eviction.

The caller doesn't wait for the database, so writes are very fast. The tradeoff is a window where committed writes exist only in the cache.

The Problem It Solves

For write-heavy workloads, making every write hit the database synchronously (as write-through does) is a bottleneck. Many updates to the same key also produce redundant DB writes.

Write-back absorbs bursts in the cache, coalesces multiple updates into fewer DB writes, and returns to the caller instantly — trading durability for throughput and latency.

How It Works

  1. 1The application writes to the cache, which stores the value and marks it dirty.
  2. 2The cache acknowledges the write immediately, before touching the DB.
  3. 3Dirty entries accumulate in the cache.
  4. 4A background process flushes dirty entries to the database — batched, timed, or on eviction.
  5. 5Multiple updates to the same key coalesce into a single DB write.
  6. 6After a successful flush, entries are marked clean.

Risk of data loss

Risk of data loss

Between the ack and the flush, the only copy of a write lives in the cache. If the cache node crashes in that window, those writes are lost. Mitigate with cache replication, persistence (e.g. Redis AOF), or shorter flush intervals — accepting the latency cost.

Tradeoffs

Pros
  • Very low write latency (no synchronous DB write).
  • High write throughput; absorbs bursts.
  • Coalesces repeated writes into fewer DB operations.
  • Reduces load on the database.
Cons
  • Data loss risk if cache crashes before flushing.
  • Weaker durability and consistency guarantees.
  • More complex (dirty tracking, flush scheduling, ordering).
  • DB can lag behind the true current state.

Interview Questions

When is write-back the right choice despite the loss risk?

For write-heavy, loss-tolerant workloads like metrics, counters, view counts, or session data, where throughput matters more than never losing a single write. It's a poor fit for financial transactions or anything requiring strict durability.

How do you reduce the data-loss window in write-back?

Replicate the cache across nodes, enable cache persistence (Redis AOF/RDB), shorten the flush interval or batch size, and flush on eviction. Each reduces the window but erodes the latency advantage that motivated write-back.

60-Second Revision
  • →Write to cache, ack immediately, flush to DB async.
  • →Fast, high-throughput writes; coalesces updates.
  • →Data loss risk in the ack-to-flush window.
  • →Mitigate with replication/persistence/shorter flushes.
  • →Best for loss-tolerant, write-heavy data (metrics, counters).

Related

Everything on Archprimer is connected — follow the graph.