Two-Phase Commit Sink
A two-phase commit sink writes a streaming job’s output inside a transaction that is prepared as records are processed and committed only when the corresponding checkpoint has succeeded. If the job fails before that point, the transaction is aborted and the replayed records write a fresh one โ so aborted output stays hidden from readers that honor the transaction boundary.
Flink distinguishes exactly-once effects on its managed state from effects at an external destination. Extending the guarantee requires replayable input and a sink that supports transactional or idempotent application. The connectorโs recovery protocol is part of that guarantee.
The two phases
- Prepare. As it processes, the sink writes to the destination in a way that is durable but not yet visible to readers โ an open transaction, a staging location, an uncommitted batch. Nothing is lost if the process dies here, and nothing is exposed either.
- Commit. Once the covering checkpoint completes, the sink commits the corresponding output. The checkpoint identifies the recoverable progress boundary; output visibility follows successful destination commit. These are separate actions, not one simultaneous visibility event.
Tying the commit to the checkpoint is the whole trick. The checkpoint records where the input had been read to; the commit makes the corresponding output visible. The pairing records which output recovery must complete; resolving an ambiguous destination response still needs the connectorโs recovery protocol.
It does not mean the two are one atomic action. A process can die after the checkpoint completes and before the destination commit returns, and that window is exactly why the pattern has a recovery half. The rule that makes it correct is which side the checkpoint is the record of truth for: a transaction belonging to a completed checkpoint must be committed on recovery, and one belonging to an incomplete checkpoint must be aborted.
- Failure before the checkpoint completes. The staged output is abandoned โ aborted, or left never made visible โ and the input is reprocessed from the previous position.
- Failure after checkpoint completion, before or during commit. Recovery must complete or recognize the completed transaction without duplicating its effects. This is a contract of the sinkโs recovery layer; a raw destination API need not return success for every repeated commit call.
Three requirements follow, and they fail in different ways rather than all degrading to at-least-once.
- The transaction identifier must be in the checkpoint. A restarted process has to name the transaction it is resuming. Without it, recovery cannot find the pending work at all, and whether that ends in a duplicate or a gap is luck.
- The destination must keep the transaction alive across the restart. Every system has a limit, and this is the requirement whose failure is loss, not duplication: the completed checkpoint already advanced the input position, so if the transaction expires or is aborted by the destination, restarting from that checkpoint reprocesses nothing and the output for those records never exists. Reprocessing is not automatic here โ it has to be arranged.
- An unresolvable pending transaction must stop the job. Proceeding past one is how the same output is committed twice, or silently skipped.
A transaction that expires or is aborted during an outage deserves a plan rather than a retry, because the two sides have to be moved back together: restart from an earlier checkpoint or savepoint whose input position precedes the lost output, and reconcile the destination for that range first โ remove whatever the abandoned transaction may have left visible, or make the re-run replace the range rather than add to it. Choosing an outage window longer than the destination’s transaction timeout is therefore a decision to accept manual recovery, which is worth knowing before the outage rather than during it.
A lost commit response leaves the outcome unknown. Resolve it through the connectorโs transaction identity and durable completion evidence. For example, PostgreSQL COMMIT PREPARED operates on an existing prepared transaction; an identifier already removed after completion can produce a missing-transaction error on repetition. That error alone is not proof of success, because the transaction could also have been aborted or its state lost. If the recovery contract cannot establish the outcome, stop and reconcile rather than guess.
One consequence worth naming for anything with a visible side effect: the pattern bounds duplicates in the destination store, not in the world. A notification, a payment, or an outbound call is not covered by the transaction, and the last section returns to how to protect it.
Transactional or idempotent: choosing
| Transactional sink | Idempotent sink | |
|---|---|---|
| How a repeat is made harmless | Staged output stays hidden until commit; recovery recognizes or completes the same transaction without repeating its effects | The destination recognizes the same logical operation and applies it once โ for a keyed row, an upsert under a version guard that ignores an older or equal revision, so a replayed older version does not overwrite newer state |
| Needs from the destination | Transactions, or an atomic way to make staged output visible | A stable key; an atomic apply of the operation against that key; an ordering or version guard wherever replay can deliver an older version; a rule for equal versions with different content; and the key or version kept for as long as replay can occur |
| Cost | Output appears only at checkpoint boundaries, so latency follows the checkpoint interval | Simpler than a transaction, not free: every write carries the key and version, the destination has to honor the guard, and readers see each applied write rather than checkpoint-aligned batches, so intermediate states are visible |
| Fails when | The destination has no transaction and no atomic swap | The destination cannot recognize a repeat โ no stable key to write against, a receiver that treats every request as new, or a keyed overwrite with no version check, which lets a replayed older version win |
Where the destination is a table format or an object store, the atomic-visibility half of this is the same mechanism as atomic publication: stage the files, then move one pointer. The streaming addition is only that the pointer moves when the checkpoint says so.
The reader has to cooperate
Readers must also honor transaction visibility. Kafkaโs read_committed mode excludes uncommitted and aborted transactional records, while the default read_uncommitted can return them. Both modes can return nontransactional records.
An open transaction can hold read_committed consumers behind the last stable offset in its partition. Visibility therefore depends on transaction completion or resolution as well as polling and processing delay; it is not a fixed promise equal to the nominal commit interval.
What it cannot cover
External actions need their own recovery contract. An email cannot be unsent, and a payment retry without a recognized operation identity can charge again. A lost API response can leave the caller unable to determine whether the action happened.
Protect the external effect with a retry-safe receiver contract. A stable operation key recognized by the receiver, such as the transaction identifier or a business key rather than a per-attempt value, is one mechanism; an operation with a naturally idempotent intended effect can also be safe. Verify the identity scope, concurrency behavior, and retention horizon rather than assuming that a request field alone supplies the guarantee. Deferring the effect until after the commit changes when it fires and how often it fires spuriously, but a retried commit re-executes it, so it is a reduction in exposure rather than a fix. And accepting occasional duplication is a decision to detect and correct it, which is why a periodic reconciliation stays part of the design rather than being an admission that it failed.
So the question to ask of a side effect is not whether it can wait for the commit, but whether the thing receiving it can tell that it has seen this one before.
How sinks fit with sources, state, and the rest of end-to-end correctness is worked through in Stream Processing Semantics; the state side is covered in stateful stream processing.
References
- Apache Flink Documentation, Learn Flink: Fault Tolerance via State Snapshots
- Apache Kafka Documentation, Consumer Configs (isolation.level)
- Apache Flink, End-to-End Exactly-Once Processing
- PostgreSQL 18, COMMIT PREPARED
- PostgreSQL REL_18_STABLE, twophase.c
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
