Quick revision
The short answer
A projection that moves from ACTIVE back to PENDING has almost always applied an older event after a newer one. The usual causes are events for one customer landing on different partitions, retries or redelivery after a rebalance replaying an old event, and one partition being processed by several threads. We fix it by keying messages with the customer id so one customer's events stay in order, and by making the projection version-aware so it ignores any event older than what it already stored.
What to remember
In this answer, we saw that a status going backwards means an older event was applied after a newer one. Kafka orders messages only within one partition, so missing or inconsistent keys, retries, rebalance redelivery and parallel processing inside a partition can all reorder events for one customer. The fix has two layers: send every event keyed by the entity id so one entity stays on one partition, and store the source version in the projection with a conditional upsert so that stale and duplicate events are ignored.
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.
A projection that moves from ACTIVE back to PENDING has almost always applied an older event after a newer one. The usual causes are events for one customer landing on different partitions, retries or redelivery after a rebalance replaying an old event, and one partition being processed by several threads. We fix it by keying messages with the customer id so one customer's events stay in order, and by making the projection version-aware so it ignores any event older than what it already stored.
Why this question is asked
Projections built from events look simple until they start going backwards. A status that flips back to an older value is one of the most common bugs in event-driven Spring Boot services, and it is hard to reproduce because it depends on timing. Interviewers ask about it because the answer shows whether we understand how Kafka actually orders messages, what happens during rebalances, and how to write a consumer that stays correct even when ordering is not perfect. In this answer, we will recap the ordering guarantee Kafka gives, reproduce the bug, walk through each likely cause, and then fix it in two layers: correct keys and partitions, and a version check in the projection that makes replays harmless.
1A Quick Recap of Kafka Ordering
A projection is a table that a service builds from events, here a customer_status table filled from CustomerStatusChanged events. A Kafka topic is split into partitions, and each partition is an ordered log. Kafka guarantees order within one partition and nothing across partitions. Which partition a message goes to depends on its key: the default partitioner hashes the key, so the same key always maps to the same partition. A message with a null key is spread across partitions.
So the guarantee we can rely on is narrow. Two events for customer 42 come out in the order they were written only if they have the same key and the consumer handles that partition one record at a time. If any of those conditions breaks, an older PENDING event can be processed after a newer ACTIVE one.
2Reproducing the Status Going Backwards
Here is the consumer as it is often first written. It simply copies the status from each event into the table.
Notice that the producer never set a key. PENDING and ACTIVE went to different partitions, and the consumer for partition 0 happened to be faster. The table ends with whichever event was processed last, not whichever happened last.
3The Likely Causes, One by One
The first cause is the one above: events for one entity in different partitions, because the key is missing, is a different field in different producers, or because someone increased the partition count, which changes the hash-to-partition mapping for existing keys. The second cause is retries. If the producer retries a send without the idempotent producer, or a consumer error handler sends a failed event to a retry topic and processes it later, an old event can arrive after newer ones.
The third cause is redelivery after a rebalance. A rebalance is when Kafka moves partitions between consumers in a group, for example during a deploy. If a consumer processed records but had not committed their offsets, the new owner of the partition reads them again. Replaying v1 after v2 has been applied sets the status back. The fourth cause is concurrency inside one consumer: handing records from one partition to a thread pool, or using a parallel consumer without key ordering, lets two events for the same customer run at the same time in any order. Note that concurrency = 3 on a @KafkaListener is safe on its own, because each container thread owns whole partitions.
| Cause | How the old event wins | Fix |
|---|---|---|
| No key or wrong key | versions of one customer on different partitions | key = customerId everywhere |
| Partition count changed | same key maps to a new partition | plan partitions up front; version check |
| Retry topic or producer retry | failed old event reprocessed later | idempotent producer; version check |
| Rebalance redelivery | uncommitted records read again | versioned, idempotent upsert |
| Threads inside one partition | two events for a key run in parallel | keep per-key ordering |
4Fix 1: Keep One Customer on One Partition
The first fix is to send every event with the entity id as the message key. Then all events for customer 42 go to the same partition, and a single consumer thread sees them in the order the producer wrote them. We should also enable the idempotent producer, which is the default in recent Kafka clients, so broker-side retries cannot reorder or duplicate within a partition.
But ordering by key is a property of the producer and the topology, and it can be broken by a retry topic, a migration or a second producer that forgets the key. So we do not rely on it alone.
5Fix 2: Make the Projection Version-Aware
The robust fix is to let each event carry the version of the source record, for example the JPA @Version value or a sequence number from the source table, and to store that version in the projection. The update only applies when the incoming version is newer. An old event, whether reordered, retried or replayed, then changes nothing.
Notice that this one statement makes the consumer both idempotent, so applying the same event twice has the same effect as applying it once, and order-tolerant. I would log or count the skipped updates, because a sudden rise in them points back to a producer or partitioning problem worth fixing at its source.
6Follow-up questions
What changes if the producer cannot supply a version number?
Then we need another source of order. If the source database exposes a log position, change data capture with a tool such as Debezium gives each change its log sequence number, which is a reliable version. An event timestamp can work only if it comes from one clock, the source database's commit time, and we accept the rare case of equal timestamps. Failing all of that, the consumer can treat the event as a notification and fetch the current state from the source by id, so it always writes the latest value whatever order events arrive in, at the cost of a call per event.
Why is concurrency = 3 on @KafkaListener not the cause?
Because Spring Kafka creates three consumer threads in the group and Kafka assigns each partition to exactly one of them. Every record of a partition is still processed by one thread in order. The problem only appears when our own code hands records of one partition to other threads, or when a library processes one partition in parallel without keying by the message key.
Would a compacted topic fix this?
Not by itself. Log compaction keeps the latest record per key in the log for new readers, but a running consumer still reads the records in the order they were written to each partition. Compaction helps rebuild a projection from scratch; it does not protect against wrong keys or replays.
7Conclusion
In this answer, we saw that a status going backwards means an older event was applied after a newer one. Kafka orders messages only within one partition, so missing or inconsistent keys, retries, rebalance redelivery and parallel processing inside a partition can all reorder events for one customer. The fix has two layers: send every event keyed by the entity id so one entity stays on one partition, and store the source version in the projection with a conditional upsert so that stale and duplicate events are ignored.
Open with
When a projection goes from ACTIVE back to PENDING, my first assumption is that an older event was applied after a newer one, because the consumer is doing last write wins.
Then, if they want more
Kafka only guarantees order within a partition, so I would check four things. Are events keyed by customer id, and is it the same key in every producer? If the key is missing, PENDING and ACTIVE can land on different partitions and be processed in either order.
Second, are there retries, either producer retries without idempotence or a retry topic that reprocesses an old failed event later? Third, redelivery after a rebalance: if offsets were not committed before a partition moved, the new consumer replays events that were already applied.
Fourth, does anyone process one partition with a thread pool, which breaks per-key order even with correct keys. The fix has two parts. I would key every event by the entity id so one customer always lands on one partition.
And I would make the projection version-aware: the event carries the source version, I store it, and I update with a conditional upsert where the stored version is lower than the incoming one. That makes stale events and replays harmless. I would also count the skipped updates, because a spike tells me ordering upstream is broken.
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 Cloud documentation, Resilience4j 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 "A Spring Boot service consumes Kafka events to update a projection table. Occasionally a...": https://scrivio-interview-library.netlify.app/spring-boot/distributed-data/9ac5f9cfd8/