What exactly-once really means
The word exactly-once appears in broker documentation and comes up often in interviews. But it stands for two different promises. The broker gives one, and the other can only be had in the consumer's code. Whoever mixes them up risks processing one payment twice.
A courier service promises that the warehouse never registers one parcel twice. If the shop hands in the same parcel a second time by mistake, the warehouse recognizes it by its number. The promise is honest, but it is about the warehouse. The courier can still come to the customer again if the signature went missing, and that is for the customer to sort out.
The analogy is off in one way: the warehouse remembers every number, while Kafka recognizes a resend only until the producer restarts.
The broker's promise
Kafka can do exactly-once inside itself, and it has two mechanisms for that:
- the idempotent producer: by the producer's id and the message's sequence number the broker recognizes a resend and does not store it a second time;
- transactions: a program that reads from Kafka and writes to Kafka commits its new messages and the offset of the ones it read in one step — all or nothing.
What matters is the boundary of these mechanisms: all of this works only inside Kafka. A Kafka transaction covers neither a write to your database, nor sending an email to a customer, nor a call to another service.
The end-to-end promise
What the user cares about is the effect: the money is credited once, a single email arrives. The broker cannot promise that: it does not see your database and does not know whether the consumer finished the action before crashing. Only the consumer itself knows that.
So in practice you build effectively-once: at-least-once delivery and an idempotent consumer. A message may arrive twice, but the effect happens once.
The id and the action in one transaction
In the deduplication exercise the consumer kept the processed ids in memory. A real service keeps them in the database, in a processed table with the primary key message_id. If the id and the action are written separately, a crash between the two writes leads to one of two failures:
- the action is written but the id is not — a repeat performs the action again;
- the id is written but the action is not — a repeat is skipped, and the action is lost.
The fix is to write the id and the action in one database transaction: it saves both writes together or neither.
BEGIN;
INSERT INTO processed(message_id) VALUES ($1) ON CONFLICT DO NOTHING;
UPDATE accounts SET balance = balance + $1 WHERE id = $2;
COMMIT;The consumer sends these commands to the database one at a time, each with its own parameters. In the INSERT it passes the message id for $1, and in the UPDATE the amount and the account number for $1 and $2. Let's go step by step:
BEGINopens the transaction.INSERT … ON CONFLICT DO NOTHINGtries to write the id. If this id is already there, the command does not fail with an error; it inserts zero rows.- The consumer checks how many rows were inserted. Zero means the message has already been processed: the consumer rolls the transaction back with
ROLLBACK, skips theUPDATEand acknowledges the message. - One row means the message is new, and the
UPDATEcredits the money. COMMITsaves the id and the credit together.- Only after
COMMITdoes the consumer acknowledge the message to the broker.
Let's check both crashes. The consumer crashed before COMMIT: the database undoes both writes, and the repeat runs as a new message. It crashed after COMMIT but before the acknowledgement: the repeat finds the id and is skipped.
The producer's side
The producer has the mirror problem: write a change to its database and send a message to the broker, and these are two different systems. If the producer crashes between them, the message is lost or goes out without the database write. The outbox pattern solves this; it comes later in the course.
How to answer in an interview
To the question "does Kafka give exactly-once?" a good answer tells the two promises apart:
- inside Kafka, yes: the idempotent producer and transactions;
- for an effect in an external system, no: you need at-least-once and an idempotent consumer.
Key points
- The word exactly-once names two different promises: the broker's promise, which holds only inside Kafka, and the end-to-end one, about the effect in an external system.
- The end-to-end promise comes from effectively-once: at-least-once plus an idempotent consumer.
- The message id and the action are written in one transaction, and the message is acknowledged after
COMMIT. - The chapter in one line: acknowledge a message after the work and make the consumer idempotent, so nothing is lost and no effect repeats.