Quick revision
The short answer
Retry topics take a failed record out of its partition's order, so later records for the same account overtake it, which is fine for independent records and wrong for a ledger. The fix is an ordered retry: when a record for account K fails, mark K as blocked in a durable store and park the record; every later record for K is parked behind it, while other accounts continue. When K's oldest parked record succeeds, its parked records are drained in order and K is unblocked; if it fails permanently, K stays blocked for an operator or a correction event. Short transient errors still use blocking retries, an outage pauses the container, and a per-account sequence check catches anything out of order.
What to remember
In this answer, we saw that retry topics move a failed record out of its partition's order, which let a Withdrawal overtake the Deposit it depended on. Ordered retry keeps order per account: a failed record blocks its key in a durable store and is parked, later records for that key are parked behind it, and other accounts continue. When the oldest parked record succeeds, the key's records drain in order and the key is unblocked; a permanent failure keeps it blocked for an operator. Short transient errors still use blocking retries, a full outage pauses the container, and a per-account sequence check catches anything out of order.
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.
Retry topics take a failed record out of its partition's order, so later records for the same account overtake it, which is fine for independent records and wrong for a ledger. The fix is an ordered retry: when a record for account K fails, mark K as blocked in a durable store and park the record; every later record for K is parked behind it, while other accounts continue. When K's oldest parked record succeeds, its parked records are drained in order and K is unblocked; if it fails permanently, K stays blocked for an operator or a correction event. Short transient errors still use blocking retries, an outage pauses the container, and a per-account sequence check catches anything out of order.
Why this question is asked
The scenario shows the exact failure that non-blocking retries cause: a Deposit fails on a timeout, goes to a retry topic, and 40 seconds later the Withdrawal that depended on it has already been rejected. Blocking retries would have kept the order but held up every account on the partition. Interviewers ask this design question to see whether we can get the best of both: order per key, without one key blocking the others. In this answer, we will see why retry topics break dependent records, design ordered retry with a blocked-key set, walk through draining and permanent failures, decide when blocking retries are still better, and add a sequence check as a safety net.
1Why Retry Topics Broke the Ledger
Kafka keeps a partition's records in order, and Spring Kafka's blocking error handler preserves that order by retrying a failed record in place. Non-blocking retry topics do the opposite: a failed record is published to a retry topic, and the main consumer moves on. The record has left its place in the partition's order.
For independent records, that is the point. For a ledger, it is wrong. Account A-17 had a Deposit of 100 at offset 500 and a Withdrawal of 80 at offset 501. The Deposit timed out and went to the retry topic. The consumer processed the Withdrawal immediately, saw a balance of 20 and rejected it. Forty seconds later the Deposit succeeded, but the customer's withdrawal had already been refused for insufficient funds.
6 messages in order. Play them, or step through them one at a time.
2Ordered Retry With a Blocked-Key Set
The idea is to keep order per key rather than per partition. When the Deposit for A-17 fails, the consumer does two things in one step: it records A-17 as blocked in a durable store, and it sends the record to a parking topic (or a retry topic) keyed by A-17. The durable store can be a small database table or a compacted Kafka topic keyed by account, and its contents are also cached in memory for fast checks.
From then on, for every record the consumer reads, it first checks whether that record's account is blocked. If it is, the record goes to the parking topic behind the earlier one, without being processed. If it is not, it is processed normally. So A-17's Withdrawal joins the queue behind the Deposit, while A-18, A-19 and every other account on the partition carry on at full speed.
3Draining and Unblocking
A separate retry worker reads the parking topic. For each blocked account, it retries the oldest parked record with back-off. When the Deposit for A-17 finally succeeds, the worker applies A-17's remaining parked records in the order they were parked, the Withdrawal now sees a balance of 120 and succeeds, and only after the last parked record is applied does it remove A-17 from the blocked set.
There is a race to handle: a new record for A-17 might arrive at the main consumer while the worker is draining. The rule is that the main consumer keeps parking A-17's records as long as A-17 is blocked, and the worker only unblocks once the parking queue for A-17 is empty, using an atomic check in the store. That keeps a single ordered stream per account at every moment.
If the oldest record fails permanently, for example because the event is invalid, A-17 stays blocked. An operator, or an automated correction event from the producing team, decides what to do: fix and replay the record, or skip it explicitly with an audit note, after which draining continues. Silently skipping it would let later records apply on a wrong balance.
- Is K in the blocked set?Yespark record behind K's earlier onesNoprocess it
- Did processing fail transiently?Yesblock K, park the recordNodone, commit
- Retry worker: oldest parked record for K succeeds?Yesapply the rest in order, then unblock KNokeep K blocked; operator or correction event
3 steps in order. Play them, or step through them one at a time.
4When Blocking Retries Are Still Better
Ordered retry has a cost: a durable store, a parking topic, a worker and careful race handling. For short transient errors, a database deadlock that clears in 200 milliseconds, a blocking retry on the main partition with a short back-off is simpler and costs almost nothing. So we keep a few quick blocking retries first and park only when they are exhausted.
And when the downstream is completely down, ordered retry is the wrong tool. If the ledger database is unavailable, every record fails, and every account gets blocked and parked one after another. That is just a slower, more complicated form of blocking. In that case we pause the listener container, which stops fetching without losing the assignment, and resume when a health check passes.
5A Per-Account Sequence Check as a Safety Net
Finally, the ledger itself should defend its order. If each account event carries a per-account sequence number assigned by the producer, the ledger stores the last sequence applied per account and refuses any event that is not exactly the next one. An event that arrives out of order, because of a bug, a replay or a manual redrive, is detected and parked instead of being applied. Combined with ordered retry, this makes ordering problems visible instead of silent.
6Follow-up questions
How large can the blocked-key set get during a partial outage, and how do you bound it?
In a partial outage, for example one shard of the downstream failing, every account touching that shard gets blocked as its records arrive, so the set can grow to thousands of accounts within minutes. We bound it with a threshold: if the blocked set grows beyond, say, 1 percent of active accounts or a few thousand entries, the problem is no longer about individual records, so the consumer pauses the container and alerts, turning into plain blocking until the downstream recovers. We also alert on the age of the oldest blocked key, so a single stuck account is not forgotten.
Does this survive a consumer restart or rebalance?
Yes, because the blocked set is durable and keyed by account. A new owner of the partition loads the blocked keys for its accounts before processing, so it keeps parking records for accounts that were blocked by the previous owner.
7Conclusion
In this answer, we saw that retry topics move a failed record out of its partition's order, which let a Withdrawal overtake the Deposit it depended on. Ordered retry keeps order per account: a failed record blocks its key in a durable store and is parked, later records for that key are parked behind it, and other accounts continue. When the oldest parked record succeeds, the key's records drain in order and the key is unblocked; a permanent failure keeps it blocked for an operator. Short transient errors still use blocking retries, a full outage pauses the container, and a per-account sequence check catches anything out of order.
Open with
Retry topics take a failed record out of its partition's order, so later records for the same key overtake it. That is fine for independent records and wrong for a ledger, which is exactly what happened: the Deposit went to a retry topic and the Withdrawal was rejected before it came back.
Then, if they want more
I would design ordered retry per key. When a record for account K fails, I mark K as blocked in a durable store, a small table or a compacted topic, and send the record to a retry or parking topic. Every later record for K goes to the same topic behind it, while all other accounts continue normally.
A retry worker retries K's oldest parked record; when it succeeds, it drains K's parked records in order and only then unblocks K, with an atomic check so new records do not slip past. If it fails permanently, K stays blocked until an operator or a correction event resolves it.
I would still use blocking retries with a short back-off for brief transient errors, and when the downstream is completely down I would pause the container, because parking every key during an outage is just slower blocking.
As a safety net, I would add a per-account sequence check in the ledger so an out-of-order record is detected and parked rather than applied.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
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 "Design error handling that keeps per-account order while not blocking other accounts.": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/a3cd9d4a43/