Kafka Nodes
Zookeeper
Communication
Distributed Systems
Information Exchange

How Kafka Nodes and zookeeper will communicate with each other?

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 used for building real-time data pipelines and streaming apps. It is horizontally scalable and capable of handling trillions of events a day. Initially developed by LinkedIn and later open-sourced as part of the Apache project, Kafka is written in Scala and Java. Apache Kafka requires a cluster management tool, and that's where Apache ZooKeeper comes in.

What is ZooKeeper?

Apache ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. It is used in distributed systems for synchronization, configuration management, naming, and group services.

Kafka and ZooKeeper Interaction

Functionality:

Here is how Kafka uses ZooKeeper:

  1. Broker Registration: Every Kafka broker or server in a Kafka cluster registers itself in ZooKeeper. When a new broker starts, it registers its ID and address in ZooKeeper, in the /brokers/ids path.
  2. Topic Configuration: Kafka uses ZooKeeper to store metadata about topics, including the list of existing topics, the number of partitions for each topic, location of replicas, topic configurations and which broker is the leader for a partition.
  3. Cluster Membership: ZooKeeper is used for leader election for broker partitions. It helps in electing a new leader in case of current leader failure.
  4. Access Control Lists (ACLs): For Kafka versions before 2.0, ACLs were managed via ZooKeeper. This controls access to topics on Kafka.

Technical Communication:

  1. Read and Write Operations: Kafka brokers use the ZooKeeper’s znodes (ZooKeeper data nodes) to read and write data like metadata about topics, brokers, and other configurations. Each node in the Kafka cluster communicates with ZooKeeper, which centralizes service and provides coordination.
  2. Watches: Kafka sets watches on certain znodes in ZooKeeper. For instance, Kafka brokers watch the /brokers/ids path to track when a new broker joins or leaves the cluster.

Simplified Example of Communication:

  1. Starting Up: When a Kafka server starts, it registers itself in ZooKeeper. ZooKeeper maintains a list of all active brokers.
  2. Creating a Topic: When a topic is created in Kafka, it creates certain metadata in ZooKeeper, like the number of partitions, replication factors and lists of replica locations per partition.
  3. Broker Failure: If a broker fails, this change is detected by ZooKeeper because the node associated with the broker disappears. ZooKeeper notifies other members of the Kafka cluster about the change.

Transition to Kafka Raft (KRaft) Mode

As Kafka evolves, the community is moving towards removing the ZooKeeper dependency. Kafka 2.8 introduced KRaft mode, which stands for Kafka Raft metadata mode. It aims to handle metadata about brokers, topics, and configurations within Kafka entirely, bypassing the need for ZooKeeper. This simplifies the architecture and potentially improves performance and scalability by eliminating one layer of interaction.

Summary Table

FeatureKafkaZooKeeper
RoleData StreamingCluster Coordination and Management
Responsibilities in InteractionRegisters broker, Stores log segmentsMaintains broker list, coordinate leader elections
Communication MechanismSends data about topics, partitionsListens and responds to changes, holds metadata
Failure ManagementRebalance and elect new leadersNotifies Kafka of broker failures

Conclusion

Kafka and ZooKeeper's communication is deeply ingrained into Kafka's architecture, playing a crucial role in maintaining high availability and fault tolerance. The move towards KRaft mode signifies an important evolution in Kafka's architecture, aiming to reduce complexity and enhance system responsiveness.


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

All Rights Reserved.