Quick revision
The short answer
The method wrote the new profile to Redis, then the database write failed during the failover, so Redis held values the database never stored; readers saw them until the one-hour TTL expired, and the next miss loaded the old value from PostgreSQL, so the change disappeared. The write path should commit to the database first and only then touch the cache, preferably by deleting the entry after the transaction commits. When the database outcome is unknown, we delete rather than write, and deleting also avoids races between concurrent writers. A failure-injection test and an alert on cache writes without a matching commit keep it fixed.
What to remember
In this answer, we traced the vanishing changes to a write path that updated Redis before PostgreSQL, so failed database writes left unsaved values in the cache until the TTL expired. The fix is to commit to the database first and delete the cache entry after commit, to delete whenever the outcome is unknown, and to prefer deleting over setting because it is safe against concurrent writers. Failure-injection tests and an alert on cache writes without a commit keep the cache from ever getting ahead of the system of record again.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The method wrote the new profile to Redis, then the database write failed during the failover, so Redis held values the database never stored; readers saw them until the one-hour TTL expired, and the next miss loaded the old value from PostgreSQL, so the change disappeared. The write path should commit to the database first and only then touch the cache, preferably by deleting the entry after the transaction commits. When the database outcome is unknown, we delete rather than write, and deleting also avoids races between concurrent writers. A failure-injection test and an alert on cache writes without a matching commit keep it fixed.
Why this question is asked
Writing to the cache first feels user-friendly: readers see the change immediately. The incident shows the cost, users seeing a change that was never saved and then watching it vanish an hour later. Interviewers like this question because the fix is a principle, the cache must never get ahead of the system of record, and because the obvious fix, swapping the order, still has subtle races. In this answer, we will replay what happened, reorder the write path, handle the unknown outcome, explain why deleting beats setting, and add the tests and alerts that keep it fixed.
1A Quick Recap: The Cache Is a Copy
In a cache-aside design, PostgreSQL is the system of record, the place where the truth lives, and Redis holds copies of some of it to make reads fast. A copy is only useful if it is never newer than, or different from, the original. If the cache can contain values the database does not have, the cache stops being a cache and becomes a second, unreliable database.
2What Happened During the Failover
The method wrote the new profile to Redis, then wrote it to PostgreSQL. For three minutes, the database was failing over, and about 0.3 percent of those writes failed, so the method returned HTTP 500. But the Redis write had already succeeded. From then on, reads hit Redis and showed the new profile, so users believed their change was saved. When the one-hour TTL expired, the next read missed, loaded the profile from PostgreSQL, which still had the old version, and put that back in the cache. The change disappeared.
8 messages in order. Play them, or step through them one at a time.
3Ordering the Write Path Correctly
The fix is to let the database decide first. We commit the transaction, and only after a successful commit do we touch the cache. If the commit fails, the cache is untouched and still matches the database. The cleanest way to touch it is to delete the entry, so the next read loads the committed value.
Notice why we delete after commit rather than inside the transaction. If we deleted before the commit, a reader could reload the old value from the database in the gap between our delete and our commit, and the cache would hold the old value again for an hour. A @TransactionalEventListener with phase AFTER_COMMIT is another clean way to express the same thing. Readers lose the instant update, but the next read, a few milliseconds later, gets the committed value, which is what we want.
4When the Outcome Is Unknown
Some failures do not tell us what happened. A timeout during commit may mean the database never received it, or that it committed and the reply was lost. In that case, writing either the new or the old value to the cache is a guess. So whenever the database outcome is unknown, we delete the cache entry. Deleting is always safe: the worst case is one extra database read, and the next read loads whatever the database really has.
5Why Deleting Beats Setting
Even with the right order, writing the new value into the cache after commit has a race. Two requests update the same profile. Request A commits version 1, request B commits version 2, but B's cache write arrives before A's because A's thread was paused. The cache ends up holding version 1 while the database holds version 2, and it stays wrong until the TTL. If both requests delete instead, the order does not matter: both deletes leave the entry empty, and the next read loads version 2.
| Approach | DB write fails | Concurrent writers | Verdict |
|---|---|---|---|
| Cache first, then DB | cache shows unsaved data | can disagree | never |
| DB, then SET new value | cache unchanged | late SET can win | risky |
| DB, then DELETE after commit | cache unchanged | order irrelevant | preferred |
One small race remains in cache-aside with delete: a reader that loaded the old value just before the commit may write it to the cache just after our delete. It is rare and bounded by the TTL; a shorter TTL, or a second delete a short moment later, reduces it further.
6Testing and Alerting
This bug was invisible until a database failover, so the test must create one. A test that injects a failure into the database step, for example by making the repository throw, should assert that the cache does not contain the new value and that the response is an error. A second test can simulate a commit timeout and assert that the entry was deleted.
In production, an alert on cache writes that have no matching database commit catches regressions. One way is to log or count every cache mutation together with the transaction outcome and alert on any mutation recorded for a rolled-back or failed transaction.
7Follow-up questions
Which users need to be told, and how would you find them?
The users whose update returned HTTP 500 during the failover window. Access logs or traces give the request ids, the user ids and the profile fields they tried to change for every failed update in those three minutes, about 0.3 percent of updates. For each, I would compare the attempted change with what the database holds now; if the database has the old value and the user has not changed it again since, the change was lost. Those users get a message saying their change was not saved and asking them to make it again, or, if the changes are unambiguous and safe, we reapply them with their consent.
Doesn't writing to the database first make reads stale for a moment?
Only for the time between commit and the cache delete, a few milliseconds, and in that window readers see the previous committed value, which is consistent with the database. That is far better than seeing values that may never be committed.
Would write-through to the cache solve this?
Only if the cache layer itself writes to the database first and updates the cache only on success, which is the same principle in a different place. A write-through that updates the cache regardless of the database outcome repeats the bug.
8Conclusion
In this answer, we traced the vanishing changes to a write path that updated Redis before PostgreSQL, so failed database writes left unsaved values in the cache until the TTL expired. The fix is to commit to the database first and delete the cache entry after commit, to delete whenever the outcome is unknown, and to prefer deleting over setting because it is safe against concurrent writers. Failure-injection tests and an alert on cache writes without a commit keep the cache from ever getting ahead of the system of record again.
Open with
The method wrote to Redis first and then to PostgreSQL. During the failover, the database write failed and the service returned 500, but Redis already had the new value.
Then, if they want more
So readers saw the change for an hour, until the TTL expired, and then the next miss loaded the old value from the database, and the change disappeared. The cache got ahead of the system of record.
The fix is to write to the database first and only after the transaction commits touch the cache, and I would delete the entry rather than set it. In Spring that is an afterCommit synchronization or a TransactionalEventListener with AFTER_COMMIT, so if the commit fails the cache is never touched.
When the outcome is unknown, like a timeout that may have committed, I delete the entry, because deleting is always safe: the next read loads the truth.
Deleting also avoids a race between concurrent writers: if two requests commit versions one and two but their cache SETs arrive in the opposite order, the cache keeps the older version, while two deletes give the same result in any order.
Finally I would add a test that injects a database failure after the cache step and asserts the cache is unchanged, and an alert on cache writes without a matching commit. And I would find the affected users from the 500 responses in the access logs during the failover.
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 what happened, and how should the write path be ordered and built so the cache...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/c1a8a08c4a/