Step 1 · The pattern
Write-through: save, then confirm
Each write goes through the cache to the database. Only when the database has it does the user get a confirmation. Safe, but every write waits for the slowest part.
- write
- confirmed
In short
Write-through vs write-back: how it works and how it breaks
When an application writes through a cache, there are two ways to order the work. Write-through updates the database and the cache together and only then tells the caller it succeeded. It is slower, because every write waits for the database, but an acknowledged write is always durable. Write-back, also called write-behind, updates the cache, acknowledges at once, and saves to the database later in batches. Writes feel instant and the database is shielded from bursts. The cost is a window in which the only copy of the data is in the cache's memory. If that node restarts, every write still waiting in the buffer is gone, and the users were already told it worked. Choose by what a lost write costs. View counters and last-seen timestamps suit write-back. Orders, payments and balances need write-through, or a durable log in front of the buffer.
How this comes up in interviews
"Writes to your service are slow because each one waits for the database. Someone proposes acknowledging from the cache and saving later. What do you ask?"
Then they ask
- The cache node restarts with unflushed writes in its buffer. What did users see, and what is in the database?
- Which kinds of data would you put on write-back, and which would you never put there?
- How could you keep the fast acknowledgement and still not lose confirmed writes?
A strong answer covers
- Write-through confirms only after the database has the write: slower, but an acknowledged write is durable.
- Write-back confirms once the cache has it and flushes later in batches: fast, and it shields the database from bursts.
- The risk is the window where the only copy is in memory. A restart loses writes that users were told had succeeded.
- Choose by the cost of a lost write: counters and last-seen timestamps suit write-back, orders, payments and balances do not.
- A durable log in front of the buffer keeps the speed without the loss.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Redis docs: persistence ↗ · AWS Builders' Library: Caching challenges and strategies ↗ · Uber: How Uber serves over 40 million reads per second with an integrated cache ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.