Concepts
A backup of Kafka copies every partition on its own. A restore of it brings each partition back to a different moment, and that can produce a cluster that never existed. This chapter explains why, what a consistent cut is, how Tidemark finds one every few seconds, and what a restore from it gives back.
Your use case
Every pattern on the stage, one at a time: what breaks without a backup, what breaks with the backup as it is, what Tidemark changes, and what stays your team’s job either way.
Why back up Kafka
Kafka already keeps several replicas of every partition. That protects against a lost broker, not against a lost cluster: a deleted topic, a broken upgrade, a storage failure that reaches every replica, or a datacenter that is gone. Replicas copy a mistake as faithfully as a record.
Two things make a backup worth having.
- Recovery time
-
An application that loses its cluster can start again on a restored one, from where it stood, instead of being rebuilt from its sources, if those still exist.
- Persistent messaging
-
Many topics are not a cache of something else. A command topic, a stream of payment orders, an event log that is the system of record: what is in them exists nowhere else. If Kafka is where a record lives, it has to survive the loss of the cluster.
A backup tool such as Kannika does the copying. It reads every partition continuously, the way any consumer does, and writes the records to object storage. It backs up the consumer group offsets too, so that after a restore every application can resume where it stood.
Tidemark copies no data. It writes down, every few seconds and beside the backup, where a restore may cut so that the result is a state the cluster really had. The rest of this chapter is about why that line is needed and how it is found.
Why a restore of the backup as it is breaks
The backup reads each partition on its own schedule. It closes a segment when the segment is full or old enough, and only closed segments can be restored. So at any moment the backup of one topic reaches further than the backup of another.
Take an application that reads orders and writes an invoice for each order to invoices.
If the backup of invoices happens to be further along than the backup of orders, a restore holds invoices whose order is missing.
The order will not arrive later either: it was never restored, so the application never sees it again.
With two topics a team can reason about this. With several hundred applications, chains of them, and topics that feed back into each other, nobody can, and a restore is the wrong moment to find out.
What a consistent cut is
A cut is one position per partition: everything below it is restored, everything from it on is not. A cut is consistent when it contains no effect without its cause.
The idea comes from Chandy and Lamport’s paper "Distributed Snapshots" (1985).
In Kafka terms, the cause is a record appended to orders, and its effect is the record an application appends to invoices after reading it.
A cut can place the two in four ways:
| Order | Invoice | After a restore |
|---|---|---|
inside the cut |
inside the cut |
Both back. Fine. |
inside the cut |
outside the cut |
The order is back, its invoice is not. The application reads the order again and writes the invoice. Fine. |
outside the cut |
outside the cut |
Neither is back. Fine, apart from the loss itself. |
outside the cut |
inside the cut |
An invoice without its order: a state the cluster never had. Inconsistent. |
The two directions are not equally bad. A cause without its effect is harmless: it is the state after any application crash, and every Kafka application is built for it. It resumes from its committed offset and works through the gap. An effect without its cause cannot heal, because nothing will ever produce the cause again.
So a cut does not have to put every partition at the same instant. It only has to make sure that nothing inside it has its cause outside.
One topic, one application, its output, and a backup of each. Watch what the backups hold, and what a restore of them would bring back.
Why one instant is impossible, and not needed
The obvious cut is one instant for the whole cluster: stop everything, note every position, start again. Kafka cannot be stopped, and it has no shared clock. Each broker leads its own partitions, and record timestamps come from producers whose clocks nobody controls. A cut taken by timestamp is only as good as the worst of those clocks, and that error cannot be measured.
What can be done is to ask every broker at once. Tidemark sends one request per leader broker, all in the same moment, and asks each for the newest offsets of its partitions. The answers come back within a few milliseconds of each other. For each partition, Tidemark records the time from the send to that partition’s answer, its interval. The widest interval is the cut width: no two readings of the cut can lie further apart than that.
A cut a few milliseconds wide is not one instant, and it does not need to be. An effect takes time to come about. An application has to fetch the cause, process it, and produce the effect, and the effect only becomes readable once the broker has replicated it. If the cut is provably narrower than the fastest such chain in the cluster, no effect can have been produced from a cause the cut missed: there was not enough time.
The rest is measurement. On a three-broker test cluster, for example, the cut width had a median of about 1.2 ms, and the fastest processing chain a median of about 6 ms. What a given cluster does is its own number, and Tidemark measures it in every round.
|
Note
|
The argument rests on one assumption: the effect is produced by a process that first consumed the cause. Three shapes do not fit it, and for them the manifest can warn but not promise:
For application teams has the checklist that keeps an application inside the assumption. |
The canary and the width of a cut
"Narrower than the fastest chain" is a claim about the cluster’s applications, and Tidemark cannot see inside them. So it runs the fastest application it can think of, the canary.
The canary writes a small record into the topic tidemark.canary.in, reads it back, writes it into _tidemark.canary.out, and reads it back from there.
It does no work in between.
The time from the first write to the last read is one _chain: two hops, each written with acks=all and readable only once it is replicated.
Every real application does at least that, plus its own work, so no real chain is faster than the canary’s.
The canary runs continuously, about once a second, on its own thread. The fastest chain it measured during a round is that round’s chain floor. Its distribution sits next to the cut width:
Each partition is judged on its own. A partition whose interval is below the chain floor is clean: every chain in the cluster is longer than the uncertainty of its reading. A partition whose interval reaches the floor is wide: a chain as fast as the canary could have crossed it. The verdict compares the widest interval with the fastest chain, the two ends that must not meet, so it is strict by construction.
Health is recorded per partition rather than per manifest because one slow broker widens only the partitions it leads. Whoever restores one damaged topic needs to know whether that topic was clean, not whether an unrelated broker hesitated.
The canary is a lower bound, not a guarantee. An application that does almost nothing is nearly as fast as one that does nothing, so the margin can be close to zero. That is why both numbers are published and worth watching rather than assumed.
What Tidemark does every few seconds
It copies no data. It reads where every partition and every group stands, at one instant, measures how wide that instant really was, and writes it down beside the backup.
One round, every ten seconds by default, has six steps:
-
Consumer groups. Tidemark reads
__consumer_offsetscontinuously and keeps every group’s committed offsets in memory. The round takes a snapshot of that view first. -
Transactions. In the same way it follows
__transaction_state, so it knows which transactions are open and which commit while the round runs. -
Last stable offsets. The fan-out: one
ListOffsetsrequest per leader broker, withread_committed, all sent together. Every answer is timestamped as it arrives. -
High watermarks and log start. Read right after the cut, for diagnosis: the distance between high watermark and cut shows open or hung transactions.
-
Judge the cut. Every partition’s interval against the round’s chain floor, and every transaction against the cut.
-
Write the manifest to object storage.
A round asks the whole cluster in one go and is never patched. If a partition does not answer in time, the whole round is repeated at once with a longer deadline. If the longest deadline is not enough either, the round still writes a manifest, and names the partitions it is missing and why.
Nothing is discarded. A manifest from a misbehaving cluster is not wrong, only wider, and it says by how much. Throwing it away would leave nothing recent exactly when the cluster is in trouble, which is when the backup matters.
What a manifest holds
A manifest is one JSON file in object storage, written once per round. It holds everything a restore needs, so that choosing a restore point does not require a running Tidemark: the case it exists for is the case where nothing is running.
- The cut
-
For every partition, the
first_excluded_offset: every record strictly below it is in the cut, the record at it is not. Next to it the partition’s leader, its interval and its health. The offsets are those of the original cluster. A restore renumbers, and the backup tool keeps each record’s original offset in a header, so a restore translates offsets instead of computing them. - Group offsets
-
For every consumer group, the committed offset on every partition, from before the cut. Groups that stand beyond the cut, or below the start of the log, are listed separately.
- Transactions
-
Which transactions were open, which ones the cut fell through, and where the round could not tell.
- Topics
-
Every topic with its partition count, replication factor and configuration, including the topics the restore leaves out. A restore creates them before it restores a record into them: what it needs is what was there, not what somebody meant to be there.
- ACLs
-
The cluster’s access control lists, with a state that says what an empty list means: no authorizer, no permission to read them, or a cluster that grants nothing.
- Canary and width
-
The chain floor, the canary’s distribution in the round, and the cut width.
Topics and ACLs come from the metadata refresh that follows the previous round, so they describe the cluster about one round before the cut. Definitions and access change over days; a cut changes over milliseconds.
A manifest is judged on three axes, each recorded in the file:
| Axis | Holds when |
|---|---|
clean |
every partition is clean: its interval was below the chain floor. |
complete |
every partition the round expected answered and has an offset. |
atomic |
no transaction is split by the cut, and none is undecided. |
A manifest that holds all three is a clean restore point. Anything else is degraded: wide on some partitions, incomplete, or not atomic. A degraded manifest is still a complete answer for what it does name, and the file says exactly where it falls short. Automated consumers take only clean ones; a degraded one is for a human who chooses it deliberately, with the numbers in front of them.
Restore point and RPO
A manifest is only usable once the backup has reached it. The backup may still be behind on some partition, and a cut the backup does not cover cannot be restored. So two different numbers matter:
-
The round cadence sets how fine the choice of restore points is.
-
The backup’s coverage says how far it has written, per partition.
The restore point is the newest manifest that is written to object storage and that the backup covers on every partition of its cut. The recovery point objective in practice, the RPO, is the age of that manifest. Everything after its cut is not in a restore.
The slowest partition decides, never the average. One backup partition that lags behind holds the restore point back for the whole cluster. A manifest still in Tidemark’s local buffer does not count: if the cluster is lost, Tidemark and its buffer are probably lost with it.
Tidemark publishes the age of the newest restore point twice: for clean manifests, and for manifests of any class. The clean one is what a guided restore takes without an override, and the number to alarm on. The gap between the two shows how degraded the cluster is right now. For both, Tidemark reads the backup’s own index through a provider for the backup tool; Configuration says how to turn it on.
What a restore gives back
A restore rebuilds the cluster at one manifest’s cut: every partition up to its first_excluded_offset, every topic with its configuration from the manifest, and every group at its offset.
How that is done, step by step, is During the disaster.
What the applications find afterwards follows from four facts.
Consumer offsets are taken before the cut
A group’s offset and the cut are read a few milliseconds apart, and the order matters.
Tidemark takes the group offsets first and the partition offsets second.
A group can only commit an offset it has consumed, and a read_committed consumer cannot consume past the last stable offset.
Read in that order, every group offset lies at or below the cut on its partition.
So after a restore a group stands rather too early than too late. It reads the records between its last commit and the cut again, and processes them a second time. That is the harmless direction: a duplicate, never a gap.
A group that stands beyond the cut anyway, because it reads read_uncommitted or because someone moved it with --reset-offsets, is named in the manifest.
A restore reports it and leaves it to a human.
It never moves such a group to the cut on its own: a plausible-looking number would hide a broken group.
There is no rewind. A group resumes exactly at its offset in the manifest, because everything it built from the records before that offset comes back at the same cut: its outputs, and its state in the changelog topics.
Transactions and the last stable offset
The backup reads with read_committed, so it can never reach past a partition’s last stable offset: the first offset of the earliest transaction still open on it, or the high watermark when none is.
Tidemark cuts there.
That is also the right boundary in principle: what lies between the last stable offset and the high watermark is either aborted, and must never come back, or still open, and not decided yet.
Two consequences are worth knowing.
An open transaction holds the cut on its partition at its first record.
Records of other producers that committed after it began wait behind it, too.
A transaction that hangs freezes the cut on its partitions until the broker aborts it, which can take up to transaction.timeout.ms.
Every round still writes a current manifest, but those partitions stay where they were, so for them transaction.timeout.ms is part of the RPO.
A transaction that spans several partitions commits on each of them separately: the broker writes a commit marker into every partition, one after the other. A cut read in between can find the transaction committed on some partitions and open on others. Then its output can be inside the cut while its offset commit is outside, or the reverse. This is not a sign of a sick cluster; it happens on an idle one, to a few percent of the partitions such a transaction touches.
Tidemark does not hide it. It follows the transaction log, decides for every transaction whether the cut fell through it, and names it in the manifest. A manifest with a split transaction is not atomic, so it is not a clean restore point. If a human chooses it anyway, the restore sets the groups the transaction touched one commit back, to the previous offsets the manifest carries for them. The application then repeats the transaction instead of losing part of it: at-least-once instead of loss.
State stores are rebuilt
Kafka Streams keeps its state in local stores, usually RocksDB, and writes every change to a changelog topic as well. The local stores are not part of the cut. After a restore they hold what happened after the cut, which the restored cluster knows nothing about. So they are deleted before the application starts, and Kafka Streams rebuilds them from the restored changelog topics, which are in the cut. For application teams says how.
At-least-once after a restore
All of this points the same way. After a restore, every application processes some records a second time: the stretch between its last commit and the cut, and the transactions the cut fell through. That holds for exactly-once applications, too. Their producer gets a new identity on the restored cluster, so Kafka’s deduplication does not reach across the restore.
The processing guarantee after a restore is at-least-once. Exactly-once across a restore is not possible, with Tidemark or without it. Within Kafka that is harmless, as long as applications tolerate seeing a record twice.