Explain the process and advantages of using Kafka's exactly-once processing feature.

Instruction: Describe how exactly-once processing is achieved in Kafka and its benefits over at-least-once and at-most-once processing semantics.

Context: This question is designed to delve into the candidate's knowledge of Kafka's transactional capabilities, emphasizing the significance of exactly-once processing in ensuring data integrity.

Updated

Official answer

Kafka exactly-once processing is a scoped guarantee about committed results, not a promise that application code executes once or that every external side effect happens once.

For a Kafka consume-transform-produce pipeline, disable ordinary automatic input-offset commits, initialize a transactional producer with a stable logical identity, begin a transaction, write the output, and send the next input offsets with the consumer group's metadata into that transaction. Commit outputs and progress together; abort and recover according to the producer API on failure. Use distinct transactional identities for simultaneously active logical producers, and stop a producer after fatal fencing errors. Downstream consumers need read_committed to hide aborted output.

Kafka Streams can coordinate its Kafka outputs, state-store changelogs and consumed progress with processing.guarantee set to exactly_once_v2. State can be restored and application logic rerun after failure. Producer idempotence alone suppresses qualifying producer retries; it does not deduplicate every resubmitted business event or atomically commit consumer progress.

At-least-once processing can replay work; at-most-once processing can lose work if progress is recorded before the effect. Transactions reduce duplicate committed effects within the supported Kafka boundary, but add coordination and visibility costs. An email, payment API or unrelated database transaction still needs an idempotency key, an outbox/inbox protocol or another explicit consistency mechanism. Test a crash before and after commit rather than assuming lower latency or universal zero loss.

Producer transaction API, Streams processing guarantee

Related Questions