Scrivio Interview library
DesignIntermediate6 min read · updated 13 November 2026

A wallet system publishes money transfers between accounts, about 4,000 transfers per second. Each account's balance changes must be applied in order, and no account may go negative.

A transfer touches two accounts that usually hash to different partitions. Which key do you use, and how do you design the flow so both sides stay ordered and correct?

One record cannot be ordered under two keys, so we split the transfer into per-account steps. TransferRequested is keyed by the source account; the processor that owns that partition is the single writer for the source balance, checks funds and emits DebitApplied (or TransferRejected) keyed by the source, plus a CreditRequested keyed by the destination in the same transaction. The destination's owner applies the credit. The two sides become consistent eventually, as a small saga with a compensation if the credit can never be applied, every step carries the transfer id for idempotence, and exactly-once processing ensures a crash or rebalance never applies a debit twice.

Why this question is asked

Money movement is where event-driven design meets hard correctness requirements. Each account's balance must change in order, a balance may never go negative, and a transfer touches two accounts that will usually live on different partitions. Interviewers ask this design question to see whether we can stop looking for the one perfect key and instead shape the flow so that every decision is made by a single owner. In this answer, we will see why one key cannot work, design the two-step flow, look at how exactly-once processing protects it, handle compensations and idempotence, and size it for 4,000 transfers a second.

1Why One Key Cannot Work

Kafka orders records per partition, and the key picks the partition. To apply an account's balance changes in order, every change to that account must be keyed by that account. A transfer from account A to account B changes two accounts. If we key the transfer by A, it is ordered with A's other changes but not with B's; keyed by B, the reverse. There is no key that puts one record on two partitions.

So instead of choosing between the source and the destination, we split the transfer into two records, one per account, each keyed by the account it changes. Each account then has a single writer, the stream task that owns its partition, and that writer sees every change to the account in order.

2The Two-Step Flow

Step 1: the API publishes TransferRequested keyed by the source account A, with a transfer id, the destination B and the amount. The processor that owns A's partition holds A's balance in a state store, a local key-value store backed by a changelog topic. Because it is the only writer for A, it can check the balance and debit it without races: if A has 100 and the transfer is 30, the balance becomes 70 and it emits DebitApplied keyed by A. If A has 20, it emits TransferRejected and nothing changes.

Step 2: in the same transaction as the debit, the processor emits CreditRequested keyed by the destination B. The processor that owns B's partition applies the credit and emits CreditApplied. If the account topics and the credit topic have the same partition count and key format, they are co-partitioned, so one task owns both the debits and the credits of each account and remains its single writer.

Step throughA transfer as two per-account steps
APIOwner of AKafkaOwner of B
TransferRequested t-9, key A, 30
deliver to A's partition
balance 100 >= 30: debit to 70
DebitApplied key A + CreditRequested key B (one txn)
deliver CreditRequested t-9
credit B, remember t-9
CreditApplied t-9, key B

7 messages in order. Play them, or step through them one at a time.

Each balance has exactly one writer, so the funds check and the update never race.

3Exactly-Once Processing Holds It Together

Without transactions, a crash between updating the balance and committing the input offset would make the processor read TransferRequested again after restart and debit twice. Kafka Streams with processing.guarantee=exactly_once_v2 wraps each unit of work in a Kafka transaction: the state store changelog write, the output records and the input offset commit are committed together or not at all. After a crash or a rebalance, the new owner sees either the whole result or none of it, so a debit is applied exactly once.

Java
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "wallet-ledger");
props.put(StreamsConfig.PROCESSING_GUARANTEE_CONFIG, StreamsConfig.EXACTLY_ONCE_V2);
builder.addStateStore(Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore("balances"), Serdes.String(), moneySerde));
builder.stream(List.of("transfer-requests", "credit-requests")) // both keyed by account, same partition count
.process(AccountProcessor::new, "balances") // single writer per account
.to((key, event, ctx) -> event instanceof CreditRequested
? "credit-requests" // re-keyed to destination account B
: "account-events"); // DebitApplied, CreditApplied, TransferRejected
// inside AccountProcessor for TransferRequested t-9 (A -> B, 30):
// balance(A) = 100 -> 70, forward DebitApplied(key A), forward CreditRequested(key B)

If the balances live in a database instead of a state store, the same guarantee comes from a database transaction plus an outbox: update the balance, insert the outgoing events into an outbox table, and record the processed transfer id, all in one commit. The table must be keyed the same way as the topic so one consumer owns each account.

4A Small Saga With Idempotent Steps

The two sides are consistent eventually, not atomically. For a short moment, A has been debited and B not yet credited; the money is in flight. That is acceptable as long as the system always finishes the transfer one way or the other. This sequence of local steps with compensations is called a saga.

The compensation covers a credit that can never be applied, for example because B was closed or frozen. B's owner emits CreditRejected for t-9, which leads to a RefundRequested keyed by A, and A's owner credits the 30 back, marking the transfer failed. Every record carries the transfer id, and each owner remembers the transfer ids it has applied, so a retried request, from the API or a replay, is recognised and ignored instead of moving money twice.

5Sizing for 4,000 Transfers a Second

Each transfer produces roughly four to five records, so about 20,000 records a second, small for Kafka. The real limits are per account and per partition. A single account receiving very many transfers, such as a merchant settlement account, becomes a hot key, because its single writer processes its changes one by one. Such accounts may need special handling, for example sub-accounts that are aggregated later. With 32 to 64 partitions, the load spreads across enough stream tasks, and the transactional overhead stays modest with a commit interval around 100 milliseconds.

6Follow-up questions

How do you report the balance a user sees while a credit is still in flight?

We show the states honestly. For the sender, the debit is applied at once, so their available balance already excludes the 30, and the transfer shows as pending. For the receiver, the credit is not spendable until CreditApplied, but we can show it as incoming. The read model, built from account-events or queried from the state store through interactive queries, holds both the available balance and pending items keyed by transfer id, so the app never shows money that cannot yet be spent.

Why not use a database transaction across both accounts instead?

In a single database, a transaction locking both rows works well and is simpler at moderate scale. The Kafka design fits when accounts are partitioned across many processors for throughput, where a cross-partition atomic update is not available, and the saga gives the same final result with each step local to one owner.

7Conclusion

In this answer, we saw that a transfer cannot be ordered under two keys, so we split it into per-account steps. TransferRequested keyed by the source lets the single writer for that account check funds and debit, emitting DebitApplied and, in the same transaction, CreditRequested keyed by the destination, which its own owner applies. The transfer is a small saga with a compensation if the credit fails, every step carries the transfer id for idempotence, and exactly-once processing, or a database plus outbox keyed the same way, ensures that crashes and rebalances never apply a debit twice.

What to say in the interviewAbout 80 seconds aloud

Open with

A transfer touches two accounts, and one record cannot be in order under two keys, so I would not choose between source and destination as the key. Instead I would split the transfer into per-account steps.

Then, if they want more

Step one: TransferRequested is keyed by the source account. The stream task that owns that partition is the single writer for the source balance, held in a state store or a database keyed the same way. It checks the funds and emits DebitApplied, or TransferRejected if the balance is too low, keyed by the source account.

Step two: in the same transaction as the debit, it emits CreditRequested keyed by the destination account, using Kafka transactions or a database plus outbox, and the owner of the destination partition applies the credit. If the topics are co-partitioned, one task owns both debits and credits for each account.

The two sides become consistent eventually, not atomically, so the transfer is a small saga: if the credit can never be applied, a compensation refunds the source. Every step carries the transfer id, so retries are idempotent.

With exactly-once processing, reading, updating state, writing outputs and committing offsets happen in one transaction, so a crash or rebalance cannot apply a debit twice.

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 "A transfer touches two accounts that usually hash to different partitions. Which key do...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/069c1e08af/