What Is Write-Behind (Write-Back) Caching and When Should You Use It?
Write-behind caching, also called write-back caching, writes a new value to the cache immediately and to the database later, in the background. The application gets a success response as soon as the cache holds the value. A write therefore takes a fraction of a millisecond instead of the few milliseconds a database round trip costs. The price is a window between the cache write and the database write. If the cache node fails inside that window, the data that was not yet written is lost.
Use it when writes are frequent and a small loss is tolerable, such as view counters, analytics events, and session state. Do not use it for money or inventory.
How write-behind works, step by step
- The application writes a key and value to the cache and receives success at once.
- The cache marks the entry as dirty, meaning newer than the database copy. It does this with a flag on the entry or by appending the key to a write queue.
- A background writer wakes on a schedule, often every 1 to 5 seconds, or when the queue reaches a size limit.
- The writer flushes the dirty entries to the database in one batch, often hundreds of rows in a single statement or transaction.
- Repeated writes to the same key inside one interval are coalesced, meaning only the latest value is written. So 1,000 increments to one counter become one database write.
- Reads are served from the cache, so a reader sees the new value even before the database has it.
Step 5 is where the saving comes from. Write-through, which writes the cache and the database inside the same request, would have sent all 1,000 increments to the database. The write-through and read-through patterns are explained in What is Read-Through vs Write-Through Cache?.
Where write-behind is used
CPU caches. The L1 and L2 caches in a modern processor use write-back as the default policy. A modified cache line is marked dirty and written to main memory only when it is evicted. That keeps the memory bus free for other work.
Caching libraries. Ehcache supports write-behind through a cache writer with a configurable batch size and maximum delay. Hazelcast implements it in its MapStore. Setting write-delay-seconds above zero turns write-through into write-behind, and the cluster flushes all dirty entries at that interval. Redis has no built-in write-behind. The application treats Redis as the fast layer, and a separate worker drains a Redis list or stream of dirty keys into the database.
ORMs. Hibernate calls its session behavior transactional write-behind. Changes to objects are held in memory and flushed as batched SQL when the transaction commits. That is write-behind inside one transaction, and it does not survive a crash before commit.
The durability risk and how to reduce it
The risk is that a crash destroys dirty entries that exist only in memory. Three mitigations reduce it, and production systems usually use at least two.
Replicate the cache. Hazelcast keeps one backup copy of each entry on another node by default, so a single node failure loses no dirty entries. Redis replication does the same. A replica receives the write a few milliseconds after the primary, so a tiny window remains.
Persist the write queue. Append each dirty key to a durable log before confirming the write. A Kafka topic or a Redis Stream with append-only persistence enabled both work. On restart, the writer replays the log from the last flushed position.
Cap the flush delay. A 5-second delay means a crash loses at most 5 seconds of writes. A 500-millisecond delay loses at most half a second. Cap the number of dirty entries too, so that a slow database does not let the queue consume all cache memory.
Write-through, write-behind, and write-around compared
| Write-through | Write-behind | Write-around | |
|---|---|---|---|
| Write latency | Cache plus database, a few ms | Cache only, under 1 ms | Database only, a few ms |
| Data loss risk | None beyond the database | Dirty entries lost on crash | None beyond the database |
| Read after write | Cache has the new value | Cache has the new value; database lags | Cache miss or stale until refreshed |
| Database write load | One write per request | Batched and coalesced | One write per request |
| Best for | Money, inventory, settings | Counters, analytics, sessions | Bulk loads, rarely read data |
Write-around skips the cache on writes and sends them to the database only. It suits data that is written often and read rarely, because filling the cache with it would evict entries that readers want. Its weakness is the read immediately after a write. That read misses the cache, or returns the old value if the key was already cached and has not yet been invalidated.
When to choose write-behind: the decision rule
Write-heavy data with tolerable loss leans write-behind: view counts, analytics events, session data, presence, and rate-limit counters. Money and inventory lean write-through, because a confirmed write must be in the database before the user sees success. Mixed systems use both, one policy per data type.
In the interview, say the loss window out loud. "With a 2-second flush and a replicated cache, a crash loses at most 2 seconds of counter increments". Then add: "I accept that for a view count and reject it for an account balance". That shows you chose the policy for a reason and know its cost. If the interviewer asks how to remove the loss entirely, name the persisted write queue. Note that it moves the latency back toward write-through.
How to Prepare
- Draw the six steps of the mechanism from memory, including where the dirty flag lives and what coalescing removes.
- Practice the decision rule on five data types: balance, cart, view count, session, product catalog. Say which policy each gets and why.
- Know one library setting such as Hazelcast's write-delay-seconds, so you can name a real setting when asked how the delay is set.
- Study the caching chapters in Grokking System Design Fundamentals, which cover write policies, eviction, and cache invalidation with diagrams.
- Place a cache in a full design with Grokking the System Design Interview, where counters and feeds give you write-behind cases to defend.

GET YOUR FREE
Coding Questions Catalog

$98

$197

$72