Apache Kafka

Kafka replication is not a backup, so back up your topics to S3

By

Kafka replication is not a backup. MirrorMaker 2, Cluster Linking or any mirror copies what production does, so a bad producer write, a flood of tombstones or a shortened retention can reach the standby too. A real Kafka backup lives on separate storage with separate credentials, keeps history you can go back to, includes the Schema Registry _schemas topic, and comes with a plan for consumer offsets, because offsets are local to each cluster. This post shows how to back up Kafka topics to S3 with kaf-mirror, an open source (Apache 2.0) replication tool from Scalytics, and KafScale, a Kafka-compatible broker that stores immutable segments in S3. It is updated for kaf-mirror 2.0.0 and covers the replication halt switch (a tech preview) for a compromised source, how to restore, what 2.0 changes, and the limits you should know before you rely on it.


Ask a team how they back up Kafka and the answer is often "we have a DR cluster". MirrorMaker 2 or Cluster Linking keeps a second cluster in step with production, and if a site goes down they fail over. That is a sound setup. It is just not a backup.

A replica is made to copy what production does, as fast as it can. That covers the mistakes too. A release that writes broken events, a script that floods a compacted topic with tombstones, or an attacker with a stolen producer key all reach the standby within seconds. When you need the data from before the harm, the standby shows you the same damage.

We built kaf-mirror at Scalytics to copy Kafka topics between clusters. It can also copy them into KafScale, which keeps the data as immutable segments in S3. That second path is what makes a mirror into a backup. This post is up to date for kaf-mirror 2.0.0, and the kaf-mirror site has the docs and a short demo video. It walks through what a Kafka backup needs, where offsets and schemas catch people out, and how to set it up.

Code: github.com/scalytics/kaf-mirror. Open source under Apache 2.0, written in Go on top of franz-go. Current release is v2.0.0, also as a container at ghcr.io/scalytics/kaf-mirror:v2.0.0. Docs and demo are at kafmirror.scalytics.io. KafScale is at github.com/KafScale/platform.

Why Kafka replication is not a backup

Replication tools do their job fine. They keep a second cluster close to the first so you stay online when a region or a data center fails. The same design is why they cannot shield you from these cases.

  • Bad producer writes. A bug writes broken events into an orders topic. The mirror copies them like any other record, since to the mirror they are just records.
  • Tombstones and deletes. A tombstone is a normal record with an empty value. The mirror copies it, and if the copy is a compacted topic, the old value is gone there too once compaction runs.
  • Config changes. MirrorMaker 2 syncs topic configs to the remote topics by default. sync.topic.configs.enabled is true, the sync runs every 600 seconds, and retention.ms is not on the default exclude list (MirrorMaker configs). So someone who cuts retention on production can shorten it on the standby as well.
  • Stolen credentials. If the same keys or the same automation can write to both clusters, an attacker who has one has both.

The Apache Kafka docs describe MirrorMaker as a tool that replicates topics with data and configuration, consumer groups with offsets, and ACLs (Kafka geo-replication). That is just what you want for failover, and exactly what you do not want in a backup.

What a Kafka backup needs

A backup is a copy you can return to after production went wrong. For Kafka that comes down to a few things.

  • Separate storage and separate credentials. Production keys and production automation should not be able to change or delete it.
  • Data that is not rewritten. New records are added. Old ones stay as they were until your own retention removes them.
  • The schemas. Without the Schema Registry data, Avro or Protobuf records in the backup cannot be read.
  • A plan for consumers. After a restore, every consumer group needs to know where to start.
  • A tested restore. A backup you never restored is a guess.

Kafka offsets do not survive a move to another cluster

This is the part that catches most teams off guard. An offset is a position in one partition of one cluster. When a mirror writes a record to another cluster, that cluster gives it its own offset. Compaction, retention and transaction markers mean the numbers move apart, so offset 1,000 on the source is not offset 1,000 on the copy. kaf-mirror states this openly in its verify output: offsets are cluster-local, so source and target high watermarks are not compared.

MirrorMaker 2 deals with this for failover through checkpoints. It keeps a map of source offsets to target offsets and can write translated consumer group offsets to the target. The map is not exact for every record, so expect some records to be read again after a failover. For a backup restore you need a plan of your own.

  • Make consumers safe to replay. If a consumer can handle a record twice, you can start it a little early and lose nothing. My post on finding duplicate Kafka messages and checking consumer lag shows how to check what you got.
  • Find the start point by data, not by offset. An order id, an event id or a business date in the payload works on any cluster.
  • Write down where each group was. Export committed offsets and lag for your important groups on a schedule, so after an incident you know roughly how far each one had read.

If you have to dig through a topic by hand to find the last good record, my older post on corrupted Kafka messages and offset recovery covers that.

Back up the schemas too

With the Confluent serializers in their default format, each Avro, Protobuf or JSON Schema record starts with one version byte and a 4-byte schema ID, then the data (Confluent wire format). The ID refers to an entry in Schema Registry. If you restore the data into a cluster where the registry hands out different IDs, the bytes are still there but your consumers cannot read them.

Schema Registry keeps all schemas, versions, IDs and compatibility settings in a Kafka topic, _schemas by default. It is a compacted topic with one partition, and Confluent calls it the source of truth for schema IDs and says to back it up (Schema Registry backup and restore). So handle _schemas like any other topic in your backup, and restore it first.

Back up Kafka to S3 with kaf-mirror and KafScale

kaf-mirror consumes from a source cluster and produces to a target cluster. The source offset is committed only after the target has acknowledged the write, so a stopped job picks up where it left off. If the target is down, the consumer stops and its offset does not move, and the source cluster holds the data until the target is back. There is no extra disk buffer to look after.

The target can be another Kafka cluster for a hot standby, or KafScale for a backup. KafScale is a Kafka-compatible broker that runs on Kubernetes, keeps log segments in S3 and its metadata in etcd. The segments never change, so data that is already in the bucket is not rewritten by new writes. I wrote about the design in Kafka on object storage, an open source option you can self-host.

The README has a ten minute quickstart with a local MinIO bucket. First start KafScale.

git clone https://github.com/KafScale/platform.git && cd platform
make demo

The broker listens on 127.0.0.1:39092 and the segments land in the MinIO bucket kafscale. Before you add it to kaf-mirror, create every target topic yourself with the same partition count as its source. A job does not start when the source and target partition counts differ, and with KafScale v2.1.0 the make demo setup turns topic auto-create on with one partition, so a topic that is created by accident ends up with the wrong count (KafScale issue 446, and the kaf-mirror backup guide). Check the source, then create the target.

kafka-topics.sh --bootstrap-server source-kafka:9092 --describe --topic orders
# PartitionCount: 3
kafka-topics.sh --bootstrap-server 127.0.0.1:39092 --create --topic orders \
  --partitions 3 --replication-factor 1

Then build and start kaf-mirror in a second terminal.

mkdir -p bin
go build -o bin/kaf-mirror ./cmd/kaf-mirror
go build -o bin/mirror-cli ./cmd/mirror-cli
./bin/kaf-mirror

The idempotent producer stays on, which is the default. The kaf-mirror docs verify this with KafScale v2.1.0. The switch KAFMIRROR_DISABLE_IDEMPOTENT_WRITES=true is only needed for KafScale before v2.1.0. On the first start kaf-mirror creates admin@localhost with a random password and writes it to a file named data/kaf-mirror-bootstrap-<random>. Read it in a third terminal, then delete the file.

cat data/kaf-mirror-bootstrap-*
rm data/kaf-mirror-bootstrap-*

Now log in and add both clusters and a job. In 2.0 the --password flag is gone, and login asks for the password without showing it, so it never lands in your shell history.

./bin/mirror-cli login admin@localhost   # asks for the password
./bin/mirror-cli clusters add   # your source cluster
./bin/mirror-cli clusters add   # target: brokers 127.0.0.1:39092
./bin/mirror-cli jobs add       # pick source, target and topics
./bin/mirror-cli jobs start
./bin/mirror-cli jobs status full

jobs add lets you pick topics from the source cluster, and the default is to write them to the same name on the target. Exact new names and regex mappings such as app-.* are also possible (topic mappings). jobs status full shows the mirror state with progress, gaps and resume points. For production, run KafScale from its Helm chart against a real S3 bucket, and give the bucket its own credentials that production apps and admins do not have.

If you run on AWS, look at S3 Object Lock for the bucket. It stores objects write-once-read-many, and in compliance mode a locked object version cannot be deleted or overwritten by any user, including the root user, until the retention period ends. It only works with versioning turned on (S3 Object Lock). Match the lock period to the retention you set in KafScale before you turn it on.

Stop the copy when the source is under attack

A backup that keeps copying a hacked source fills up with bad data. kaf-mirror has a replication halt for that. It is a tech preview, and the docs call it experimental. It is off by default, and an admin turns it on with mirror-cli protection enable. Once it is on you can freeze every running job in four ways.

  • mirror-cli protection halt "source compromised" from the CLI.
  • POST /api/v1/protection/halt on the API.
  • Creating the file data/HALT.
  • Setting KAF_MIRROR_HALT=1.

The file and the env variable work when the API is down. Jobs stay paused until an admin starts them again. You also label each cluster as prod, dr or other, and while protection is on, kaf-mirror will not start a job that writes into a prod cluster. Since 2.0 the check also runs before every send to the target, including retries, so a record that triggers the automatic halt is rejected before it is produced or committed. Writes that already went to the target cannot be taken back.

There is also an automatic halt. kaf-mirror samples records and stops when many existing keys suddenly get high-entropy values (which looks like encryption in place), when a flood of empty values arrives, when produce or auth errors burst, or when payloads grow a lot while source lag suddenly drops. These are rules of thumb and can fire on a genuine bulk rewrite. If you already know the source is bad, halt by hand.

Two public incidents show why the copy has to sit where production cannot reach it, even though neither one involved Kafka. In May 2024 Google Cloud deleted the VMware Engine private cloud of UniSuper, an Australian pension fund, because an operator had left a parameter blank when it was set up a year earlier (Google's incident report). UniSuper ran copies in two geographies, and the deletion removed both (joint statement from UniSuper and Google Cloud). What got the fund back online were backups the deletion did not touch, including copies with another provider. In April 2026 a Cursor coding agent at PocketOS found an API token with full rights and deleted the production volume on Railway in one call. The volume-level backups went with it, because Railway kept them in the same volume (The Register). Railway later restored the data from its own disaster backups.

kaf-mirror would not have stopped either of these. A halt does nothing against a delete call made straight to the cloud platform. What changes is where the backup lives. The KafScale copy sits in its own S3 bucket, which can be in another cloud account, under credentials that production apps, admins and their tokens never get. Segments already in that bucket are not rewritten by new writes. And when the damage does come through Kafka, as encrypted values or a flood of tombstones, the halt stops the copy so the bad records do not pile up on top of the good ones.

Restore and test it

Because KafScale speaks the Kafka protocol, you read the backup back out with normal Kafka clients, for example a second kaf-mirror job that points from KafScale to a fresh cluster. Go in this order.

  1. Restore _schemas first and start Schema Registry on it, so the schema IDs match the data.
  2. Restore the data topics, or just the topics you need.
  3. Start consumers from a known point by data or business date, with replay-safe processing.
  4. Check counts and duplicates before you open the restored cluster to users.

Run this as a drill on a schedule, not for the first time in the middle of an incident. A drill also shows you how long a restore takes, which is the number your bosses will ask for.

What kaf-mirror 2.0 changes for a Kafka backup

The 2.0.0 release is a major version with breaking changes, so read the Breaking section of its CHANGELOG.md before you upgrade. These are the parts that matter for a backup.

  • Timestamps and partitions are kept. Two gaps that I reported while writing this post are closed. Issue 27 (lost source timestamp) and issue 28 (source partition not kept) were both fixed in pull request 29, which is part of 2.0.0. A reset by date on the copy now works on the original time, as long as the target topic uses the default CreateTime.
  • A recreated source topic is followed. A job tracks the Kafka topic ID. If someone deletes a source topic and creates it again, the job switches to the new topic and keeps copying. If the topic is deleted and not created again, the job stays active, counts the errors in error_count and lists the topic in missing_source_topics.
  • Halt checks every send. See the halt section above. It is still a tech preview.
  • The CLI is safer to script. Job commands refuse to run without a selected job, mirror-cli exits with status 1 when a command fails, and destructive commands need --yes without a terminal. Passwords are only read from a prompt or from stdin. Tokens, keys and passwords are no longer printed, and the token file is always mode 0600. That helps when you script your restore drill.
  • Safer defaults. CORS now defaults to http://localhost:8080 instead of *. The release also updates dependencies with security fixes.
  • Easier to deploy. The Docker image and the Helm chart (version 0.3.0) start with their defaults. On kind and k3s you turn on the optional volumePermissions init container. Released images report their real version. mirror-cli now ships for Linux arm64 and Apple Silicon, and the image ghcr.io/scalytics/kaf-mirror:v2.0.0 is published for linux/amd64 and linux/arm64.

What a copied record keeps in kaf-mirror 2.0

Per the kaf-mirror record fidelity doc at tag v2.0.0. Green is kept as is, amber is kept with a condition, red is new on the target.

Key and value
Copied as is, including null keys and tombstones.
Headers
All of them, in their original order.
Partition
Source partition N goes to target partition N. The job only starts when both topics have the same partition count.
Timestamp
The source timestamp becomes the target timestamp. A target topic set to LogAppendTime replaces it with its own append time.
Offset
Not kept. The target broker assigns its own.

Before 2.0 the timestamp was the time of the copy and the partition was not kept (kaf-mirror issues 27 and 28).

In the 2.0.0 release tests, Redpanda (the README quick start) and KafScale v2.1.0 were used as targets, with the target topics created first.

Things to know before you rely on it

  • At least once, not exactly once. The idempotent producer removes duplicates from client retries, but there is no transaction across clusters. If you stop a job while a record is in flight, that record can be copied a second time.
  • Offsets are new, the rest of the record is kept. Since 2.0 the key, value, headers, partition and source timestamp are copied. The target assigns its own offsets. A target topic set to LogAppendTime replaces the timestamp, and the target broker rejects timestamps outside its message.timestamp.before.max.ms and message.timestamp.after.max.ms limits.
  • Target retention counts from the source time. Because the timestamp is kept, a record older than the retention.ms of the target topic is deleted soon after the copy. Set the retention of your backup topics on purpose.
  • Create the target topics first. On KafScale v2.1.0 create every target topic with the source partition count, whatever the auto-create setting is.
  • Verify is not a record compare. validate-mirror uses the lag of the source consumer group and checks that topics exist. It does not prove both sides hold the same records.
  • Halt is not ransomware protection. It is experimental, it can fire on a genuine bulk rewrite, and it stops new copying. It cannot see encryption done directly on the S3 objects, does not delay replication, and does not roll anything back.
  • Small internal database. Jobs, users and metrics live in SQLite. Metrics and events are kept 30 days by default, and admin-cli backup full backs up the tool itself.

If you would rather not run this yourself, kaf-mirror is part of Lascaris, and the whitepaper there explains how it sits next to the Kafka you already run.

Frequently asked questions

Is Kafka replication a backup?

No. Replication copies bad writes, tombstones and, with MirrorMaker 2 defaults, topic config changes to the standby. A backup needs separate storage and credentials and history you can return to.

Does MirrorMaker 2 copy consumer offsets?

It translates consumer group offsets through checkpoints and can write them to the target. The translation is not exact for every record, so expect some re-reads after a failover.

How do I back up Schema Registry?

Back up the _schemas topic, which holds all schemas and IDs, and restore it before the data so the schema IDs in your records still match.

Can I back up Kafka to S3 without Kafka Connect?

Yes. kaf-mirror copies topics into KafScale, which stores them as segments in S3. You read them back with any Kafka client.

Does kaf-mirror keep timestamps and partitions?

Since 2.0.0, yes. The source record timestamp becomes the target timestamp, and source partition N is written to target partition N. Offsets are not kept, because the target assigns its own.

Is kaf-mirror free?

Yes. The code is open source under Apache 2.0.

Source code

kaf-mirror is on GitHub at github.com/scalytics/kaf-mirror, with a Docker Compose setup that starts two local Kafka brokers (make dev-up), a Helm chart and the replication halt docs in docs/protection.md. Get the binaries from the v2.0.0 release page, or read the docs and watch the demo on kafmirror.scalytics.io. For large single messages that do not belong in Kafka at all, see my post on Kafka to S3 for large messages with kaf-s3.

If your Kafka has a DR cluster but no real backup, and you need to show auditors how you would get the data back, that is the kind of problem I work on in my Kafka consulting.

Need help with distributed systems, backend engineering or data platforms? See my consulting services.

Alexander Alten

Alexander Alten

Founder of Scalytics, co-creator and PMC member of Apache Wayang, and author of KafScale. I write about Kafka, streaming, data platforms, IoT and AI agent infrastructure in production.