Quick revision
The short answer
Kafka appends whatever producers send; it has no compare-and-append on a key's last version, so two instances that read the same balance of 100 both succeed in appending a debit of 80. The fix is a single writer per account: send Withdraw commands to a commands topic keyed by account id, so the one instance that owns that partition holds the account's state, validates each command in turn and emits the event, using Kafka Streams or a transactional consume-process-produce loop with exactly_once_v2 so state, events and offsets commit together and zombies are fenced. The alternative is an event store in a database with a unique (account_id, version) constraint plus an outbox; invariants across accounts still need a saga.
What to remember
In this answer, we saw that Kafka allowed the overdraft because it appends whatever producers send and has no compare-and-append on a key's version, so two writers that read the same balance both succeed. We designed a single writer per account by routing Withdraw commands through a topic keyed by account id, with Kafka Streams or a transactional loop using exactly_once_v2 so state, events and offsets commit together and zombies are fenced. The database alternative uses a unique (account_id, version) constraint and an outbox, and in both designs, rules across accounts need a saga.
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 appends whatever producers send; it has no compare-and-append on a key's last version, so two instances that read the same balance of 100 both succeed in appending a debit of 80. The fix is a single writer per account: send Withdraw commands to a commands topic keyed by account id, so the one instance that owns that partition holds the account's state, validates each command in turn and emits the event, using Kafka Streams or a transactional consume-process-produce loop with exactly_once_v2 so state, events and offsets commit together and zombies are fenced. The alternative is an event store in a database with a unique (account_id, version) constraint plus an outbox; invariants across accounts still need a saga.
Why this question is asked
Event sourcing stores every change as an event and rebuilds state by replaying them, and Kafka looks like a natural home for such events: durable, ordered and replayable. This incident shows the piece that is missing. Interviewers ask it to see whether we understand what an event store must guarantee on write, why Kafka does not provide that by itself, and how to design around it. In this answer, we will recap event sourcing and optimistic concurrency, replay the race, design a single-writer path with Kafka, look at the database alternative, and see where both stop.
1A Quick Recap: Event Sourcing and Optimistic Concurrency
In an event-sourced account, the balance is not stored directly. We store the events, AccountCredited 100, AccountDebited 30 and so on, and the balance is what we get by applying them in order. To handle a withdrawal, the service loads the account's events, computes the balance, checks the rule that the balance must not go below zero, and appends a new AccountDebited event.
That check is only valid if nothing changes between reading and appending. Event stores normally guarantee this with optimistic concurrency: the writer says append this event as version 8 of account 42, and the store refuses if version 8 already exists, because that means someone else wrote first. The losing writer reloads and checks again. This compare-and-append is what keeps business rules true under concurrency.
2Why Kafka Allowed the Overdraft
Kafka has no such check. A producer sends a record to a partition, and the broker appends it. It does not know what version of the account the producer saw, and there is no API to append only if the last record for this key is still X. Idempotent producers only prevent duplicates of the same send after a retry; they do not compare against other producers.
So the race is simple. Instance A and instance B both receive a withdrawal of 80 for the same account at the same moment. Both read the events and compute a balance of 100. Both check 100 - 80 >= 0, which passes. Both append AccountDebited 80, and both appends succeed. The balance is now 100 - 80 - 80 = -60.
7 messages in order. Play them, or step through them one at a time.
Both checks were correct for what each instance saw; nothing compared their writes.
3Single Writer per Account
If Kafka cannot arbitrate between two writers, we make sure there is only one writer per account. Requests no longer write events directly. Instead, they send a command, Withdraw(account 42, 80), to an account-commands topic keyed by account id. All commands for account 42 go to the same partition, and in a consumer group exactly one instance owns that partition at a time. That instance holds the current state of every account in its partitions, for example in a local state store, and handles commands one at a time: check the balance, then emit AccountDebited or WithdrawalRejected.
Now the race cannot happen. The two withdrawals are two records in the same partition. The owner processes the first, the balance becomes 20, and the second sees 20, fails the check and is rejected. The ordering that Kafka does guarantee, within a partition, does the job that compare-and-append did in the event store.
- Withdraw(42, 80) keyed by account id
- Owner of the partition reads it
- Load balance from local state100
- Balance - 80 >= 0?YesEmit AccountDebited, update state to 20NoEmit WithdrawalRejected
- Commit state, event and offset together
5 steps in order. Play them, or step through them one at a time.
4Making the Single Writer Safe
The single writer has two failure modes to cover. If it crashes after emitting an event but before committing its input offset, the command will be processed again and the debit emitted twice. And after a rebalance, an old instance that was paused by a long garbage collection, a zombie, may wake up and still believe it owns the partition.
Kafka transactions solve both. In a consume-process-produce loop, the instance reads a command, updates its state, sends the event and the state change (to a changelog topic) and adds the input offset to the same transaction with sendOffsetsToTransaction. Either all of them commit or none do, and read_committed consumers never see aborted events. Kafka Streams does this for us with processing.guarantee=exactly_once_v2: the state store's changelog, the output events and the input offsets commit atomically. Zombies are fenced too: a transactional producer with a newer epoch fences the old one, and with exactly_once_v2 the offset commit also carries the consumer group generation, so a zombie from an older generation cannot commit.
Notice the cost: the account service is now asynchronous. The caller sends a command and learns the result from an event, or waits for it with a timeout, which changes the API.
5The Database Alternative
If an asynchronous command path does not fit, keep the event store in a database. A table of events with a unique constraint on (account_id, version) gives optimistic concurrency directly: both instances read version 7, both try to insert version 8, one insert succeeds and the other fails with a unique violation, reloads, sees a balance of 20 and rejects the withdrawal. The accepted events are then published to Kafka through an outbox: written to an outbox table in the same transaction and relayed by a poller or by change data capture. Kafka still distributes the events to every reader, but it is no longer the place where writes are decided.
6Where Both Designs Stop
A single writer per account enforces rules within one account. A transfer from account 42 to account 77 touches two accounts that may live in different partitions with different owners, so no single writer sees both. Rules across keys need a saga: debit 42 first, then credit 77, and if the credit fails, issue a compensating credit to 42. The database design has the same boundary unless both accounts are updated in one database transaction, which is often impossible once accounts are spread across shards.
7Follow-up questions
Is a Kafka transaction enough without the single writer?
No. Transactions make one writer's consume, state and produce steps atomic, but two writers with their own transactions can still both read 100 and both commit a debit. The single writer is what serialises the decisions; transactions make that writer crash-safe.
How does the caller get the result of a Withdraw command?
It can subscribe to the account's result events and match them by command id, or the service exposes an endpoint that waits for the result with a short timeout. The command id also makes retries safe, since the writer can ignore a command it has already processed.
What happens during a rebalance in the single-writer design?
The partition and its state move to another instance, which restores the state store from its changelog before processing. Commands for those accounts wait during the move, and fencing makes sure the old owner cannot commit anything afterwards.
8Conclusion
In this answer, we saw that Kafka allowed the overdraft because it appends whatever producers send and has no compare-and-append on a key's version, so two writers that read the same balance both succeed. We designed a single writer per account by routing Withdraw commands through a topic keyed by account id, with Kafka Streams or a transactional loop using exactly_once_v2 so state, events and offsets commit together and zombies are fenced. The database alternative uses a unique (account_id, version) constraint and an outbox, and in both designs, rules across accounts need a saga.
Open with
Kafka allowed it because it appends whatever producers send. An event store normally offers compare-and-append: write this as version 8 only if version 8 does not exist yet.
Then, if they want more
Kafka has nothing like that, so two instances that both read a balance of 100 both pass the check and both append a debit of 80, leaving -60. To prevent it I would make sure there is only one writer per account.
Requests become Withdraw commands on a commands topic keyed by account id; the one instance that owns that partition holds the account state, validates commands one at a time and emits AccountDebited or WithdrawalRejected. The second withdrawal then sees a balance of 20 and is rejected.
I would build it with Kafka Streams or a transactional consume-process-produce loop with exactly_once_v2, so the state update, the emitted event and the input offset commit together, and a zombie instance after a rebalance is fenced.
If the asynchronous command path does not fit, the alternative is an event store in a database with a unique constraint on account id and version, which gives optimistic concurrency, and an outbox to publish the events to Kafka. Either way, rules across accounts, like a transfer, cannot be enforced by one writer and need a saga with compensation.
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 "Explain why Kafka allowed this, and design the write path so it cannot happen.": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/a94b66a642/