Imagine an overnight incident: the consumer is rewound to a point before the failure. As messages replay, the write counter climbs. Then a support ticket arrives: an order that had already shipped is back to "paid."

Nobody changed that order. You simply replayed the last few hours of messages.

The counterintuitive part is this: the replay completed successfully, every step succeeded, not a single write was missing. It was written back "successfully." The problem is not in reading. It is in the instant of writing back.

1. "Can reread" and "can recover" are two promises

What gets said first in an incident is often something reassuring: the messages are all in the log, just replay them.

That sentence is only half right. What it promises is "we can read those messages again," while recovery needs "after writing them back, the state at the target is correct." The former is a capability of the log; the latter is a property of how you write. However complete the log, it cannot answer the latter for you.

Separate the two and the problem becomes clear:

  • Rereading asks: is that message still there? Can I get it once more?
  • Recovery asks: after writing it into the target table again, is this a harmless duplicate, or a regression that puts an old state back over a new one?

A well-built log system can do the first perfectly and fail completely at the second. The two depend on entirely different things: the first on retention policy and positions, the second on the way you write back.

2. Primary-key upsert prevents duplicates, not regressions

Faced with "will a replay write twice," most people reach for primary-key upsert first. Its effect is real: write the same row image twice, the database still has one row; reread an unchanged message, the state does not change. Duplicates, it handles.

But what it handles is "the same message arrived twice," not "an old message arrived late."

Imagine order 0042 going from "paid" to "shipped." During the failure, the downstream may have written only "paid." On recovery replay, if the message carrying "shipped" arrives and writes first, followed by an out-of-order old message "paid," an overwrite-style upsert writes both without distinction — the late old image covers the new state. That is exactly how an order regresses.

PostgreSQL's INSERT ... ON CONFLICT ... DO UPDATE (that is, upsert) guarantees that on a conflict it inserts or updates atomically, and does not produce two rows because of concurrency. It does not compare new against old. Whoever writes last wins. In a normal ordered stream, the later write really is the newer one, so the assumption holds; once a replay introduces out-of-order delivery, the assumption collapses.

A primary-key upsert prevents duplicate rows, not duplicate writes or stale state. Repeating the same row image can leave the same values while still executing an update or firing a trigger.

3. Three ways to write back, and what each one protects

Spread out "how to write back after a replay" and there are roughly three practical approaches. They differ not in performance, but in what they can stop.

Approach Prevents duplicates Prevents regression Prerequisites and cost
Overwrite-style upsert on primary key only Yes, for duplicate rows, not repeated updates No, an old image covers the new state Target has a unique key; generated SQL is enough, simplest
Business write plus event-identity ledger in one transaction, then commit the position Yes, the ledger remembers which events were seen No, a late out-of-order old event still overwrites Ledger key must be stable; business row and ledger row in the same transaction
Reject stale writes by source version Same-version replays do not update an existing row; not a general event ledger Yes, for existing rows with comparable non-null versions Upstream must carry a comparable source version; target needs a version column; custom SQL required

The second deserves a few more words, because it is often treated as the end of the road. Its idea is: write the business change and an event-identity record into the same transaction at the target, and only after the transaction commits, advance the consumer position. That closes the gap of "the position moved but the target never wrote" — if the target did not commit, the position does not move, the replay comes again, and the ledger keeps it from writing twice.

This genuinely solves a large class of problems. But it has a clear boundary: the ledger only answers "have I seen this event." A late out-of-order old event is a brand-new event identity, absent from the ledger, so it is admitted and written — and the new state is covered back to the old. Deduplication cannot stop a regression, because they are two different things.

The third is the only one that confronts a regression head-on: give each row a source version that can be compared for newness, and on write explicitly reject versions older than the one in the table.

4. Event identity and business version are not interchangeable

This is the crux of the whole matter, and the distinction most easily blurred.

Event identity Business version
What it answers Have I seen this? Is this newer than the row in the table?
Where it comes from cluster, topic, partition, offset monotonic per-key business version, or a source coordinate with a proven ordering for that key
What it is for deduplication judging newness, rejecting stale writes
What it cannot do judge newness: offsets are not comparable across partitions, still less after repartitioning deduplication: one business version may be carried by several events

The two are often treated as one, because under "normal order" they point the same way: a later message has a larger offset and a newer business state. Once replay, out-of-order delivery, or repartitioning enters, that correspondence breaks.

Using event identity to judge newness fails: an offset is ordered only within a single partition, cannot be compared across partitions, and after changing the partitioner or adding partitions, the new and old events of one business key may land in different partitions. Using business version to deduplicate also fails: several distinct events can share a version outside a full-row-image contract. Dropping them by version alone can lose changes that should have been processed.

Event identity tells you "whether it was processed"; business version tells you "whether it should overwrite." Use the former as the latter, and a replay will put a new state back into an old one.

5. What the line that stops a regression looks like

Rejecting stale writes by source version comes down, in SQL, to adding a condition to the overwrite. In PostgreSQL it looks like this:

INSERT INTO orders (id, status, src_version) VALUES (?, ?, ?) ON CONFLICT (id) DO UPDATE SET status = EXCLUDED.status, src_version = EXCLUDED.src_version WHERE EXCLUDED.src_version > orders.src_version 

The key is the final WHERE: only a newer image updates an existing row; an equal or older version does not update it. The PostgreSQL documentation is explicit — when the condition is not met, the row is not updated; it is locked, but not rewritten. That is exactly what we want: an old message arrives, and it creates no regression.

There are three prerequisites that must be stated, and without any one of them this path does not hold:

  • The upstream must carry a comparable, non-null version for each key. Prefer a monotonic business version when available. A binlog coordinate is usable only if it preserves ordering across the capture and failover topology. A MySQL GTID identifies a transaction; it is not a portable scalar ordering across sources or a unique row-event ID.
  • The target table must store that version. Use a unique key on id and a non-null, consistently ordered src_version. Equal versions must represent the same row image. The condition runs only on conflict; if the row is gone, an old insert can recreate it unless you retain a tombstone or another per-key version watermark.
  • That SQL must actually be executed. Taking Apache SeaTunnel's JDBC Sink as an example, the 3.1.0 documentation states that with a final key set and enable_upsert = true, what is generated is an ordinary INSERT ... ON CONFLICT ... DO UPDATE — overwrite-style, with no version-comparison condition. To add that WHERE, you need custom SQL — generate_sink_sql = false with a query — and the ? placeholders bind in upstream field order, so the position of the version column must line up with the placeholder. In custom-SQL mode, the save-mode machinery no longer runs.

These are facts on the configuration path, from static review of documentation and source, not the result of a test run end to end.

6. Three more boundaries to hold

First, custom SQL is not the same as end-to-end verification. The shape of the SQL above is standard, and PostgreSQL supports the condition; but in a concrete sync job, whether the source version reaches the upstream reliably, how the version column is maintained on deletes and updates, and whether out-of-order messages are all kept out by the condition — all of that counts only after a run with a real job. No numbers are given here, because there is no reproducible reconciliation result.

Second, deletes and side effects are outside this SQL's coverage. An upsert with a version condition governs "the state of a row." Whether DELETE messages need tombstones, counter increments, and triggers on external systems each have their own replay semantics, and you cannot expect one version column to settle them all. If the target table has other writers, the source of the version number has to be redesigned.

Third, direct and layered pipelines store the recovery position in different places. In a direct pipeline, recovery rests on the source position the job saved plus binlog that has not yet been cleaned up; in a layered pipeline, an extra Kafka layer lets ingestion and loading advance at different speeds, but you also gain one position to watch and one person responsible for it. Neither automatically makes writing back safe — that is a matter of how you write, and has nothing to do with whether there is a log in the middle.

Epilogue: the row that regressed was written back by you

Back to the order at the start. It reverted to "paid" not because the log lost a message, nor because the replay missed data. Quite the opposite: the replay delivered every message, and your way of writing faithfully landed every one of them — including that old message that should have been kept out.

So next time someone reports "can reread" as "can recover," do not ask whether the log is still there. Ask this: the old message that came back — on what grounds does it not cover the current new state?

If the answer is only "we used upsert," that order will regress once more. A unique key guarantees one row per key, not one write per event or the right version at the end.