Apache Kafka — Event Streaming from Basics to a Platform Service›16 · Capacity, performance and disaster recovery

Lesson 16 of 17 · Operating Kafka

Capacity, performance and disaster recovery

Size a Kafka cluster from numbers instead of guesses: storage from throughput, retention and replication; network from produce, replication and consumer fan-out; partition counts from target throughput per partition and consumer; broker counts from the tightest limit. Benchmark with the built-in perf tools. Then plan disaster recovery: what replication within a cluster protects against and what it does not, MirrorMaker 2, active-passive and active-active designs, consumer offset translation, stretch clusters, and the RPO and RTO you can honestly promise.

Advanced
Key wordscapacity planningsizingthroughputretentionstorage formulareplication factornetwork bandwidthpartition countpartitions per brokerkafka-producer-perf-testkafka-consumer-perf-testbenchmarkdisaster recoveryRPORTOMirrorMaker 2active-passiveactive-activeoffset translationstretch clustermulti-regioncost

Size from numbers

Start with the workload, per topic or in total:

Input Example
Peak write rate 50 MB/s (after compression)
Average record size 1 KB
Retention 3 days
Replication factor 3
Consumer groups reading everything 3
Growth over 12 months ×2

Storage

50 MB/s × 259,200 s (3 days) ≈ 13 TB of data
× RF 3                       ≈ 39 TB on disk
× 1.4 headroom               ≈ 55 TB  → e.g. 6 brokers × ~9 TB usable

Headroom covers growth, uneven partitions and space needed while moving partitions. Brokers should not run above about 70–75% disk.

Network

into brokers:   50 MB/s produce + 100 MB/s replication (RF−1 copies)      = 150 MB/s
out of brokers: 100 MB/s replication + 50 MB/s × 3 consumer groups        = 250 MB/s

Spread over 6 brokers that is about 25 MB/s in and 42 MB/s out each, at peak. Plan to keep under about half of a NIC's real capacity so a broker failure (whose load moves to the others) and catch-up reads still fit. In the cloud, cross-zone traffic is billed: replication across three zones and consumers in other zones can cost more than the brokers. Follower fetching (client.rack) reduces it.

Partitions

A partition is processed by one consumer per group and lives on one disk per replica, so:

partitions ≥ target throughput ÷ throughput one partition sustains (producer and consumer side)
partitions ≥ number of consumers you need in parallel

If one consumer handles 5 MB/s and a topic needs 50 MB/s, you need at least 10 partitions; choose 12 or 24 for growth, since adding partitions later moves keys. Keep the total per broker sensible (a few thousand partition replicas per broker is a common comfortable range; very large numbers raise memory, open files and recovery time).

Brokers

Take the tightest limit: disk, network, CPU (TLS and compression cost CPU), or partition count. Then add one broker of slack so the cluster still fits when a broker is down or being upgraded.

Planning a cluster is like planning a school's lockers. How many books arrive each day (throughput), how many days each book stays (retention), and how many copies of each book you keep (replicas) tell you how many lockers you need. How many classes want to read every book (consumer groups) tells you how wide the corridors must be. And you always keep some empty lockers, for new pupils and for the day a locker breaks.

Benchmark before you trust the numbers

Use the built-in tools against a cluster like production (same instance types, disks, TLS and compression):

$ kafka-producer-perf-test.sh --topic perf --num-records 2000000 --record-size 1000 --throughput -1 \
    --producer-props bootstrap.servers=b:9092 acks=all linger.ms=10 compression.type=lz4
$ kafka-consumer-perf-test.sh --bootstrap-server b:9092 --topic perf --messages 2000000 --group perf-1
  • Run several producers in parallel to find the cluster's limit, not one client's.
  • Watch broker metrics during the test: idle percentages, disk and network.
  • Test degraded mode: stop a broker during the run and confirm throughput and latency stay within your SLO.
  • Repeat after major version upgrades and configuration changes.

Disaster recovery: what can go wrong

Failure Protected by replication in one cluster? Needs
A broker or disk fails ✔ RF 3, min ISR 2
One availability zone fails ✔ if replicas span 3 zones Rack awareness
The whole region or cluster is lost ✘ A second cluster
A topic is deleted or bad data is written ✘ (replicated instantly) Delayed copies, backups, ACLs on delete
A bad upgrade corrupts the cluster ✘ A second cluster, tested rollback

MirrorMaker 2

MirrorMaker 2 (MM2) is Kafka's tool for copying data between clusters. It runs on Kafka Connect and copies topics, topic configuration, ACLs (optionally) and consumer offsets:

  • MirrorSourceConnector copies records.
  • MirrorCheckpointConnector translates consumer group offsets, so groups can resume on the other side.
  • MirrorHeartbeatConnector checks the link.

By default mirrored topics are renamed with the source prefix (primary.orders), which avoids loops in active-active setups. For active-passive, the identity policy keeps the same names.

Patterns

Pattern How Pros Cons
Active-passive MM2 copies primary → DR; clients switch on failure Simple; clear source of truth DR idle; some data lost on failover (RPO = MM2 lag); failback needs planning
Active-active Each region writes locally; MM2 copies both ways with prefixed topics Local writes, both used Consumers must read local + remote topics; ordering across regions; more complex
Stretch cluster One cluster across 3 nearby regions or zones, synchronous replication RPO 0, automatic failover Needs low latency between sites; every write pays the distance
Backup to object storage Sink connector archives topics Protects against deletion and bad data Restore is slow; not a hot standby

Be honest about RPO and RTO

  • RPO (data you may lose) for asynchronous mirroring ≈ the replication lag at the moment of failure: seconds to minutes. Measure it and alert on it.
  • RTO (time to recover) is mostly switching clients: DNS or bootstrap changes, credentials and ACLs present on the DR side, schemas in a shared or mirrored registry, consumers starting from translated offsets.
  • Practise failover regularly. An untested DR plan is a hope, not a plan.
  • Consumers must be idempotent: after failover some records are processed again.

Try it: a capacity sheet and a DR drill

  1. Fill the sizing formulas for a workload of your choice (or: 20 MB/s, 7 days, RF 3, 4 consumer groups). How many brokers, partitions and TB do you need?
  2. Run the perf tests on the 3-broker lab cluster with acks=1 and acks=all, with and without lz4, and record the differences.
  3. Run two single-node clusters and a MirrorMaker 2 process between them (connect-mirror-maker.sh with a small properties file). Produce to the source, consume on the target, and check primary.<topic> exists.
  4. Write down the RPO and RTO you would promise for that setup and what you would need to measure them.

Going deeper

  • Cost per GB in, out and stored is the basis for chargeback (lesson 17). Measure it per topic.
  • Tiered storage changes the sizing: local disk only holds hot data; object storage holds the rest at a fraction of the cost.
  • Plan cluster migrations (version, cloud, managed service) as a DR exercise: mirror, move consumers, then producers, then retire.

Recap

  • Size storage = rate × retention × RF + headroom; network includes replication and every consumer group.
  • Partition count comes from throughput per partition and consumer parallelism; leave room to grow.
  • Benchmark with the perf tools, including with a broker down.
  • In-cluster replication does not cover region loss or deletion: use MirrorMaker 2, a stretch cluster, or backups.
  • Promise an RPO you measure (replication lag) and an RTO you have practised.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.