guide
guide7 min read

Exactly once semantics in Kafka explained: what Flink guarantees in your sink

What idempotence, transactions and checkpoints cover, and where duplicates still reach your sink

Exactly-once in Kafka means that a record produced inside a transaction is committed to its output topics once, even across retries and crashes. This is defined in KIP-98 and described in the Apache Kafka documentation. It does not mean your warehouse table, database or REST endpoint will never see a duplicate. The guarantee holds inside Kafka, and outside it you only get the guarantee if the sink cooperates.

Key Takeaways

  • •Kafka's exactly-once covers the read-process-write cycle between Kafka topics. It does not extend to arbitrary external systems.
  • •Idempotent producers stop duplicates from retries within one producer session. Transactions make a batch of writes and offset commits atomic.
  • •Flink's exactly-once with Kafka relies on checkpoints plus a two-phase commit sink. Consumers must set isolation.level=read_committed or they will read uncommitted and aborted data.
  • •A sink is safe only if it is transactional or idempotent. Plain append-only inserts into a warehouse can still produce duplicates after a restart.
  • •Most real duplicate incidents come from configuration gaps, such as transaction timeouts shorter than checkpoint recovery, and from consumers left on the default isolation level.

Exactly once semantics in Kafka explained: the three layers

People say "Kafka has exactly-once" as if it were one feature. It is three mechanisms stacked together, and each one covers a different failure.

  • •Idempotent producer: the broker tracks a producer ID and a sequence number per partition. If a retry resends a batch the broker already wrote, it discards the copy. This removes duplicates caused by network retries, but only within one producer session and one partition.
  • •Transactions: a producer can write to several partitions and commit consumer offsets as one atomic unit. Either all of it becomes visible or none of it does. This is what makes a consume-transform-produce loop safe.
  • •Read-committed consumers: transactional writes only look atomic to a consumer that sets isolation.level=read_committed. The default is read_uncommitted, so a consumer you did not configure will see records from transactions that later abort.

The Confluent engineering post on how Kafka does exactly-once walks through the same design. The consistent point in both sources is the boundary: the guarantee applies to Kafka reading and Kafka writing. Once a record leaves for another system, that system has to take part in the protocol.

Flink does not rely on Kafka transactions alone. It takes periodic checkpoints of operator state and source offsets. On failure, it rolls back to the last completed checkpoint and replays from there. Replay by itself would create duplicates in the output, so the sink has to join the checkpoint protocol.

The Flink Kafka connector documentation describes how KafkaSink handles this when you set the delivery guarantee to EXACTLY_ONCE. The sink opens a Kafka transaction, writes records into it, pre-commits when a checkpoint barrier arrives, and commits only after Flink confirms the checkpoint completed. If the job fails before that confirmation, the transaction is aborted or resumed on recovery, so the records are not exposed twice.

This is a two-phase commit. It works well when the sink supports transactions. It is also where most surprises come from, because it adds timing constraints that Kafka and Flink settings must respect together.

It depends on what the sink is. This is the practitioner question we see most often, and the honest answer is a table of cases rather than a yes or no.

Sink typeDuplicates after failure?Why
Kafka topic (transactional, read_committed consumers)NoTwo-phase commit through Kafka transactions
Kafka topic, consumers on read_uncommittedReaders can see aborted or in-flight recordsThe consumer ignores transaction markers
Database with upsert on a primary keyNo, the final state is the sameThe write is idempotent, so replays overwrite identical rows
Database with plain INSERT and no keyYesA replay after restore writes the same rows again
Object storage or warehouse via file appendYes, unless commits are deduplicatedFiles written between checkpoints can be repeated after rollback
HTTP or webhook callYesThere is no transaction or idempotency key by default

The rule we use is simple. A sink is exactly-once only when its write is either atomic with the checkpoint or idempotent under replay. If neither is true, you have at-least-once delivery and a deduplication problem that someone has to own.

Where exactly-once breaks in practice

  • •Transaction timeout shorter than recovery time: if the producer's transaction.timeout.ms is lower than the checkpoint interval plus restart time, the broker can abort a transaction Flink still intends to commit. The Flink docs call out that this setting must be sized with the checkpoint interval in mind, and the broker caps it with transaction.max.timeout.ms.
  • •Consumers left on the default isolation level: downstream jobs read uncommitted records and treat aborted data as real. This is a one-line config fix that is easy to miss.
  • •Reusing transactional ID prefixes across jobs: two jobs sharing a prefix can fence each other out. Give each job a unique prefix.
  • •Long checkpoint intervals: committed data only becomes visible at checkpoint completion, so a 10-minute interval means 10 minutes of end-to-end latency for read_committed readers.
  • •Stateless restarts: restoring from no checkpoint or an old savepoint replays data that downstream systems already consumed. The guarantee assumes you recover from the latest successful checkpoint.

None of these show up as errors. The job reports healthy, and the symptom is a row count that is a little too high, or a metric that doesn't match the source. That is why we treat sink duplicates as a data quality problem and not only a streaming configuration problem.

How to verify your sink is actually exactly-once

  • •Check the consumer first: confirm isolation.level=read_committed on every consumer of a transactional topic.
  • •Compare counts end to end: reconcile source offsets consumed against rows landed in the sink for a fixed window.
  • •Kill the job on purpose: restart a staging job mid-checkpoint and look for repeated keys in the output.
  • •Add a natural key: if the sink table has no unique key, add one so duplicates become detectable and upserts become possible.
  • •Alert on duplicate rate: track distinct keys against total rows and alert when they diverge.

Our Streaming Agent covers the Kafka side of this checklist. It governs topics and the schema registry, and it monitors exactly-once configuration. Because it chains with the quality and observability agents, a duplicate-rate drop in a landing table can be traced back to the topic and consumer settings behind it. You can ask it questions like "Why did row counts jump 8% after last night's restart?" from Claude Code or Cursor, because every agent is an MCP server.

We covered how Data Workers keeps schemas and contracts consistent between producers and consumers in a separate post. Schema drift and duplicate delivery often show up together in the same incident, so the two checks belong in the same runbook. Our Catalog Agent) also helps here by recording which tables are fed by transactional topics and which are fed by at-least-once paths.

A decision rule for choosing a delivery guarantee

Exactly-once is not free. Transactions add latency and operational constraints. Our opinion is that you should use it where a duplicate causes real harm, such as financial totals or billing events, and accept at-least-once with idempotent writes elsewhere. For most analytics sinks, an upsert on a stable key gives the same final result with far fewer moving parts than a transactional commit.

Frequently Asked Questions

Does Kafka guarantee exactly-once delivery to every consumer? No. Kafka guarantees that transactional writes are committed atomically and that idempotent producers do not duplicate on retry. Consumers only see that clean view if they use isolation.level=read_committed, and the guarantee ends at the Kafka boundary.

Does Flink exactly-once with Kafka guarantee no duplicates in my sink? Only if the sink is transactional or idempotent. With KafkaSink set to EXACTLY_ONCE and read_committed consumers, yes. With a plain-insert database or file sink, a restart can write the same records again.

What is the difference between an idempotent producer and a transactional producer? An idempotent producer prevents duplicates from retries on a single partition within one session. A transactional producer extends that across partitions and sessions, and lets you commit consumer offsets atomically with the writes.

Why do I still see duplicates with exactly-once enabled? The usual causes are consumers on the default read_uncommitted level, transaction timeouts shorter than checkpoint and recovery time, a non-transactional sink downstream, or a restore from an old savepoint.

Is exactly-once worth the cost? For billing, ledgers and counters, usually yes. For analytics tables where an upsert on a natural key produces the same end state, idempotent writes are simpler and cheaper to operate.