How it works
A consumer does two things for each record: it does the work and it records its progress. A crash can land between them, and their order decides what breaks. Commit the offset first and you get at-most-once delivery: a crash after the commit skips the record, so it is lost but never repeated. That suits high-volume telemetry where a missing sample is harmless. Do the work first and you get at-least-once delivery: a crash before the commit repeats the record, but nothing is lost.
A consumer reads offset 500, “credit account 9 by $20”, writes the credit to Postgres, and crashes before committing. Its replacement resumes at offset 500 and credits another $20. Redelivery cannot be designed away, because after a crash or a lost acknowledgment the sender cannot tell whether the work finished. What you can control is the effect.
So “exactly once” in practice means at-least-once delivery plus an effect that lands once. An idempotent effect gives the same result when repeated: insert the event ID into a table with a unique constraint, in the same transaction as the credit, and the repeat conflicts and changes nothing. A transactional effect commits the result and the offset atomically, by keeping the offset in Postgres with the credit and resuming from it, or through Kafka transactions when the output and offsets both live in Kafka. Neither reaches a third-party email or card API, which needs the provider’s own idempotency support or an accepted risk. Deduplication tables also cost storage and must outlive the longest possible replay.
A consumer turns “order shipped” events into emails through a third-party API with no idempotency support. A teammate suggests enabling Kafka transactions to stop duplicates. What do you decide?
Kafka transactions cover records and offsets inside Kafka, not an HTTP call to the provider. For a shipping notice, a duplicate is milder than a missing email, so stay at-least-once. Record a sent marker per event ID to shrink the window, and accept that a crash between the API call and the marker can still send a second email.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Committing before the work risks loss; committing after it risks repeats.
- Exactly once describes the effect, built on at-least-once delivery.
- Kafka transactions stop at the edge of Kafka, so external calls need their own protection.