Quick revision
The short answer
Kafka orders records only within one partition of one topic, so partition 5 of orders and partition 5 of payments are two separate logs written by two different producers, and the same key and partition number create no order between them. The shipping consumer fetches from both independently, so which record it sees first depends on fetch timing, batching and lag. We fix it either by making the consumer tolerant, storing an early PaymentCaptured as pending and applying it when OrderCreated arrives, or by publishing the order's whole lifecycle to one topic keyed by order id. Sorting by record timestamps from different producers is not a fix.
What to remember
In this answer, we saw that Kafka orders records only inside one partition of one topic, so the same key and partition number on orders and payments give two independent logs, and the consumer's fetch timing decides which event comes first. We can fix it by making the consumer tolerant, parking early payments or using a state machine with idempotent updates, or by having the order service publish the whole lifecycle, including OrderPaid, to one topic keyed by order id. Sorting by timestamps from different producers is unreliable because clocks and retries skew them.
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.
Kafka orders records only within one partition of one topic, so partition 5 of orders and partition 5 of payments are two separate logs written by two different producers, and the same key and partition number create no order between them. The shipping consumer fetches from both independently, so which record it sees first depends on fetch timing, batching and lag. We fix it either by making the consumer tolerant, storing an early PaymentCaptured as pending and applying it when OrderCreated arrives, or by publishing the order's whole lifecycle to one topic keyed by order id. Sorting by record timestamps from different producers is not a fix.
Why this question is asked
This scenario catches many teams, because the reasoning seems sound: same key, same partition count, so same partition number, so the events must line up. Interviewers ask it to check that we know exactly what Kafka's ordering guarantee covers, and that we can offer practical fixes with their trade-offs. In this answer, we will recap the guarantee, see why the two topics drift apart, implement a tolerant consumer, look at the single-topic alternative, and explain why timestamps do not help.
1A Quick Recap of Kafka's Ordering Guarantee
Each partition is an independent append-only log. Kafka promises that records in one partition are read in the order they were appended, and nothing more. There is no order between partitions of the same topic, and none between topics.
Using the same key and the same number of partitions does put order 1001 on partition 5 of both topics, because the partitioner computes the same hash modulo 12. But partition 5 of orders and partition 5 of payments are still two separate logs, on possibly different brokers, written by two different producers, the order service and the payment service. Sharing a number is a coincidence of arithmetic, not a shared timeline.
2Why the Consumer Sees Payments First
The shipping service subscribes to both topics. Its consumer fetches from orders-5 and payments-5 independently, in batches, and the order in which records come out of poll() depends on which fetch returns first, how batches line up and how far behind the consumer is on each topic.
Suppose the order service publishes OrderCreated at 10:00:00.000 and the payment service publishes PaymentCaptured at 10:00:00.300. If the consumer is 2 seconds behind on orders, because that topic is busier, and up to date on payments, it receives PaymentCaptured first. It looks up order 1001, finds nothing and fails. Retrying a moment later would succeed, which is why the failure is only occasional.
7 messages in order. Play them, or step through them one at a time.
3Fix 1: Make the Consumer Tolerant of Order
The most robust fix accepts that events from different sources arrive in any order. When PaymentCaptured arrives for an unknown order, the consumer stores it as pending, for example in a table keyed by order id, and commits the offset. When OrderCreated arrives, the consumer creates the order and applies any pending events for it.
An even cleaner variant is a state machine: each order has a row that both events upsert into, recording payment captured and order created as separate facts, and shipping starts only when both are true. Either way, the consumer no longer depends on arrival order. It must also be idempotent, so a redelivered event does not apply twice, and pending entries need a timeout alert in case the order never arrives.
4Fix 2: Publish the Lifecycle to One Topic
If shipping really needs a total order of an order's lifecycle, the events must come from one log. One way is for the order service to own the lifecycle: it consumes PaymentCaptured from the payment service, records the payment against the order, and publishes OrderPaid to the orders topic, keyed by order id. Now shipping reads only orders, and OrderCreated always precedes OrderPaid on partition 5, because the order service only publishes OrderPaid after the order exists.
This moves the ordering responsibility to the service that owns the order, which is usually the right place. Its cost is an extra hop of latency and a dependency of the order service on payment events.
| Tolerant consumer | One lifecycle topic | |
|---|---|---|
| Where order is handled | In each consumer | In the owning service |
| Extra latency | None | One processing hop |
| Consumer complexity | Pending store or state machine | Simple |
| Works for other consumers | Each must do the same | Everyone gets the order |
5Why Sorting by Timestamp Does Not Work
It is tempting to buffer events and sort them by record timestamp. But timestamps from different producers come from different clocks, which can differ by tens of milliseconds or more, and a producer that retries a send may set the timestamp before a delay. An event that happened later can carry an earlier timestamp. Sorting also needs a waiting window, and any window can be exceeded by lag. Timestamps are useful for monitoring, but not as an ordering mechanism across producers.
6Follow-up questions
Would using one producer for both topics make them ordered?
No. Even a single producer writing to two topics creates two logs, and the consumer still fetches them independently. Ordering requires one partition, which means one topic.
How long should pending payments be kept?
Long enough to cover normal lag and outages, for example a day, with an alert well before that. A payment whose order never arrives points to a real problem, such as a lost event or a bug in the order service, and should reach a person rather than being silently dropped.
7Conclusion
In this answer, we saw that Kafka orders records only inside one partition of one topic, so the same key and partition number on orders and payments give two independent logs, and the consumer's fetch timing decides which event comes first. We can fix it by making the consumer tolerant, parking early payments or using a state machine with idempotent updates, or by having the order service publish the whole lifecycle, including OrderPaid, to one topic keyed by order id. Sorting by timestamps from different producers is unreliable because clocks and retries skew them.
Open with
Ordering in Kafka exists only within one partition of one topic. Keying both topics by order id with 12 partitions does put order 1001 on partition 5 of each, but partition 5 of orders and partition 5 of payments are two separate logs, written by two different producers.
Then, if they want more
The shipping consumer fetches from both independently, so which record it sees first depends on fetch timing, batching and how much lag it has on each topic. If it is behind on orders and caught up on payments, PaymentCaptured arrives first. I would give two fixes.
First, make the consumer tolerant: when PaymentCaptured arrives for an unknown order, store it as pending in a table keyed by order id, and apply it when OrderCreated arrives; or model the order as a state machine that both events upsert into, so arrival order does not matter. The consumer must be idempotent and pending entries need a timeout alert.
Second, if shipping needs a total order of the lifecycle, publish the lifecycle to a single topic keyed by order id, for example by having the order service consume the payment and publish OrderPaid after recording it. I would not sort by record timestamps from different producers, because clocks differ and retries skew them, so they are unreliable for ordering.
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 "The team expected the same key and partition number to keep them in order. Explain, and...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/274f706225/