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

  1. The application writes a key and value to the cache and receives success at once.
  2. 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.
  3. A background writer wakes on a schedule, often every 1 to 5 seconds, or when the queue reaches a size limit.
  4. The writer flushes the dirty entries to the database in one batch, often hundreds of rows in a single statement or transaction.
  5. 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.
  6. 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-throughWrite-behindWrite-around
Write latencyCache plus database, a few msCache only, under 1 msDatabase only, a few ms
Data loss riskNone beyond the databaseDirty entries lost on crashNone beyond the database
Read after writeCache has the new valueCache has the new value; database lagsCache miss or stale until refreshed
Database write loadOne write per requestBatched and coalescedOne write per request
Best forMoney, inventory, settingsCounters, analytics, sessionsBulk 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.
TAGS
System Design Fundamentals
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Grokking system design courses with practical examples and hands-on projects
Compare grokking system design courses that teach through practical examples and hands-on projects. Covers Design Gurus, Educative, ByteByteGo, MIT labs, and project-based alternatives.
What to Expect in the Wiz System Design Interview
Wiz design rounds live in its platform's shape: agentless cloud scanning at billions-of-resources scale, the Security Graph, and multi-tenant systems where wrong means breached.
What to Expect in the Mistral AI System Design Interview
Mistral design rounds center on production RAG and agentic systems with cost/performance tradeoffs graded hard, plus serving open-weight models efficiently. Themes and preparation.
Custom system design for scalable e-commerce platforms
Design a scalable e-commerce platform for system design interviews. Covers product catalog, search, cart, checkout, inventory, payments, and handling flash sale traffic spikes.
What Is Saga Pattern?
Learn what the Saga pattern is in microservices, when to use it, examples, trade-offs, and interview tips. Perfect for system design interview prep.
Threads in Distributed Systems: How Servers Handle Many Requests at Once
A thread is one stream of execution inside a process, and distributed servers use pools of them to serve many clients on one machine.
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$98

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.