Scrivio Interview library
DebuggingFoundations7 min read · updated 12 November 2026

A Spring Boot service consumes Kafka events to update a projection table. Occasionally a customer's status in the projection goes from ACTIVE back to PENDING even though the source system shows ACTIVE. What are the likely causes and how would you fix them?

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.

Java
// Producer: no key, so events for one customer spread over partitions
kafkaTemplate.send("customer-status", event);
// Consumer: last write wins, whatever its age
@KafkaListener(topics = "customer-status", concurrency = "3")
void on(CustomerStatusChanged e) {
jdbc.update("UPDATE customer_status SET status = ? WHERE customer_id = ?",
e.status(), e.customerId());
}
// t=0 PENDING (v1) -> partition 2 (slow consumer)
// t=1 ACTIVE (v2) -> partition 0 (fast consumer applies it: ACTIVE)
// t=3 partition 2 catches up, applies v1 -> PENDING <- wrong

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.

Causes and what each one needs
CauseHow the old event winsFix
No key or wrong keyversions of one customer on different partitionskey = customerId everywhere
Partition count changedsame key maps to a new partitionplan partitions up front; version check
Retry topic or producer retryfailed old event reprocessed lateridempotent producer; version check
Rebalance redeliveryuncommitted records read againversioned, idempotent upsert
Threads inside one partitiontwo events for a key run in parallelkeep per-key ordering
Only the first row is fully fixed by keys. Every other row needs the projection to defend itself.

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.

Java
kafkaTemplate.send("customer-status", e.customerId().toString(), e); // key = customer id
// spring.kafka.producer.properties.enable.idempotence=true (default in recent clients)

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.

Java
-- PostgreSQL: versioned, idempotent upsert
INSERT INTO customer_status (customer_id, status, version)
VALUES (:id, :status, :version)
ON CONFLICT (customer_id) DO UPDATE
SET status = EXCLUDED.status, version = EXCLUDED.version
WHERE customer_status.version < EXCLUDED.version;
-- apply v2 ACTIVE -> 1 row updated, status ACTIVE
-- apply v1 PENDING -> 0 rows updated, status stays ACTIVE
-- apply v2 again -> 0 rows updated (replay is harmless)

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.

What to say in the interviewAbout 90 seconds aloud

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.
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/