← Lessons

quiz vs the machine

Platinum1820

System Design

Exactly Once in Kafka

How idempotent producers and transactions give effectively once processing.

6 min read · advanced · beat Platinum to climb

The hard problem

Networks retry, so a producer may send the same record twice and a consumer may reprocess after a crash. Exactly once means each record affects the result a single time despite these failures.

Idempotent producer

Kafka gives each producer a producer id and a sequence number per partition. The broker drops duplicates with a sequence it has already seen, so a retried send does not write the record twice.

Transactions tie steps together

For a read process write pipeline, Kafka transactions let a consumer commit its input offsets and its output records atomically. Either both commit or neither does, so the output and the consumed position stay consistent.

Read committed isolation

Downstream consumers set isolation to read committed, so they only see records from transactions that fully committed. Aborted records stay hidden.

The honest caveat

This covers Kafka to Kafka flows. A side effect to an external system, like a payment call, still needs idempotency there, since Kafka cannot roll it back.

Key idea

Idempotent producers drop duplicate writes and transactions commit offsets and outputs atomically, giving effectively once processing for Kafka to Kafka pipelines.

Check yourself

Answer to earn rating on the learn ladder.

1. How does the idempotent producer avoid duplicate writes?

2. What do Kafka transactions make atomic?

3. What is a key limitation of exactly once?