Full answer
The three semantics
The difference is when the consumer acknowledges relative to doing the work.
- At-most-once: ack (or commit offset) first, then process. If the consumer crashes mid-work, the message is lost. Acceptable for metrics samples or "user is typing" signals.
- At-least-once: process first, then ack. If the consumer crashes after processing but before acking, the message is delivered again and processed twice. This is the default in most systems.
- Exactly-once: each message affects the system once.
Why "exactly-once delivery" is not possible in general
The sender cannot tell the difference between "the message was lost" and "the acknowledgement was lost". To avoid losing data it must resend, and the receiver may then get a duplicate. This is a form of the Two Generals problem. So the practical goal is exactly-once effect, built on top of at-least-once delivery.
How to get exactly-once effect
- Idempotent consumers. Give each message a unique ID. Before applying, check a processed-IDs table (in the same database transaction as the effect):
-- PostgreSQL syntax, shown for illustration (not run)
BEGIN;
INSERT INTO processed_messages (message_id) VALUES ('evt-8812')
ON CONFLICT DO NOTHING;
-- if the insert affected 0 rows, ROLLBACK: it was a duplicate
UPDATE wallets SET balance = balance + 500 WHERE user_id = 77;
COMMIT;Or design the operation to be naturally idempotent: SET status = 'shipped' is safe to repeat; balance = balance + 500 is not.
-
Transactional offset commits. Store the consumer's offset in the same database transaction as the result. On restart, resume from the stored offset. The result and the position can never disagree.
-
Broker-level transactions. Kafka offers idempotent producers (deduplicating retries per partition) and transactions that atomically write output records and consumer offsets. This gives exactly-once for read-process-write pipelines that stay inside Kafka. Side effects outside Kafka (sending an email, calling a payment provider) are not covered and still need idempotency keys.
Trade-offs
Exactly-once processing costs extra storage (dedup tables, with a cleanup policy), extra latency (transactions) and complexity. Use it where duplicates do real harm: payments, ledger entries, inventory. Use at-least-once with simple dedup for most event handling, and at-most-once only where loss is harmless.
By level
Fresher: Define the three semantics and explain that retries can cause duplicates, so the receiver should handle repeats. A fresher should know that retries can create duplicates.
Mid: Explain ack and offset timing, implement an idempotent consumer with a dedup table, and distinguish naturally idempotent operations. Expect a concrete dedup mechanism in the same transaction as the effect.
Senior: Discuss Kafka idempotent producers and transactions, transactional outbox, dedup retention windows, side effects through external APIs, and the latency and throughput cost of each guarantee. The answer should explain why broker transactions do not cover external side effects.
Key points
- The ack timing decides the semantics: before work is at-most-once, after work is at-least-once
- Lost message and lost ack look the same to the sender
- Aim for exactly-once effect: at-least-once plus idempotency
- Commit the result and the offset or message ID in one transaction
- Kafka transactions cover Kafka-to-Kafka pipelines, not external side effects
Common mistakes
- Claiming a broker gives exactly-once for external side effects like emails
- Using non-idempotent updates such as balance + x with at-least-once delivery
- Keeping dedup records forever without a retention plan
- Assuming retries never happen in normal operation
Follow-ups
- How would you make sending an SMS effectively once?
- How long would you keep processed message IDs?
- What does Kafka's idempotent producer protect against?
Tips
- Tie each semantic to when the ack happens
- Say "exactly-once effect" rather than "exactly-once delivery"
- Show one dedup mechanism concretely
Evaluation rubric
- Correct definitions linked to ack timing
- Explains why exactly-once delivery is impossible in general
- Gives at least two methods for exactly-once effect
- Recognises limits of broker transactions for external effects