0

"We Can Just Replay It": The Most Expensive Sentence in Data Engineering

A story about the six hours PostgreSQL was down, and the three offsets that could not agree on where we were.


02:14, Saturday.

The alert was not dramatic. primary down — connection refused. A routine maintenance window had turned into an incident, and our PostgreSQL primary was not coming back.

In the incident channel, Marcus — the architect who had insisted on Kafka a year earlier — typed the sentence that made everyone relax:

"Don't panic. Every change is in Kafka. When PG is back, we just replay it."

Six hours later, PostgreSQL came back. We never replayed that day.

Not because Kafka was broken. Kafka was fine. Every layer was fine. The problem was that the sentence Marcus typed was not a plan. It was a wish with a topic name attached to it.

This is the story of the most expensive sentence in data engineering — and the three positions that quietly disagree every time someone says it.


1. What did "we can replay it" actually promise?

Let me be fair to Marcus. He was not careless. We had done the respectable things:

  • acks=all on the producer
  • Seven-day retention on the topic
  • Idempotent production enabled
  • A consumer group with a committed offset we could inspect on any dashboard

Our architecture diagram had a Kafka logo in the middle. It looked like the shape of a system that could travel back in time.

But "we can replay it" is not one promise. It is four, and each one can fail on its own:

  1. The event actually made it into Kafka. Not "the producer sent it" — made it in, durably, in a replica that could be elected leader.
  2. The event is still there. Retention has not aged it out, and compaction has not collapsed it into a newer image of the same key.
  3. We can name the exact position to resume from — in a system where the source, the log, and the target each keep a different number.
  4. Replaying that position actually reconstructs the target, without skipping a write or applying the same business event twice.

Marcus had verified none of these. He had verified that a Kafka cluster existed.

Owning a log is not owning the past. A log is a shrinking window, and "replay" is a leased capability — you keep paying for it every day, or the landlord takes it back at 02:14.

I gave this failure mode a name: the Replay Mirage.

The Replay Mirage is the belief that adding a log buys you the ability to go back. What it actually buys you is a deadline and three offsets — and the deadline is always closer than the diagram suggests.

The Replay Mirage — "we can replay it" is four promises, each of which can fail alone


2. The six hours, reconstructed

We rebuilt the incident from four sources: the Kafka broker logs, the consumer group's committed offsets, the MySQL binlog, and our application's own event ledger.

02:14  PostgreSQL primary goes down. Sink writes start failing.
02:15  Consumer keeps reading Kafka. Offsets advance.
       Business writes to PG fail. ← nobody is watching this gap
02:31  Backpressure propagates. The CDC publisher slows.
03:47  MySQL binlog head advances on unrelated tables.
02:14→08:20  Topic hit its per-partition size cap before
              its 7-day time limit. Old segments deleted early.
08:20  PostgreSQL restored.
08:26  "Start replay from yesterday's offset."
08:27  Offset already below the earliest retained position.
       With auto.offset.reset=latest, the group jumped to the tail.
       Replay produced... nothing. And reported success.

Six hours, four diverging signals — Kafka offsets keep moving while the target is down

Two things in that timeline are worth staring at.

First, the consumer kept committing offsets while the target was down. From the dashboard, the group looked healthy — offsets climbing, lag near zero. It was consuming a database that did not exist. A committed offset is a statement about the consumer, not a receipt from the target.

Second, the "seven-day retention" was a half-truth. The topic had both a time limit and a per-partition size cap. During a busy period, the cap was reached in days, not a week. Retention is whichever boundary you hit first, and we had only monitored one of them.

Kafka did not lose our data. We had already agreed, in a config we never re-read, that some of it was allowed to disappear.


3. The three offsets that cannot agree

Here is the part that no architecture diagram draws. In a layered CDC pipeline, "where are we?" is not one question. It is three, and the answers are allowed to disagree:

Position Lives in What it actually means
Source read position MySQL binlog file + offset / GTID How far the publisher has read
Consumer position Kafka partition + offset How far the consumer has advanced its pointer
Business progress Rows in the target, plus a dedup ledger What the target has actually made durable

The incident was a disagreement between the second and the third.

Our consumer had a code path that committed the Kafka offset before the PostgreSQL write completed. In normal operation, nobody noticed — the write followed milliseconds later. But when the target was down for six hours, that ordering turned into a six-hour hole with a healthy-looking offset curve drawn over it.

The reverse ordering has its own failure: write to PostgreSQL first, then commit the offset. Crash between the two, and the event is read again. That is safe only if replay is harmless — which brings us back to the four promises.

There is no configuration that makes these three numbers identical. There is only a discipline: advance the business position and the consumer position in the same transaction, or accept that you will sometimes reconcile them by hand.

Concretely, that means writing the business change and an event-identity ledger row into the same PostgreSQL transaction — and only then committing the Kafka offset. The ledger key has to be stable and specific: cluster, topic instance, partition, and offset, with a stated retention period. Otherwise a replay three weeks later cannot tell "I have seen this" from "this looks similar."

And one more trap: a fast replay can deliver offset 42 after offset 43. If the target's dedup is a simple upsert, an older image can overwrite a newer state — the order was SHIPPED, and the replay drags it back to PAID. Offset-based dedup protects you from duplicates. It does not protect you from stale data. That needs ordering guarantees or source-version protection on the rows themselves.

Kafka guarantees order inside one partition. It does not guarantee that your consumer applied that order, that your key stayed in that partition after a repartition, or that your target accepted the writes in sequence.


4. "All right, delete Kafka then"

At the retro, the pendulum swung hard. Someone — fairly — said: we had Kafka, we had retention, we had monitoring, and we still could not recover one afternoon. Why keep the layer at all?

Marcus pushed back. "If we delete Kafka, we delete the only thing that lets audit and risk consume independently. Risk can't pause because PostgreSQL is in maintenance. A new consumer can't replay last week. You'd be fixing our outage by breaking their guarantee."

Both were right, which is why the argument went nowhere. They were arguing about a product. The real question was about who owns the recovery position.

In a direct pipeline — MySQL binlog → CDC task → PostgreSQL — recovery rests on two things: the source position the task saved, and binlog that has not yet been recycled. If the target slows, backpressure reaches the reader; if the pipeline cannot catch up, old log entries can be gone before the restore completes. You cannot respond by restarting from "latest" — that silently skips every change still waiting to land. You need a rebuild path you have actually rehearsed: snapshot into a shadow table, reconcile, cut over.

In a layered pipeline — binlog → publisher → Kafka → consumer → PostgreSQL — ingestion and loading can move at different speeds. While PostgreSQL is down, other consumer groups can keep reading, as long as Kafka has capacity, the publisher is healthy, and the events are still inside the retention window. The price is that you now own three positions instead of two, and every one of them needs a monitoring signal and an owner.

This is not a choice between Kafka and SeaTunnel, either. Three words get used as if they were interchangeable, and they are not:

  • Kafka is an event log.
  • Kafka Connect is a framework for connecting Kafka to external systems.
  • SeaTunnel is a data integration tool — it can run as a direct task, or participate in a topology that includes Kafka.

A debate between "Kafka" and "SeaTunnel" is a category error. The debate is between two ways of assigning responsibility for the gap.

Same outage, different recovery positions — the direct pipeline has two, the layered one has three


5. Two architecture questions, one table

Forget throughput for a moment. Ask who needs the data and how far back they need to go.

Question one: we have one PostgreSQL read replica. The business only needs the final state of each order — no per-change audit. The on-call team can resolve a failure inside the binlog retention window, and we rehearse restore and reconciliation quarterly.

I would start direct. Acceptance is not "the task turned green again." It is: every INSERT, UPDATE, and DELETE created during the failure is reflected in the replica, and a deleted order did not come back to life because an old event was replayed.

Question two: audit, risk, and the query replica all drink from this stream. Risk cannot stop because PostgreSQL is under maintenance. Audit needs every state transition, not the last row. A new consumer needs to replay last week independently.

Start with the event log. Choose a stable partition key for an order and route every one of that order's events to the same partition — and understand that the ordering guarantee exists only if the key really lands in one partition and the consumer applies it serially. Repartitioning or changing the partitioner is a migration: the same key's old and new events can end up in different partitions. Kafka orders a partition, not your business.

And if you turned on log compaction to save space, stop calling the topic an audit trail. Compaction keeps the latest value per key; it is designed to forget. If you need the whole history, state the retention policy explicitly and decide where compliance history goes once it leaves the online window. "Compact" and "auditable" are opposite choices wearing compatible names.

The layer does not decide whether you can replay. Your retention policy, your partition key, and your commit order do. Kafka just holds the receipt.


6. The four things "replayable" has to mean before you say it

Put these into the acceptance criteria, not the architecture diagram.

  • Recovery window — measured, not configured. Test with the oldest position that is actually still readable. A topic retention policy has both a time limit and a per-partition size cap; whichever is reached first wins. Check the source's binlog window too. For an established consumer group where silent data loss is unacceptable, fail loudly on a missing offset (auto.offset.reset=none) instead of jumping to latest. Give brand-new groups a documented, deliberate starting point.
  • Catch-up ability. If the stream keeps producing at peak rate during recovery, can the restore write faster than new events arrive? If not, a long retention window only postpones the moment the delay becomes visible. Watch backlog and the age of the oldest unapplied event per partition.
  • Replay result. Interrupt once after the target write but before the progress commit, and once after the progress advances but before the write. Observe duplicates and gaps. Check the final row, the expected deletes, and side effects on external systems — not just the message count.
  • Ownership boundary. Decide who may reset a consumer offset, who reviews sensitive-data access during historical replay, and who declares a rebuild when the window is gone. No dashboard answers these.

And hold onto one uncomfortable fact: acks=all waits for the current in-sync replicas, not for every assigned replica. min.insync.replicas is a threshold you must verify. New clusters enable Eligible Leader Replicas by default, and under strict min-ISR rules the high watermark will not advance below the threshold — but ELR is not unclean leader election, which elects an out-of-sync replica and really can lose data. Producer idempotence suppresses the producer's own retry duplicates and preserves that producer's per-partition order; it does not undo a CDC publisher that restarts, re-reads the binlog, and re-emits a business event. Kafka transactions and read_committed do not commit your PostgreSQL transaction.

Test publisher failover and target write failure as two separate drills. A clean replay of one does not cover the other.


7. Why this hurts more in Southeast Asia

In our region, "just replay it" is not only an engineering claim. It is a data-governance claim.

Across Southeast Asia, data-protection regimes are tightening — Indonesia's PDP Law, Singapore's PDPA, Thailand's PDPA, Vietnam's newer personal-data rules. Replaying a week of order events means re-reading a week of customer data, and the person doing the replay may not be the person who was authorised to see it at the time it was written.

When an auditor asks how a disputed order reached its current state, "we can replay it from Kafka" is not an answer either. If the topic is compacted, the intermediate states are already gone. If the consumer committed offsets while the target was down, the trail has a hole in it. If the event identity ledger cannot reconstruct a stable key, you cannot prove which version you are looking at.

Duplicated audit entries and silent replay gaps are not cosmetic defects here. They come with remediation dates.

A replay is a read of history. In this region, history is personal data — and someone has to be allowed to read it.


Epilogue: the sentence we retired

We did not delete Kafka. We also did not keep the sentence.

The new rule in our incident channel is that nobody says "we can just replay it" without naming three numbers: the oldest position still readable, the position we would resume from, and the target-side evidence that the row at that position is already there. If you cannot name all three, you say "recovery unverified" instead.

Marcus still maintains the topic. He also added a dashboard nobody asked for: the age of the oldest unapplied event per partition, next to the consumer offset curve. The two lines used to move together. During our six hours, they separated — and the offset curve kept smiling all the way to the tail.

So the next time someone tells you not to worry because you can replay it, don't look at the Kafka logo on the diagram. Don't look at the retention setting. Don't look at the green offset graph.

Ask one question:

"If the target went dark right now, which exact position would we resume from — and what proves the row at that position is already in the table?"

If the room goes quiet, you don't have replay.

You have a deadline, three offsets, and a sentence that sounds like a plan.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí