Quick revision
The short answer
The principles I would set are: never depend on order across different event types or listeners unless it is guaranteed and documented; where order matters, scope it to a key and use a mechanism that guarantees it, such as all events for one key on one partition; design consumers to handle out-of-order and duplicate events, for example with entity versions; make flows that need strict order explicit workflows; and review event designs, including their ordering assumptions, as part of architecture reviews.
What to remember
In this answer, we set principles for event ordering. We do not depend on order across event types or listeners unless it is guaranteed and documented; we scope order to a key and use a mechanism such as one partition per key; we make consumers tolerate disorder and duplicates with versions; we turn flows that need strict order into explicit workflows; and we review ordering assumptions in architecture reviews so the questions become habit.
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 principles I would set are: never depend on order across different event types or listeners unless it is guaranteed and documented; where order matters, scope it to a key and use a mechanism that guarantees it, such as all events for one key on one partition; design consumers to handle out-of-order and duplicate events, for example with entity versions; make flows that need strict order explicit workflows; and review event designs, including their ordering assumptions, as part of architecture reviews.
Why this question is asked
Ordering bugs are among the hardest in event-driven systems because they appear only under particular timing: a retry, a rebalance, a slow consumer, a listener made asynchronous. The code looks correct and passes tests, and then once a week an order is shown as shipped before it was paid. Interviewers ask a twenty-year engineer for principles because the fix is not one technique but a set of habits teams apply when they design flows. In this answer, we will look at why ordering breaks in-process and across brokers, then go through each principle with the reasoning and an example, and close with how to make them part of the review process.
1Why Ordering Breaks
Inside one process, listeners for the same event run in an order that is unspecified unless we set @Order, and even then only for synchronous listeners; adding @Async lets them finish in any order. Across a broker, the picture is similar but at a larger scale. Kafka keeps order only within one partition. Two events for the same order sent with different keys can land on different partitions and be consumed in either order. Retries make it worse: if event 1 fails and is retried while event 2 succeeds, the consumer sees 2 before 1. Consumer group rebalances and redeliveries add duplicates on top.
So in practice there is no global order in a distributed system, and only limited order inside one process. Every design that assumes more is betting on timing.
2Principle 1: No Ordering Across Types or Listeners Unless Guaranteed
The first rule is negative: a design must not depend on PaymentCaptured arriving after OrderPlaced, or on listener A running before listener B, unless the mechanism guarantees it and the guarantee is written down where the next developer will see it. Most ordering bugs come from an assumption that held in testing because events happened to arrive in sequence.
3Principle 2: Scope Order to a Key and Use a Mechanism That Guarantees It
Usually what the business needs is not global order but order per entity: the events of one order must be processed in sequence, while different orders are independent. That is a much cheaper guarantee. In Kafka, we use the entity id as the message key, so all events for one order go to the same partition and one consumer processes them in sequence.
Notice that this only works if every producer of events for that entity uses the same key and the same topic, and if consumers do not hand messages to a thread pool that reorders them. Those conditions are exactly what the guarantee should document.
4Principle 3: Make Consumers Tolerate Disorder and Duplicates
Even with keyed partitions, duplicates happen after retries and rebalances, and some flows cross topics where order is not guaranteed. So consumers are designed defensively. The common technique is a version number on each event for an entity. The consumer stores the last version it applied and ignores events with a version that is not newer.
| Problem | Technique | Cost |
|---|---|---|
| One entity's events reordered | key by entity id, one partition per key | hot keys limit parallelism |
| Stale or duplicate events | entity version, apply only newer | a version field in every event |
| Prerequisite not seen yet | park and retry later | a retry store and monitoring |
| Steps that must run in sequence | explicit workflow or saga | an orchestrator to build and run |
This makes the consumer correct whatever order and however many times events arrive. Where an event cannot be applied yet because a prerequisite is missing, such as a payment for an order the consumer has not seen, the consumer can park it and retry later rather than failing or applying it wrongly.
5Principle 4: Strict Order Means an Explicit Workflow
When a business process has steps that must happen in sequence, such as reserve stock, then charge the card, then ship, it should not be built from events whose order we hope for. It should be an explicit workflow: an orchestrator, sometimes called a saga when it also handles compensation, that knows the steps, triggers each one, waits for its result and decides the next. The order lives in one place, failures have a defined handling, and the state of each instance can be inspected. Events remain useful for announcing results, but the sequence is owned by code.
6Principle 5: Review Ordering in Architecture Reviews
Principles only work if someone checks them. In architecture reviews of a new event flow, I would ask three questions: what ordering does this flow assume, which mechanism guarantees it, and what does each consumer do with a duplicate or an out-of-order event? Designs that cannot answer get fixed before they are built. Over time the questions become habit and teams answer them in the design document unasked, which is the real goal.
7Follow-up questions
Why not use a single partition to get global order?
Because a single partition is processed by one consumer at a time, which caps throughput for the whole topic, and it still does not order events across different topics or services. Ordering per key gives the guarantee the business usually needs while keeping parallelism.
What is a hot key and why does it matter here?
A key that receives a large share of the events, such as one very large customer. Since all its events go to one partition, that partition and its consumer become a bottleneck. Choosing a finer key, such as order id instead of customer id, often avoids it, if the ordering need allows.
Do these principles apply to in-process Spring events?
Yes, at a smaller scale. Within a service, do not rely on listener order unless it is synchronous and set with @Order, make dependent steps explicit, and keep listeners idempotent if they are backed by a publication registry that can resubmit them.
8Conclusion
In this answer, we set principles for event ordering. We do not depend on order across event types or listeners unless it is guaranteed and documented; we scope order to a key and use a mechanism such as one partition per key; we make consumers tolerate disorder and duplicates with versions; we turn flows that need strict order into explicit workflows; and we review ordering assumptions in architecture reviews so the questions become habit.
Open with
The first principle is that nobody depends on order across different event types or listeners unless a mechanism guarantees it and it is documented. Most ordering bugs come from an assumption that held in testing.
Then, if they want more
Second, when order matters, it is almost always order per entity, not global order, so we scope it to a key and use a mechanism that guarantees it: in Kafka, the entity id as the message key so all events of one order land on one partition, and consumers that do not reorder them in a thread pool.
Third, consumers must tolerate out-of-order and duplicate events anyway, because retries and rebalances happen. I would put a version on every entity event and have consumers apply only newer versions, for example with an update that checks the stored version, and park events whose prerequisites have not arrived yet.
Fourth, if a business process needs strict sequence, like reserve, charge, ship, it should be an explicit workflow or saga with an orchestrator, not a chain of events whose order we hope for.
And fifth, I would make ordering a standard question in architecture reviews: what order does this flow assume, what guarantees it, and what does each consumer do with a duplicate or a late event. In-process, the same rules apply on a smaller scale.
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
- Spring Framework reference, Core technologies
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 "Event ordering assumptions have caused subtle bugs in several of your systems...": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/24efc39c21/