Home / Data Platform / Streaming & CDC

Data PlatformRunbookAdvanced

Kafka KRaft broker decommission runbook: move partitions off, shut down and unregister

Remove a broker from a KRaft-mode Kafka cluster without offline partitions - drain its replicas with a targeted reassignment, stop it cleanly and unregister it from cluster metadata.

Deepak Bhardwaj7 min read

Applies toApache Kafka 3.x in KRaft mode and Kafka 4.x; Confluent Platform and Red Hat Streams for Apache Kafka use the same tools without the .sh suffix

Goal

You need to retire a broker: old hardware, a shrink after a migration, or a node you no longer trust. Stopping it is not enough. Any partition with a replica on that broker stays under-replicated forever, a partition with replication factor 1 goes offline, and in KRaft mode the broker stays registered in the controller's metadata until you remove it.

The safe order is always:

  1. Move every replica off the broker.
  2. Confirm nothing references it.
  3. Shut it down.
  4. Unregister it.

This runbook uses broker ID 4 as the one leaving and 1,2,3,5 as the brokers that stay. Replace them with your IDs.

Warning

Do not stop the broker first and "fix it later". Partitions whose only in-sync replica was on that broker go offline, and producers using acks=all with min.insync.replicas set may start failing with NOT_ENOUGH_REPLICAS.

Pre-checks

Cluster is healthy before you start

BS=kafka1:9092
CFG=admin.properties   # client config with your security settings

# Must return nothing
kafka-topics.sh --bootstrap-server $BS --command-config $CFG --describe --under-replicated-partitions
kafka-topics.sh --bootstrap-server $BS --command-config $CFG --describe --unavailable-partitions

# Quorum is healthy and the controllers agree
kafka-metadata-quorum.sh --bootstrap-server $BS --command-config $CFG describe --status

If any partition is already under-replicated, fix that first. A reassignment on top of a degraded cluster is how one broker removal becomes an outage.

Capacity on the remaining brokers

The brokers that stay will absorb the leaving broker's data. Check free disk on each one and compare it with the leaving broker's log size:

kafka-log-dirs.sh --bootstrap-server $BS --command-config $CFG \
  --describe --broker-list 4 | tail -1 > broker4-logdirs.json

The JSON lists every partition replica on broker 4 with its size in bytes. Sum them and make sure the remaining brokers have that much headroom, plus room for normal growth. Remember to keep the rack layout valid if you use broker.rack: replacements for a replica should come from the same rack where possible.

Is it a broker, a controller or both?

grep -E '^(process.roles|node.id)' /opt/kafka/config/server.properties

If process.roles includes controller, this node is also a quorum voter. Removing a controller is a separate procedure (kafka-metadata-quorum.sh remove-controller on dynamic quorums, or a config change on static quorums). Do that as its own change; this runbook covers the broker role only.

Step 1: build a targeted reassignment

kafka-reassign-partitions.sh --generate with a new --broker-list proposes a fresh layout for every listed topic, which usually moves far more data than needed. Use it only to dump the current assignment, then replace broker 4 yourself.

# All topics, including internal ones such as __consumer_offsets
kafka-topics.sh --bootstrap-server $BS --command-config $CFG --list \
  | python3 -c 'import sys,json; print(json.dumps({"version":1,"topics":[{"topic":t.strip()} for t in sys.stdin if t.strip()]}))' \
  > topics.json

kafka-reassign-partitions.sh --bootstrap-server $BS --command-config $CFG \
  --topics-to-move-json-file topics.json --broker-list 1,2,3,4,5 --generate \
  | sed -n '/Current partition replica assignment/{n;p}' > current.json

Then rewrite only the partitions that include broker 4, picking the least-loaded remaining broker that is not already a replica:

# drain_broker.py  usage: python3 drain_broker.py current.json 4 1,2,3,5 > drain.json
import json, sys
from collections import Counter

current = json.load(open(sys.argv[1]))
leaving = int(sys.argv[2])
targets = [int(b) for b in sys.argv[3].split(",")]

load = Counter()
for p in current["partitions"]:
    for r in p["replicas"]:
        load[r] += 1

moves = []
for p in current["partitions"]:
    if leaving not in p["replicas"]:
        continue
    candidates = sorted((b for b in targets if b not in p["replicas"]), key=lambda b: load[b])
    if not candidates:
        sys.exit(f"No target for {p['topic']}-{p['partition']}")
    new = candidates[0]
    load[new] += 1
    replicas = [new if r == leaving else r for r in p["replicas"]]
    moves.append({"topic": p["topic"], "partition": p["partition"], "replicas": replicas})

json.dump({"version": 1, "partitions": moves}, sys.stdout, indent=1)
print(f"{len(moves)} partitions to move", file=sys.stderr)

Replacing broker 4 in place keeps the replica order, so if broker 4 was the preferred leader, the new broker becomes the preferred leader for that partition. If you use racks, extend the script to prefer a target in the same rack.

Keep current.json. It is your rollback file.

Step 2: execute with a throttle

kafka-reassign-partitions.sh --bootstrap-server $BS --command-config $CFG \
  --reassignment-json-file drain.json --execute --throttle 50000000

--throttle is in bytes per second for replication traffic of the moving partitions. Start conservatively, watch producer latency and broker network, and raise it by re-running --execute with a higher --throttle value if the cluster has room.

Track progress:

kafka-reassign-partitions.sh --bootstrap-server $BS --command-config $CFG \
  --reassignment-json-file drain.json --verify

Warning

Keep running --verify until every partition reports complete. --verify is also what removes the throttle configs once the reassignment finishes. If you skip it, the replication throttle stays in place and slows recovery after later incidents.

If the reassignment hurts production, stop it:

kafka-reassign-partitions.sh --bootstrap-server $BS --command-config $CFG \
  --reassignment-json-file drain.json --cancel

Step 3: confirm nothing references broker 4

kafka-topics.sh --bootstrap-server $BS --command-config $CFG --describe \
  | grep -E 'Replicas: ([0-9]+,)*4(,[0-9]+)*\s' || echo "broker 4 holds no replicas"

kafka-log-dirs.sh --bootstrap-server $BS --command-config $CFG \
  --describe --broker-list 4 | tail -1 | python3 -c 'import sys,json; d=json.load(sys.stdin); print(sum(len(l["partitions"]) for b in d["brokers"] for l in b["logDirs"]))'

Both should show zero partitions. Then move leadership back to preferred replicas so the new layout is balanced:

kafka-leader-election.sh --bootstrap-server $BS --admin.config $CFG \
  --election-type PREFERRED --all-topic-partitions

Also update anything that names broker 4 directly: client bootstrap.servers lists, DNS records, load balancer targets, monitoring and alert rules.

Step 4: shut down and unregister

Stop the broker with a normal service stop so it performs a controlled shutdown:

sudo systemctl stop kafka

In KRaft mode the controller still holds a registration for the broker after it stops. Remove it:

kafka-cluster.sh unregister --bootstrap-server $BS --config $CFG --id 4

Some versions and distributions document --bootstrap-controller <controller:port> for this command instead of --bootstrap-server; check kafka-cluster.sh unregister --help on your build. The command calls the UnregisterBroker API and removes the broker from cluster metadata. Leaving stale registrations behind causes confusing metadata and tooling output later.

Caution

Only unregister a broker that holds no replicas and will not come back with the same node.id. Wipe or rebuild its log directories before reusing the host, so an old copy of the data cannot rejoin the cluster.

Symptom → cause → fix

Symptom Likely cause Fix
--verify shows a partition still in progress for hours Throttle too low, or a very large partition Raise --throttle by re-running --execute; check the target broker's disk and network
Under-replicated partitions appear during the move Normal while new replicas catch up; persistent if a target broker is overloaded Watch UnderReplicatedPartitions; lower the throttle or pause other heavy work
Producers get NOT_ENOUGH_REPLICAS after stopping the broker Replicas were not moved first Start the broker again, run steps 1-3, then stop it
Leaders piled onto one broker after the move Preferred leader election not run Run kafka-leader-election.sh --election-type PREFERRED
Broker still listed in tooling after shutdown Not unregistered kafka-cluster.sh unregister --id <id>

Prevent it next time

  • Keep a standard drain script like drain_broker.py in your ops repo, reviewed and tested, rather than writing one during a change window.
  • Alert on UnderReplicatedPartitions and OfflinePartitionsCount so a half-finished decommission is visible immediately.
  • Avoid replication factor 1 for anything you care about; it turns every broker removal into a data copy you cannot skip.
  • Put bootstrap addresses behind DNS names, so removing a broker does not need client config changes.
  • Check whether your Kafka distribution supports broker cordoning (Confluent Platform documents it); it stops new partitions being placed on a broker while you drain it.