How to build efficient Kafka broker healthcheck?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a highly popular distributed streaming platform known for its capability to handle large volumes of real-time data. A crucial component of maintaining a Kafka cluster is ensuring the health and performance of its brokers. This article discusses building an efficient health check system for Kafka brokers, covering technical explanations and practical examples.
Understanding Kafka Broker Health
A Kafka broker is a server in the Kafka cluster that is responsible for maintaining published data. Each broker may handle data for multiple partitions of multiple topics. The health of a broker can generally be determined by its ability to:
- Accept connections from producers and consumers.
- Handle read and write requests effectively.
- Sync with other brokers (if it is a part of a multi-broker setup).
- Stay up to date with the controller (the broker responsible for maintaining the leader/follower relationship).
Key Metrics for Broker Health Checks
Monitoring certain metrics can give a good indication of the health of a Kafka broker:
- Broker Uptime: Duration since the broker started; a sudden reset may indicate issues.
- Request Rates: Rate at which read and write requests are made.
- Error Rates: Rates at which requests result in errors.
- Under Replicated Partitions: Number of partitions for which the broker is not an in-sync replica.
- Consumer Lag: How far behind consumers are lagging on a broker; critical for ensuring real-time processing.
- Resource Utilization: CPU usage, memory usage, disk I/O, and network I/O.
Implementing Health Checks
Step 1: Basic Connectivity Test
A simple way to start is to check if the broker is up and running. This can be performed by trying to establish a socket connection to the broker on its configured port.
Step 2: Advanced Metrics Collection
For more sophisticated health checks, use Kafka's own metrics provided via JMX (Java Management Extensions) or the Jolokia HTTP bridge.
Step 3: Automation and Monitoring Integration
Health checks should be automated and set up to run at regular intervals. Use monitoring tools like Prometheus, along with its exporter (such as JMX Exporter for Kafka), to scrape and store these metrics. Alerting can then be configured based on these metrics.
Alerts and Thresholds
Configure alerts for critical thresholds like high error rates, high consumer lag, or high number of under-replicated partitions. Here's how you might set it up with Prometheus Alertmanager:
Summary Table
| Metric | Description | Ideal Value |
| Broker Uptime | Time since broker was last (re)started. | Consistent |
| Request Rates | Volume of read/write requests per second. | Stable/Expected |
| Error Rates | Number of errors per second. | Low |
| Under Replicated Partitions | Number of out-of-sync partitions. | 0 |
| Consumer Lag | Lag in message consumption. | Minimal |
| Resource Utilization | CPU, memory, and I/O usage. | Within capacity |
Conclusion
Building an efficient health check system for Kafka brokers involves tracking several critical metrics that reflect the performance and stability of your Kafka brokers. Utilizing tools like socket programming for basic checks, JMX or Jolokia for advanced metrics extraction, and integrating with monitoring and alert systems like Prometheus can dramatically help in maintaining the health of your Kafka cluster. Like any distributed system, regular maintenance and proactive health checks are key to ensuring high availability and performance.
Related reading
- How to calculate or approximate the median of a list without storing the list
- How to catch deserialization error in Kafka-Spring?
- How to change default port(15672) of RabbitMQ Management plugin?
- how to change Kafka broker list ip
- how to build singularity container from dockerfile
- How to calculate containers' cpu usage in kubernetes with prometheus as monitoring?
- How to change RabbitMQ Heartbeat without restart
- How to change the name of the topic generated by Kafka Connect Source Connector

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.