Quick revision
The short answer
First I stop the damage: switch off or guard the code path that creates shipments and, if needed, pause the downstream consumers so the duplicates stop spreading. Then I measure the impact from data, coordinate the correction with every team that consumed the events, and find the root cause, which in cases like this is usually a check-then-act across instances with no database constraint behind it. Afterwards the fixes go into the system and into the organisation: uniqueness constraints and idempotency keys, alerts on metrics that reveal duplicates, a blameless review that is shared widely, and a design review checklist item for the pattern.
What to remember
In this answer, we walked through leading a duplicate-shipment incident: contain first with a flag, a guard or paused consumers, then quantify the affected orders from data and coordinate the correction with each consuming team, then find the root cause, a check-then-act across instances with no database constraint, and the detection gap that let it run for hours. Afterwards we added a unique constraint, idempotency keys in consumers, an outbox and duplicate alerts, shared a blameless review widely, and added the pattern to the design review checklist.
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.
First I stop the damage: switch off or guard the code path that creates shipments and, if needed, pause the downstream consumers so the duplicates stop spreading. Then I measure the impact from data, coordinate the correction with every team that consumed the events, and find the root cause, which in cases like this is usually a check-then-act across instances with no database constraint behind it. Afterwards the fixes go into the system and into the organisation: uniqueness constraints and idempotency keys, alerts on metrics that reveal duplicates, a blameless review that is shared widely, and a design review checklist item for the pattern.
Why this question is asked
Incidents like this test leadership more than code. The bug may be a three-line race, but its effects have spread across several teams and real customers for hours, and the person leading the response has to make decisions with incomplete information while keeping many people coordinated. Interviewers ask it to see whether we put containment before diagnosis, whether we work from data instead of guesses, and whether we turn one incident into lasting change rather than a single patch. In this answer, we will walk through the response in the order a senior engineer runs it: containment, impact and correction, root cause, the technical changes, and finally the organisational learning.
1The First Hour: Stop the Damage Before Explaining It
When I take the incident, my first question is not why it happens but how to make it stop happening. Every minute of diagnosis while duplicates keep flowing adds more shipments that warehouses will pick, pack and send. So I look for the fastest reversible lever. If the shipment creation path has a feature flag, I turn it off and let orders queue. If not, a quick guard can work, such as refusing to create a shipment when one already exists for the order, deployed as a hotfix. If the duplicate events have already reached other services, I ask those teams to pause their consumers, which in Kafka means stopping the consumer group so events wait safely in the topic instead of being acted on.
I also set up the incident structure straight away: one person as incident lead (me), one person handling communication to stakeholders and customer support, and a shared channel with a running timeline. Writing down each action with its time pays off later, both for the correction and for the review.
- Containflag off, guard, or pause consumers
- Are duplicates still being created?YesWiden containmentNoMove on to impact
- Quantify impact from datawhich orders, which events, which teams
- Coordinate correctioncancel, recall, refund, compensate
- Find the root causewhile the system is stable
- Fix, then review and shareconstraints, alerts, checklist
6 steps in order. Play them, or step through them one at a time.
2Measuring the Impact and Coordinating the Correction
Once the bleeding stops, I need a precise list, not an estimate. The data usually makes this possible: a query for orders with more than one shipment created in the incident window, joined with the events we published for those shipments. For example, we might find 1,240 orders with two shipments and 38 with three, between 09:12 and 14:47. That list becomes the shared source of truth for everyone.
Then each consuming team checks what they did with the duplicate events. The warehouse service may have created pick tasks, billing may have charged shipping twice, notifications may have sent two tracking emails. I ask each team to report against the same order list, and we agree on the correction per case: cancel shipments that have not left, recall or absorb the cost of those that have, refund double charges, and send customers a clear message. A senior lead does not fix everything personally but makes sure every affected system has an owner and a deadline for its correction.
3Finding the Root Cause
With the system stable, we can investigate calmly. In a case like this the logs often show the same event processed by two instances a few milliseconds apart, perhaps after a consumer rebalance redelivered messages. The code typically looks like this: it checks for an existing shipment and creates one if none exists.
This is a check-then-act race across instances. Both instances check, both find nothing, both insert. The deeper root cause is that the rule 'one shipment per order' existed only in the application code and nowhere in the database. I also ask why the problem lasted hours before detection, because that is a second failure: nothing was watching for duplicates. A good review always has at least two findings, how it happened and why we did not notice sooner.
4The Technical Changes Afterwards
The first change is making the rule impossible to break: a unique constraint on order_id in the shipment table, and the code changed to treat a unique violation as 'already done'. Every consumer of the shipment events gets an idempotency check too, keyed by the event id or by the shipment id, so that a duplicate event is harmless even if a future bug lets one through. The publish step moves to a transactional outbox so that an event is only published for a shipment that really committed.
The second change is detection. We add metrics that would have shown the problem within minutes: shipments created per order (anything above one is a bug), the ratio of shipment events to paid orders, and the number of unique violations caught. Alerts on those turn a five-hour silent incident into a five-minute page.
| Change | What it prevents | Owner |
|---|---|---|
Unique constraint on shipment.order_id | Two shipments for one order | Order team |
| Idempotency keys in every consumer | Duplicate events acted on twice | Each consuming team |
| Transactional outbox for events | Events for rows that never committed | Order team |
| Alert on shipments per order > 1 | Hours of silent duplicates | Order team, on-call |
| Design review checklist item | Same pattern in new services | Architecture group |
5Turning the Incident Into Organisational Learning
The review is blameless: we describe what the system allowed, not who wrote the code. The engineer who wrote the check-then-act did what most of us would have done, and the useful question is why our design process and our platform made it easy. I write the review with a timeline, the impact in numbers, the root cause and the detection gap, and the actions with owners and dates, and I share it beyond the team, for example in an engineering-wide review meeting, because other services almost certainly have the same pattern.
Finally, the lesson goes into the places where future designs are checked. The design review template gets a question: for every side effect triggered by an event, what stops it from happening twice, and is that rule enforced in the database? Some teams add a static check or a library helper for idempotent consumers. That is how one incident improves many services instead of one.
6Follow-up questions
Would you roll back the latest deployment as the first step?
If the incident started right after a deployment, rolling back is often the fastest containment, and I would do it without waiting for proof. Here the race may have existed for months and only been triggered by a rebalance or a traffic spike, so a rollback might not help. That is why I look for a lever that stops the effect, such as a flag or pausing consumers, rather than assuming the latest change is the cause.
How do you keep the other teams aligned during the correction?
I give everyone the same data: one list of affected orders, kept in a shared place and updated as we learn more, and one channel where each team reports its status against that list. Each team owns its own correction because it knows its system, but I track the open items and the deadlines, and the communication lead keeps customer support informed so customers hear one consistent story.
What makes a post-incident review effective rather than a formality?
Actions with owners and dates that are actually tracked to completion, a clear separation between the trigger, the root cause and the detection gap, and a blameless tone so people share the real details. It also has to reach people outside the team; a review that only the authors read prevents only that one bug.
7Conclusion
In this answer, we walked through leading a duplicate-shipment incident: contain first with a flag, a guard or paused consumers, then quantify the affected orders from data and coordinate the correction with each consuming team, then find the root cause, a check-then-act across instances with no database constraint, and the detection gap that let it run for hours. Afterwards we added a unique constraint, idempotency keys in consumers, an outbox and duplicate alerts, shared a blameless review widely, and added the pattern to the design review checklist.
Open with
My first priority would be to stop the damage, not to explain it. I would turn off the shipment creation path with a feature flag if we have one, or deploy a quick guard that refuses a second shipment per order, and ask the teams consuming our events to pause their consumers so the duplicates stop spreading.
Then, if they want more
I would set up a clear incident structure with a lead, a communications person and a timeline.
Next I would quantify the impact from data: a query for orders with more than one shipment in the window, shared as one list with every consuming team, so warehouse, billing and notifications can each correct their side, with an owner and a deadline per system. Once things are stable, I would find the root cause.
Typically it is a check-then-act, find no shipment then insert one, running on two instances after a redelivery, with nothing in the database enforcing one shipment per order. I would also treat the hours of silence as a second finding.
Afterwards I would add a unique constraint on the order id, idempotency keys in every consumer, a transactional outbox, and alerts on shipments per order and event ratios.
Then I would write a blameless review, share it across engineering, and add a question about duplicate side effects to our design review checklist so other teams find the same pattern before it bites them.
About this answer
- Last updated
- 12 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
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "A race condition in an order service caused duplicate shipments for several hours before...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/52c2b31e56/