Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

A service keeps user preferences in a write-behind map that a background writer saves to MySQL. During a 20-minute database outage the pending queue grew to 2 million entries and heap use hit 85 percent; after recovery, some users found older preferences overwriting newer ones.

Explain both problems and how to redesign the write-behind so it survives long outages and keeps the newest value.

The heap grew because pending writes were kept in memory with no bound, so a 20-minute outage turned the queue into a memory leak; it needs a cap with back-pressure, or spilling to a durable queue. Older preferences won because retries or parallel writer threads applied an older queued write after a newer one; coalescing per key, so only the latest value is kept, applying each key's writes in order, and making the database update conditional on a version fix it. During the outage the cache also held the only copy of 20 minutes of changes, so we decide whether this data justifies a durable log such as Kafka, and we alert on queue size and age and test long outages in staging.

Why this question is asked

Write-behind works beautifully while the database is up, which is why its weaknesses often surface for the first time during an outage. This incident shows two of them at once: unbounded memory and lost ordering. Interviewers ask about it because each problem has a precise cause and a precise fix, and because the redesign forces us to decide what the data is worth. In this answer, we will recap how the write-behind map works, explain the memory growth, explain how old values won, redesign the queue and the writes, decide on durability, and set up monitoring and tests.

1A Quick Recap of This Write-Behind Map

The service writes user preferences into an in-memory map that acknowledges at once. Each change is also added to a pending queue, and a background writer takes changes from the queue and saves them to MySQL. When a database write fails, the writer puts the change back to retry later. On a normal day, the queue holds a few hundred entries and drains within seconds.

2Problem 1: The Queue With No Bound

When MySQL went down, the writer could not save anything, so every change stayed in the queue, and failed retries were added back. The service kept accepting preference changes at its normal rate, so the queue grew for 20 minutes to 2 million entries. Each entry held a key, a value and some bookkeeping, a few hundred bytes, so the queue consumed hundreds of megabytes and heap use reached 85 percent. A longer outage would have ended in an OutOfMemoryError, losing the whole queue and the service with it.

The cause is that the queue had no limit. A queue between a fast producer and a stopped consumer grows without end unless something pushes back. The fix is a cap with back-pressure: once the queue holds, say, 200,000 entries, new writes are slowed or rejected with a clear error, so the service degrades instead of crashing. Alternatively, the queue spills to a durable store, such as a Kafka topic or a local disk log, so its size is limited by disk rather than heap.

3Problem 2: Older Values Overwriting Newer Ones

A user changed a preference twice during the outage: first to dark, then to light. Both changes sat in the queue. When MySQL returned, several writer threads started draining the queue in parallel, and retried entries were mixed with new ones. If thread A picked up "light" and thread B picked up "dark", and B's write landed last, MySQL ended up with dark, the older value.

Step throughHow an older preference won
UserWrite-behind queueWriter thread AWriter thread BMySQL
theme = dark (v1)
theme = light (v2)
take v2
take v1 (retry entry)
UPDATE theme = light
UPDATE theme = dark: overwrites v2

6 messages in order. Play them, or step through them one at a time.

Nothing tied the order of database writes to the order of the user's changes.

The fix has two parts. Coalesce per key: the queue keeps only the latest value for each user and preference, so when v2 arrives it replaces v1, and there is nothing old left to write. And apply the writes for one key in order, for example by routing each key to the same writer thread by hashing the key, so two threads never write the same key at the same time.

4Making the Database Refuse Old Writes

Coalescing and per-key ordering fix the queue, but bugs, restarts and multiple service instances can still produce a late, old write. The last line of defence is in the database: each change carries a version, an increasing number or a timestamp from a single clock, and the update only applies if it is newer than what is stored.

SQL
-- each queued change carries the version it was made at
UPDATE user_preference
SET value = 'light', version = 2
WHERE user_id = 42 AND name = 'theme' AND version < 2; -- 1 row updated
-- a late retry of the older change arrives afterwards
UPDATE user_preference
SET value = 'dark', version = 1
WHERE user_id = 42 AND name = 'theme' AND version < 1; -- 0 rows updated: ignored

Notice that the writer must handle zero rows updated as success, not failure, since it means a newer value is already stored. A first write for a key needs an insert, for example INSERT ... ON DUPLICATE KEY UPDATE in MySQL with the same version condition inside the update expressions.

5Deciding on Durability

There is a third problem the incident did not show. For 20 minutes, the in-memory queue held the only copy of every preference change. If the service had been restarted, deployed or had crashed in that time, all 2 million changes would have been lost, and users would have seen their preferences silently revert. So we decide what this data is worth. If losing 20 minutes of preference changes during a rare outage is acceptable, a bounded in-memory queue with back-pressure is enough. If it is not, we put a durable log between the cache and the database: the service writes each change to a Kafka topic, keyed by user so per-key order is kept, and a consumer applies them to MySQL with the version check. The log survives restarts, and its size is limited by disk.

Step throughRedesigned write-behind for preferences
  1. Serviceupdates the in-memory cache; returns at once
  2. Durable log (Kafka, keyed by user)per-key order kept; survives restarts; bounded by disk
  3. Writer (one partition per thread)coalesces per key; batches; retries in order
  4. MySQLUPDATE ... WHERE version < :new; ignores late old writes

4 layers, from the top down. Step through them one at a time.

6Monitoring and Testing

Two numbers describe a write-behind queue: its size and the age of its oldest entry. We alert when either grows beyond normal, for example more than 10,000 entries or older than 60 seconds, because that means the database is down or slow and the cache is accumulating unsaved changes. With Kafka, consumer lag gives the same signals.

Finally, we test the outage we just had. In staging, we stop the database for 30 minutes under realistic write load and check three things: memory stays within its limit and back-pressure behaves as designed, the queue drains in a reasonable time after recovery, and every key ends with its newest value, which we verify by comparing a sample of keys against the last value written.

7Follow-up questions

How long a database outage should this design survive?

It is a business decision expressed in numbers. If the agreed target is, say, two hours of database outage with no lost preference changes, then the queue must hold two hours of changes: at the incident's rate of about 1,700 changes per second, roughly 12 million entries, which rules out the heap and points to a durable log. If the business accepts losing changes in longer outages, the design survives as long as the bounded queue lasts and then applies back-pressure, and we document that limit. Either way we test the chosen duration in staging.

Why not write to the database directly and drop write-behind?

That is a real option if preference updates are not frequent enough to strain MySQL. Write-behind earns its complexity only when it removes significant database load, for example by coalescing many rapid updates. If it does not, writing directly with a cache delete is simpler and safer.

Does coalescing lose information?

It loses intermediate values, not the final one. For preferences that is exactly right, since only the latest value matters. For data where every change matters, such as an audit trail, coalescing is wrong and every change must be written.

8Conclusion

In this answer, we traced the heap growth to a write-behind queue with no bound, fixed by a cap with back-pressure or a durable spill, and the old values to parallel and retried writes for the same key, fixed by coalescing per key, ordered per-key writes and a version check in the database. We also saw that the cache held the only copy of 20 minutes of changes, which makes a durable log such as Kafka worth considering, and we added alerts on queue size and age and a staging test of long outages.

What to say in the interviewAbout 100 seconds aloud

Open with

There are two problems. First, the pending writes were held in memory with no limit. When MySQL went down, nothing drained, the service kept accepting changes, and failed retries were added back, so the queue grew to two million entries and heap hit 85 percent.

Then, if they want more

A longer outage would have ended in an out-of-memory crash. The queue needs a cap with back-pressure, slowing or rejecting writes when it is full, or it should spill to a durable queue.

Second, older preferences overwrote newer ones because after recovery several writer threads drained the queue in parallel, mixed with retries, so an older queued write could land after a newer one. I would coalesce per key, so only the latest value for each preference is kept, and route each key to one writer so its writes are applied in order.

As a last line of defence, the database update is conditional on a version, update where version is less than the new version, so a late older write updates zero rows and is ignored.

There was also a hidden risk: during the outage the cache held the only copy of twenty minutes of changes, and a restart would have lost them, so I would decide whether this data justifies a durable log like Kafka, keyed by user, between the cache and the database.

And I would alert on queue size and age and test a long database outage in staging.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain both problems and how to redesign the write-behind so it survives long outages...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/b8fdc30c9c/