Kafka
Brokers
High Availability
Distributed Systems
Data Streaming

Kafka What is the minimum number of brokers required for high availability?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed streaming platform that is designed to provide high-throughput, low-latency processing of real-time data feeds. Its robustness and scalability make it an ideal choice for developing real-time analytics applications. A critical component of Kafka's architecture is its use of a cluster of servers called brokers. These brokers play a key role in ensuring the high availability and durability of data within the Kafka ecosystem.

Minimum Number of Brokers for High Availability

For a Kafka system to be considered highly available, it must be resilient to node failures, capable of handling load without significant performance degradation, and ensure no data loss. Achieving this in Kafka requires a careful setup of broker configurations and an understanding of how Kafka's internal mechanisms like replication and partitioning work.

Broker Basics

A Kafka cluster consists of multiple brokers. Each broker is essentially a server that stores data and serves client requests. The data within a broker is organized into topics, and each topic can be divided into partitions. The partition allows Kafka to distribute data across multiple nodes, enhancing both scalability and fault tolerance.

Replication for High Availability

Kafka uses replication to ensure high availability. When a topic is created, it can be configured to replicate its partitions across several brokers. This means that each partition has one or more replicas spread across different brokers. The primary copy of the partition is called the leader, and the other copies are called followers. All read and write requests for a particular partition go through the leader, and the followers passively replicate the leader’s data.

For Kafka to handle failures gracefully, at least one replica of each partition must be available at all times. Therefore, the minimum number of brokers needed for high availability depends directly on the replication factor. The replication factor defines the total number of copies of each partition.

Optimal Number of Brokers

In practice, a replication factor of three is commonly used. This allows a Kafka cluster to tolerate at least one broker failure without any data loss or downtime, as the two remaining brokers would ensure continued availability and data integrity.

Thus, theoretically, the minimum number of brokers that should be configured in a Kafka cluster to achieve high availability is three. This setup ensures that if one broker goes down, the other two can continue to handle requests and maintain data integrity.

Example Configuration

Consider a Kafka cluster with three brokers and a topic with one partition:

  • Replication Factor: 3
  • Number of Brokers: 3
Broker IDPartition Copy Type
Broker 1Leader
Broker 2Follower
Broker 3Follower

In this configuration, each broker holds a copy of the partition. If Broker 1, which holds the leader replica, fails, one of the followers (Broker 2 or 3) can be automatically elected as the new leader, thus ensuring the high availability of the system.

Conclusion

Achieving high availability in Kafka is intrinsically linked to the configuration of brokers and replication factors. A minimum of three brokers is recommended to provide fault tolerance against a single broker failure. However, the actual number of brokers deployed should be based on specific use-case requirements, expected load, and tolerance for failure. Proper planning and testing are crucial elements in designing a resilient Kafka architecture.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design