guide
guide6 min read

Flink exactly once duplicates downstream: why they happen and how to catch them

Why Flink's exactly-once guarantee stops at the sink, and how to catch the duplicates that get through

Duplicates appear downstream of a Flink job because Flink's exactly-once guarantee applies to its own checkpointed state, and end-to-end exactly-once also needs a sink that commits atomically with checkpoints or accepts idempotent writes. The Apache Flink documentation on fault tolerance guarantees says this directly: the source must be replayable, and the sink must be transactional or idempotent. If either condition is missing, the job can be correct internally and still deliver the same record twice.

Key Takeaways

  • •Flink's exactly-once mode guarantees that each record affects Flink state once. It does not guarantee that an external system receives each record once.
  • •End-to-end exactly-once needs a replayable source plus a sink that either commits transactionally with checkpoints (two-phase commit) or writes idempotently.
  • •A Kafka consumer that reads a transactional Flink output without isolation.level=read_committed can see records from aborted or uncommitted transactions, which looks like duplicates.
  • •Duplicates in the source topic are not removed by Flink. Flink only guarantees it will not add its own.
  • •Duplicates are cheap to detect if you count them: track key-level duplicate rates, offset gaps and checkpoint health on the output, not just job status.

The phrase "exactly-once" is overloaded. Inside Flink it means that after a failure and restore, operator state reflects each input record exactly once. That works because checkpoints snapshot state and source offsets together, and on recovery Flink rewinds the source to the checkpointed offsets and replays from there.

Replay is the cause of the trouble. Records processed after the last completed checkpoint get processed again after a restore. State is rolled back, so it doesn't double count. Anything already written to an external system is not rolled back unless the sink took part in the checkpoint protocol. The Flink project describes the mechanism in its overview of end-to-end exactly-once processing with Kafka: the sink pre-commits its output when a checkpoint barrier arrives, and commits it only after the checkpoint completes across the whole job.

A sink that skips that protocol gets at-least-once behavior with a different name. The five causes below are the ones we see most often, and each one shows up differently in the data.

CauseWhat you see downstreamFirst check
Sink is neither transactional nor idempotentA burst of repeated rows right after a restart or failoverDoes the sink use two-phase commit, or write by a deterministic key?
Consumer reads uncommitted dataDuplicates or phantom records that disappear laterIs isolation.level set to read_committed on every downstream Kafka consumer?
Transaction expired before commitData loss or duplicates around long checkpoints and restartsCompare the producer transaction timeout with the broker maximum
Replay without stateLarge duplicate blocks after a redeployWas the job restarted from a checkpoint or savepoint, or from scratch?
Duplicates already in the sourceDuplicates with no relation to restartsDo duplicate keys exist in the input topic?

1. The sink is not transactional or idempotent

The most common cause is a sink that does a plain insert or append. After a restore, Flink replays records since the last checkpoint and the sink writes them a second time. Two fixes work. Use a sink that implements two-phase commit, so output stays invisible until the checkpoint completes. Or make the write idempotent, for example an upsert keyed on a deterministic primary key, so a replayed record overwrites itself.

Idempotent writes only work if the key is stable across replays. A key built from processing time or a random UUID generated inside the job changes on every replay, so the upsert inserts a new row each time.

2. Downstream consumers read uncommitted records

With the Kafka sink in exactly-once mode, Flink writes inside Kafka transactions and commits them when a checkpoint completes. Kafka consumers default to reading uncommitted records. The Flink Kafka connector documentation notes that consumers of this output need isolation.level=read_committed. Without it they can read records from transactions that were later aborted, and the replayed copies arrive again after recovery.

This is the cause we would check first, because it is a one-line config and it is easy to miss on a consumer that someone else owns.

3. The transaction timeout expires before the commit

Kafka transactions have a timeout, and the broker enforces a maximum. If a checkpoint takes longer than the transaction timeout, or the job is down longer than it, the broker aborts the open transaction. The Flink docs call out that the connector's default producer transaction timeout can exceed the broker's configured maximum, so the two must be reconciled. When they aren't, a pending commit fails on recovery and the job either errors or loses the data it was about to commit. Teams then often re-run from an earlier point to "fix" the gap, which creates duplicates.

4. The job restarted without its state

Exactly-once relies on restoring from a checkpoint or savepoint. A stateless redeploy, a new job submission that ignores the old savepoint, or a changed operator UID that prevents state mapping all start the source from configured offsets rather than checkpointed ones. Everything between those offsets and the real position is processed again. Flink behaves correctly here. The deployment process is what breaks the guarantee, and it is invisible in Flink's own metrics.

5. The duplicates were in the source all along

Flink does not deduplicate its input. If an upstream producer retried without idempotence enabled, or an upstream system emitted the same event twice, Flink processes both. The Kafka documentation describes idempotent and transactional producers, which address this at the producer, but the setting lives on a team and a service that may not be yours. Before debugging the Flink job, count duplicate keys in the input topic over the same window.

How to catch duplicates before a consumer does

Job status won't tell you any of this. A job can show healthy checkpoints and a flat error rate while a downstream table holds repeated rows. Detection has to look at the output. We use four signals.

  • •Key-level duplicate rate: for each output topic or table, the share of business keys that appear more than once in a rolling window. A baseline near zero makes any spike obvious.
  • •Offset and sequence gaps: per-partition checks that offsets advance without repeats or holes, which separates replay from loss.
  • •Checkpoint and transaction health: failed or slow checkpoints, aborted transactions and restore events, lined up on a timeline with the duplicate spikes.
  • •Consumer configuration: whether each downstream reader of a transactional topic uses read_committed.

The correlation matters more than any single signal. A duplicate spike that starts within one checkpoint interval of a restore points to causes 1 or 4. A spike with no restore points to cause 5. Phantom records that vanish point to cause 2.

Where our Streaming Agent fits

Our Streaming Agent covers Kafka, Pulsar and Kinesis topic governance, schema registry checks and exactly-once monitoring. It runs the signals above against the topics and tables a job writes to, and it is an MCP server, so you can ask the question from Claude Code or Cursor instead of opening a dashboard. A prompt like "Why did row counts jump 12% after last night's restart?" starts from the output side: duplicate keys, offset repeats and the restore event on the same timeline. The MCP specification is the open protocol that makes this work in any compatible client.

The agent is read-only in the open-source edition. It tells you which of the five causes matches what it sees. Changing sink configuration or consumer settings is a write, and in Pro that goes through approval workflows with an audit trail.

What changes when other agents join in

Duplicates rarely stay in the stream. They flow into the warehouse, inflate counts and trip quality thresholds. Our Quality Agent sees the metric drop below SLA, and the Streaming Agent sees the restore that caused it. When both agents share context, you get one explanation instead of two alerts that someone has to connect by hand. Our Schema Agent adds blast radius: which downstream tables and consumers read the affected topic. We covered how the agents share that context in a separate post on our multi-agent architecture.

If the duplicates came from a schema or contract change upstream, the Incidents Agent can attach that change to the same timeline. We think this is the practical gap in most stacks. Detection exists. The hours go to working out which of these causes applies, and that is a job for something that can see the whole path.

  • •Confirm checkpointing is enabled in exactly-once mode, not at-least-once.
  • •Confirm the sink is transactional, or that its writes are keyed by a stable business key.
  • •Set read_committed on every downstream Kafka consumer, including ones other teams own.
  • •Keep the producer transaction timeout under the broker maximum and above your worst-case checkpoint plus restart time.
  • •Deploy only from a checkpoint or savepoint, and give operators stable UIDs.
  • •Monitor duplicate-key rate on the output, not just job health.

Frequently Asked Questions

Does Flink exactly-once guarantee no duplicates in my warehouse? No. It guarantees exactly-once effects on Flink's own state. The warehouse only sees each record once if the sink commits with checkpoints or writes idempotently, and if the source had no duplicates to begin with.

Why do my Kafka consumers see duplicates when the Flink Kafka sink is set to exactly-once? The usual reason is that the consumer reads with the default isolation level, which includes uncommitted and aborted transactional records. Setting isolation.level=read_committed makes the consumer see only committed output.

Is an idempotent upsert as good as two-phase commit? For most analytical tables, yes, provided the key is deterministic across replays. Upserts also make replays visible only as brief re-writes of the same row. Two-phase commit hides in-flight output entirely, but it adds latency equal to the checkpoint interval and depends on transaction timeouts.

How do I find out whether duplicates came from Flink or from upstream? Count duplicate keys in the input topic for the same time window. If duplicates exist there, Flink passed them through. If the input is clean and duplicates cluster right after restore events, the sink or the deployment process is the cause.

Can the Streaming Agent fix duplicates automatically? In the open-source edition it is read-only, so it diagnoses and points to the cause. In Pro, writes such as configuration changes go through approval workflows and are recorded in an audit trail.