During rolling upgrade/restart, how to detect when a kafka broker is done?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When administering an Apache Kafka cluster, rolling upgrades and restarts are common tasks that help ensure that your brokers are running the latest software version without incurring downtime. Detecting when a Kafka broker is fully operational following a restart is crucial to the seamless continuation of service within your Kafka ecosystem. This includes ensuring that the broker has adequately rejoined the cluster and is capable of handling its share of partition leadership and traffic.
Broker Startup and Initialization Process
When a Kafka broker restarts, it undergoes several steps before it can be considered fully operational:
- Configuration Load: The broker loads its configuration settings from server.properties or dynamic configuration.
- Log Recovery: The broker reads its local log directories to recover and load topic partitions.
- Joining the Cluster: The broker connects to the Kafka cluster using the Zookeeper (for older versions) or through the Quorum (KRaft mode in newer versions).
- Partition Reassignment: If the broker is set to handle certain partitions, those partitions must be reassigned to it, which involves leader elections if necessary.
Methods to Detect Broker Readiness
1. Cluster Metadata:
Using Kafka command-line tools such as kafka-topics or kafka-broker-api-versions, you can check whether the broker is listed in the cluster metadata. This indicates that the broker has successfully joined the cluster.
2. JMX Metrics:
Kafka brokers expose various metrics via Java Management Extensions (JMX). You can monitor specific JMX metrics to determine broker status:
kafka.server:type=KafkaServer,name=BrokerStateThis MBean provides the current state of the broker. A value ofRUNNING(3) indicates that the broker is operational.kafka.server:type=ReplicaManager,name=UnderReplicatedPartitionsThis shows the count of under-replicated partitions. A value of zero ideally indicates that all partitions are fully replicated and the broker is serving traffic normally.
3. Logs:
Broker logs provide detailed information about its status. Messages indicating that the broker is connected to Zookeeper, or in KRaft mode, messages about controller election, signify that the broker is part of the cluster and ready to take or hand over partition leadership.
4. Health Checks:
Implementing periodic health checks that query the broker's status can help determine its operational state. These checks might involve making some metadata requests to the broker or simply ensuring it’s responsive to network requests.
Practical Examples
- Command Line Check:
This command lists the API versions that the broker supports, indirectly confirming if the broker is operational.
- JMX Check Using JConsole: Connect to the broker's JMX port using JConsole (or any JMX client) and monitor the
BrokerStateandUnderReplicatedPartitionsmetrics.
Table Summary of Key Detection Methods
| Method | Tool/Approach | Indicator of Readiness |
| Cluster Metadata | Kafka CLI Tools | Broker listed in cluster metadata |
| JMX Metrics | JConsole, JMXTrans | BrokerState at RUNNING, Low UnderReplicatedPartitions |
| Logs | Log files | Logs indicating successful cluster join |
| Health Checks | Custom scripts, monitoring tools | Responsive to network requests |
Additional Considerations
- Automation: Tools like Ansible, Puppet, or Chef can be used for automation of these checks during the rolling updates.
- Monitoring Systems: Integration with systems like Prometheus or Nagios to continuously monitor these metrics can provide real-time health status of the broker.
- Rebalance Listener: Attach listeners to monitor and react to partition rebalance events. This can give more granular control over when a broker is considered ready.
Properly detecting when Kafka brokers are fully operational after a restart is vital for maintaining the resilience and reliability of the Kafka ecosystem. Using a combination of CLI tools, JMX metrics, logs, and custom health checks ensures that your brokers are correctly handling their designated workloads and are stable post-restart.
Related reading
- Dynamic addition of queues to a rabbit listener at runtime
- Dynamic periodic tasks - alternatives to Celery beat
- Dynamic queue creation with RabbitMQ
- Dynamic Topic Name / Quarkus SmallRye Reactive Messaging Kafka
- dyld__abort_with_payload Without an error message
- dyld Library not loaded rpath/libswift_stdlib_core.dylib
- Dynamically changing the instanceindex with spring cloud stream kafka
- Dynamically connecting a Kafka input stream to multiple output streams

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.