guide
guide8 min read

Flink exactly-once Kafka duplicates read_committed: why they happen and the settings that fix them

The sink, transaction timeout and consumer isolation settings behind duplicates in a Flink job that claims exactly-once

Flink exactly-once to Kafka still produces duplicates when any link in the chain is not transactional end to end: the sink is not set to exactly-once, the transaction timeout expires before a checkpoint commits, or a downstream consumer reads with Kafka's default read_uncommitted isolation. Per the Apache Flink Kafka connector documentation, the Kafka sink gives exactly-once by writing inside Kafka transactions that are committed when a checkpoint completes. A reader that does not honor transactions sees data the guarantee does not cover.

Key Takeaways

  • •Exactly-once on KafkaSink needs three things together: checkpointing enabled, DeliveryGuarantee.EXACTLY_ONCE set explicitly, and a unique transactional ID prefix per job.
  • •Kafka consumers default to isolation.level=read_uncommitted (see the Kafka consumer configs). Every downstream reader of an exactly-once topic must set read_committed.
  • •The transaction timeout has to outlast the longest checkpoint plus recovery time, and stay within the broker's transaction.max.timeout.ms. Set both explicitly instead of relying on defaults.
  • •Duplicates already present in the source topic pass straight through Flink. Exactly-once covers the Flink-to-Kafka hop, not your upstream producers.
  • •Verify by consuming the same topic under both isolation levels and comparing counts per business key.

Flink's Kafka sink opens a Kafka transaction, writes records into it, and commits it only after the checkpoint that covers those records completes. Kafka's transactional protocol is described in KIP-98. Records written inside an open or aborted transaction are still physically in the log. Only a consumer using read_committed filters them out.

That gives five common failure points. We list them in the order we check them:

  • •The sink is not exactly-once. The builder's delivery guarantee is a setting, and anything other than EXACTLY_ONCE means at-least-once behavior on recovery.
  • •The consumer is read_uncommitted. It reads records from transactions that later abort, and it reads records before the commit. After a failure and replay, it sees both the aborted attempt and the retry.
  • •The transaction timeout is too short. If the transaction expires before the checkpoint commits, Kafka aborts it. The data from that window is lost or rewritten, depending on your recovery path.
  • •Two jobs share a transactional ID prefix. They can fence each other's producers and abort each other's transactions.
  • •The input already has duplicates. Flink preserves what it reads.

Step 1: Turn on checkpointing and set the delivery guarantee

The Flink docs state that exactly-once delivery from the Kafka sink requires checkpointing to be enabled. Without it, transactions are never committed on a checkpoint boundary, so the guarantee has nothing to attach to.

In the job setup, enable checkpointing in exactly-once mode, for example env.enableCheckpointing(60000, CheckpointingMode.EXACTLY_ONCE);. Then set the guarantee on the sink builder with .setDeliveryGuarantee(DeliveryGuarantee.EXACTLY_ONCE). We recommend setting it explicitly even where you believe it is the default, so a code review can see the intent.

Step 2: Give every job a unique transactional ID prefix

With exactly-once, the sink needs .setTransactionalIdPrefix("orders-enricher-v1"). The connector derives its transactional IDs from this prefix. The connector docs cover the exactly-once requirements, and we read the rule as: one prefix per job, never shared. If two jobs write to the same cluster with the same prefix, Kafka's producer fencing can abort one job's open transactions. Include the job name and a version in the prefix. Bump the version only when you deliberately start a job from scratch.

Step 3: Size transaction.timeout.ms against checkpoints and the broker limit

Kafka documents transaction.timeout.ms as a producer setting and transaction.max.timeout.ms as a broker setting. In the Kafka documentation, the broker maximum defaults to 900000 ms (15 minutes), and the producer default is 60000 ms (1 minute). A producer that requests a timeout above the broker maximum is rejected at initialization.

Our rule of thumb, which is our opinion rather than a documented formula: the timeout should exceed your worst-case checkpoint duration plus restart time, with headroom, and sit at or below the broker maximum. Set it on the sink: .setProperty("transaction.timeout.ms", "900000"). If your recovery can take longer than the broker maximum, raise transaction.max.timeout.ms on the brokers first. A transaction that Kafka times out cannot be committed after restart, so records from that checkpoint window are not delivered.

Step 4: Set isolation.level=read_committed on every consumer

This is the step most duplicate reports come down to. The Kafka consumer default is read_uncommitted, so a consumer reads every record in the log, including records from aborted transactions. Set isolation.level=read_committed on each reader of the topic. That includes other Flink jobs (.setProperty("isolation.level", "read_committed") on the KafkaSource builder), Kafka Connect sinks, Spark jobs and ad hoc scripts.

There is a latency cost. A read_committed consumer only sees records once the transaction commits, which happens when a checkpoint completes. End-to-end latency is therefore bounded below by your checkpoint interval. A 60-second interval means readers wait up to about a minute for fresh data. That is the price of the guarantee.

Step 5: Deduplicate or guard against duplicates already in the input

If the source topic contains duplicates because an upstream producer is not idempotent, Flink will faithfully write them again. Fix that at the producer with enable.idempotence=true, or deduplicate in the job on a business key with keyed state and a TTL. Exactly-once on the output does not remove duplicates that were already in the input.

For the warehouse side, check duplicates on the business key directly. For an orders feed landing in a table raw.order_events, run SELECT order_id, COUNT(*) AS n FROM raw.order_events GROUP BY order_id HAVING COUNT(*) > 1 ORDER BY n DESC LIMIT 50;. If duplicates share the same Kafka partition and offset, the loader replayed them. If they have different offsets, they were written twice into Kafka, which points back to Steps 1 to 4.

Step 6: Verify with a read_committed versus read_uncommitted count

Read the same topic twice and compare. With the Kafka CLI, run kafka-console-consumer.sh --bootstrap-server broker:9092 --topic orders-enriched --from-beginning --timeout-ms 30000 --isolation-level read_committed | wc -l, then repeat with --isolation-level read_uncommitted. A gap means aborted or open transactions exist in the log. That is normal after a failure and not a bug by itself. The bug is any consumer that sees the larger number.

For a per-key view, consume both ways into files and compare key counts with sort | uniq -c | awk '$1>1'. Duplicates that appear only under read_uncommitted confirm a consumer configuration problem. Duplicates under read_committed mean the sink guarantee, the timeout, or the input is at fault.

Where agents fit into this checklist

Everything above is a configuration audit across the job, the brokers and every consumer. That is the kind of cross-system check our Streaming Agent is built around: it covers Kafka, Pulsar and Kinesis topic governance, schema registry and exactly-once monitoring. Every Data Workers agent is an MCP server, so from Claude Code or Cursor you can ask a question such as "Is the orders-enriched topic safe to read with read_committed?" and have the agent inspect topic and consumer settings. The OSS edition is read-only by default, so it inspects rather than changes anything. Treat its output as a starting point and confirm with the commands in Step 6.

Duplicates also matter after the stream. Our Schema Agent maps blast radius when a key or column changes, and our Quality Agent is where uniqueness checks on business keys belong once the data lands. Both work on the same shared context as the Streaming Agent, so a duplicate spike on a topic and a failing uniqueness test on its table can be investigated together instead of in two tools.

Frequently Asked Questions

Why do I still see duplicates if my Flink job uses exactly-once? Most often a consumer is reading with Kafka's default read_uncommitted, so it sees records from aborted transactions and replays. Check consumer isolation.level first, then the sink's delivery guarantee, then the transaction timeout.

Does read_committed add latency? Yes. Records become visible only after the transaction commits, which happens when the covering checkpoint completes. Expect visibility delays on the order of your checkpoint interval, so choose that interval with downstream freshness in mind.

What happens if transaction.timeout.ms is too low? Kafka aborts the transaction when it expires. After a restart, Flink cannot commit it, so the records from that checkpoint window are not delivered. Size the timeout above worst-case checkpoint plus recovery time and within the broker's transaction.max.timeout.ms.

Can two Flink jobs share a transactional ID prefix? We advise against it. The prefix identifies a job's producers, and sharing it risks fencing and aborted transactions. Use a distinct prefix per job and per deliberate restart-from-scratch.

Does exactly-once to Kafka remove duplicates from my source topic? No. It guarantees the Flink-to-Kafka write. Duplicates already in the input are processed like any other record, so fix upstream idempotence or deduplicate on a business key in the job.