Write-Behind Cache
establishedSummary
Acknowledge writes to cache immediately and flush to the database asynchronously, so write latency seen by the caller is cache write latency rather than database write latency.
Problem
Write latency to the database is too high for the application's response time requirement, and the written data does not need strict durability guarantees on every write.
Description
Also called write-back caching. The application writes to the cache, receives an immediate acknowledgment, and returns to the caller. A background process (flush worker, cache eviction handler, or TTL-triggered flush) later drains the cache writes to the underlying database.
Contrast with the two other write strategies: - Write-through: writes go to cache and database synchronously before
acknowledging. Latency = database write latency. Consistent but slow.
- Cache-aside: application manages cache explicitly; reads and writes both
go to database directly, with cache populated on cache miss. Simplest model.
- Write-behind: writes go to cache only, acknowledged immediately, flushed
asynchronously. Fastest write latency; highest durability risk.
Risk profile: data written to cache but not yet flushed to the database is in-flight. If the cache process crashes, reboots, or is evicted before the flush, that data is permanently lost. This is acceptable for: - Metrics and counters (approximate totals survive; exact counts do not) - Session data (loss means re-authentication) - User activity tracking (best-effort analytics) - Real-time leaderboards (minor score corrections are tolerable)
It is NOT acceptable for: - Financial transactions (charge, payment, transfer) - Order state transitions - Any operation that must be durable and auditable - Systems with regulatory durability requirements
Implementation variants: 1. Queue-backed flush: cache write also appends to a durable write queue
(Redis Streams, SQS). A worker drains the queue to the database. The
queue provides durability that the cache alone does not: unflushed writes
survive a cache restart as long as the queue is durable.
2. TTL-flush: cache key is set with a TTL; on expiry or eviction, a callback
writes the value to the database. Simpler but less reliable: cache eviction
under memory pressure can trigger premature or missed flushes.
Redis persistence options (RDB/AOF) reduce but do not eliminate durability risk for unflushed writes, because AOF fsync intervals introduce a window of data loss on crash.
Tradeoffs
Client sees cache write latency (microseconds) instead of database write latency (milliseconds)
Unflushed writes are lost on cache failure; not suitable for data that must survive crashes
Reads from database (by other services) miss writes that have not yet been flushed
Smooths write spikes by absorbing them into cache; database sees metered writes
Requires flush worker, queue monitoring, and flush lag alerting
When to use
Write latency is a bottleneck and the database write path is slow
Write-behind absorbs database write latency into background processing; call returns at cache speed
Data loss of unflushed writes is acceptable for the use case
Some data types (metrics, session state, counters) can tolerate loss of the last seconds of writes
Write throughput exceeds what the database can sustain synchronously
Write-behind batches and smooths writes to the database, absorbing spikes
When not to use
Data must be durable on every write (financial, transactional, regulatory)
Cache crash before flush means permanent data loss; unacceptable for durable workloads
Read-after-write consistency is required across different service instances
Another service reading from the database will not see writes that are still in the cache
Operational Requirements
Alert on flush queue depth growth: indicates flush worker is falling behind
Growing unflushed window means increasing data at risk; must detect before cache memory exhaustion
Use a durable write queue (Redis Streams with AOF, SQS) for queue-backed flush variant
Cache-only flush has no durability on cache crash; queue-backed flush survives cache restart
Document which data types use write-behind and what the acceptable loss window is
Engineers must know which writes are not immediately durable to avoid building dependent features on durability assumptions
Characteristics
Technologies
Canonical
Alternatives
Relationships
Evolves from
Complements
Basis
Well-understood pattern with clear durability tradeoffs; operational risk profile is specific and well-documented
Related Architecture Knowledge
Inbound: affects this entity
Read-through handles cache population on miss; write-behind handles cache population on write. Together they form a complete transparent cache layer.
Full relationship →