Lab 7 of 8 · Monitoring and running Kafka in production
Survive a broker failure
Run a three-broker cluster, keep writing with one broker down, and see it recover.
- Time
- 75 minutes
- Cost
- Free.
What you learn
Production Kafka runs as a cluster so that losing one broker loses no data. You run three brokers with Docker Compose, replicate a topic three ways, stop a broker on purpose, and write the runbook for it.
- Replication factor, in-sync replicas and min.insync.replicas
- Leaders, followers and what happens when a broker fails
- The signals to watch: under-replicated partitions and consumer lag
- Writing a runbook for a broker failure
In the AI era: AI can summarise broker logs and suggest causes during an incident. Treat its answer as a lead, not a diagnosis: confirm it with `kafka-topics --describe` and the metrics before you act.
Get set up
Commands use Homebrew in the Terminal app. Already have a tool? Skip it.
- A terminalOfficial install page ↗
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Use the Terminal app. The command installs Homebrew, which the other macOS commands use.
- Multipass (a free Ubuntu server on your computer)Official install page ↗
brew install --cask multipassCheck it worked:
multipass version
Before you start
Start your server,
cd ~/kafka-streaming-lab, and stop the single broker so its port is free:docker stop kafka schema-registry.
Step by step
1 Write the cluster file
Three brokers, each also a KRaft controller. Every topic gets three copies by default, and a write needs at least two copies to succeed. Create
cluster/compose.yaml.x-kafka: &kafka image: apache/kafka:4.1.0 environment: &env KAFKA_PROCESS_ROLES: broker,controller KAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka-1:9093,2@kafka-2:9093,3@kafka-3:9093 KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT KAFKA_DEFAULT_REPLICATION_FACTOR: 3 KAFKA_MIN_INSYNC_REPLICAS: 2 KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 3 KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 3 KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 2 CLUSTER_ID: lab-cluster-0000000001 services: kafka-1: <<: *kafka hostname: kafka-1 ports: ["9092:9092"] environment: <<: *env KAFKA_NODE_ID: 1 KAFKA_LISTENERS: INTERNAL://:19092,CONTROLLER://:9093,EXTERNAL://:9092 KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka-1:19092,EXTERNAL://localhost:9092 kafka-2: <<: *kafka hostname: kafka-2 ports: ["9094:9094"] environment: <<: *env KAFKA_NODE_ID: 2 KAFKA_LISTENERS: INTERNAL://:19092,CONTROLLER://:9093,EXTERNAL://:9094 KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka-2:19092,EXTERNAL://localhost:9094 kafka-3: <<: *kafka hostname: kafka-3 ports: ["9095:9095"] environment: <<: *env KAFKA_NODE_ID: 3 KAFKA_LISTENERS: INTERNAL://:19092,CONTROLLER://:9093,EXTERNAL://:9095 KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka-3:19092,EXTERNAL://localhost:90952 Start the cluster and create a replicated topic
ckruns a Kafka tool inside broker 1, likekafkadid for the single broker.cd cluster && docker compose up -d && cd .. ck() { docker exec -i cluster-kafka-1-1 "/opt/kafka/bin/kafka-$1.sh" --bootstrap-server kafka-1:19092 "${@:2}"; } sleep 25 ck topics --create --topic payments --partitions 3 ck topics --describe --topic paymentsYou should see:ReplicationFactor: 3,min.insync.replicas=2, and each partition withIsr:listing three brokers.3 Stop a broker
Simulate a crash. Partitions led by broker 3 elect a new leader, and broker 3 drops out of the in-sync replicas.
docker stop cluster-kafka-3-1 sleep 10 ck topics --describe --under-replicated-partitionsYou should see:All three partitions listed, each with only two brokers in
Isr.4 Keep writing
With two in-sync copies left,
acks=allwrites still succeed. If a second broker stopped, writes would fail rather than risk losing data.echo 'p1:{"amount":20}' | ck console-producer --topic payments --producer-property acks=all --property parse.key=true --property key.separator=: && echo WRITE_OKYou should see:WRITE_OK.5 Bring it back
Broker 3 catches up from the others and rejoins the in-sync replicas.
docker start cluster-kafka-3-1 sleep 20 ck topics --describe --under-replicated-partitions && echo "no under-replicated partitions"You should see:Only
no under-replicated partitions.6 Write the runbook
Ask your AI assistant to draft a runbook for "a broker is down", then correct it from what you saw. Create
notes/07-runbook.md.# Runbook: a Kafka broker is down ## Signals - Under-replicated partitions above 0: ... - Consumer lag growing: ... ## Check 1. `kafka-topics --describe --under-replicated-partitions` 2. ... ## Fix 1. ... ## Why writes kept working - Replication factor 3, min.insync.replicas 2: ... ## What the AI draft got wrong - ...7 Commit, push and stop the cluster
Stop the cluster and start the single broker again for the next lab.
git add cluster notes git commit -m "Add three-broker cluster and failure runbook" git push cd cluster && docker compose down && cd .. docker start kafka schema-registry
You are done when
With broker 3 stopped,
acks=allwrites still succeeded.After restarting it, no partitions were under-replicated.
Your runbook explains the signals, checks and fix.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.