2.4 Understanding KRaft Mode
Learn Kafka's KRaft consensus protocol that eliminates Zookeeper dependency. Covers installation on Linux, Mac, Windows, and Docker with complete configuration examples.
KRaft Mode
Introduction
KRaft is Kafka's new consensus mechanism that removes the need for Zookeeper. This lesson covers how KRaft works, its benefits, and why it simplifies Kafka's architecture.
What is KRaft?
KRaft stands for Kafka Raft. It's a consensus algorithm built directly into Kafka, eliminating the need for Zookeeper in broker coordination.
Key Capabilities
- Leader Elections: Handles leadership decisions natively within Kafka
- Replication Management: Manages data replication between brokers
- Metadata Coordination: Stores and manages cluster metadata internally
- Simplified Architecture: Removes external dependency on Zookeeper
KRaft significantly simplifies Kafka's architecture by handling all coordination tasks internally.
How Does KRaft Work?
KRaft uses the Raft consensus algorithm to coordinate leader elections and replication directly between Kafka brokers. There's no need for an external system like Zookeeper.
The Coordination Process
Brokers communicate with each other directly:
- Each broker maintains awareness of cluster state
- When a broker goes down, KRaft ensures another broker takes over seamlessly
- The leader election process is streamlined and fully integrated into Kafka
- Metadata is replicated across controller nodes for redundancy
Failure Handling
When a broker fails:
- Other brokers detect the failure
- KRaft initiates leader election
- A new leader is elected from available brokers
- Clients are redirected to the new leader
- Normal operation resumes
Benefits of KRaft
1. Self-Managed Metadata
Kafka can now manage its own metadata without needing a separate Zookeeper cluster. Everything is handled internally, making the system more efficient and reducing operational overhead.
2. Simplified Architecture
By eliminating Zookeeper, you remove an entire component from your infrastructure:
- No separate Zookeeper cluster to maintain
- Fewer moving parts to monitor
- Reduced complexity in deployment and operations
3. Improved Scalability
KRaft simplifies Kafka's scalability, making it easier to run large-scale deployments:
- Faster metadata operations
- Better performance at scale
- More efficient resource utilization
4. Reduced Operational Overhead
Without Zookeeper:
- Fewer services to manage and monitor
- Simpler configuration
- Lower infrastructure costs
- Easier troubleshooting
KRaft in Action: Broker Failure Example
Let's examine how KRaft handles failures in a cluster with three brokers (Broker1, Broker2, Broker3).
Normal Operation
All brokers send heartbeats to each other to indicate they're alive and functioning. Unlike Zookeeper mode, there's no central coordinator - brokers coordinate among themselves.
1
2Broker1 <---> Broker2
3Broker1 <---> Broker3
4Broker2 <---> Broker3When Broker1 Fails
#### Step 1: Detecting the Failure
Broker1 stops responding. Broker2 and Broker3 detect the missed heartbeat and recognize that Broker1 is down.
#### Step 2: Initiating Leader Election
Both Broker2 and Broker3 check heartbeat status and initiate the leader election process among themselves.
#### Step 3: Electing New Leader
Through the Raft consensus algorithm, Broker2 is elected as the new leader.
#### Step 4: Client Redirection
When a client tries to connect to Broker1:
- Client sends request to Broker1
- Broker1 is unresponsive
- Client requests metadata from other brokers
- Client discovers Broker2 is the new leader
- Client redirects requests to Broker2
#### Step 5: Continued Operation
The cluster remains operational with Broker2 handling requests, ensuring no disruption to service.
Sequence of Events
- Missed Heartbeat: Broker1 stops sending heartbeats
- Detection: Broker2 and Broker3 detect the failure
- Election Start: Both brokers initiate leader election
- Leader Ready: Broker2 becomes ready to lead
- Leader Elected: Broker2 is officially elected
- Client Request: Client attempts to reach Broker1
- No Response: Broker1 doesn't respond
- Metadata Request: Client asks for cluster metadata
- Redirection: Broker2 provides updated information
- New Request: Client sends requests to Broker2
This process ensures the cluster stays operational without disruption, even when brokers fail.
KRaft vs Zookeeper
| Aspect | Zookeeper Mode | KRaft Mode |
|--------|---------------|------------|
| External Dependencies | Requires Zookeeper cluster | Self-contained |
| Architecture | Two separate systems | Single integrated system |
| Operational Complexity | Higher (two systems to manage) | Lower (one system) |
| Metadata Management | External (Zookeeper) | Internal (Kafka) |
| Scalability | Good | Better |
| Failover Speed | Fast | Faster |
Installing Kafka in KRaft Mode
Prerequisites
Before installing, ensure you have:
- Java Development Kit (JDK) 8 or higher (
java -versionto check) - Platform-specific tools:
- Windows: Git Bash or PowerShell
- Mac: Homebrew package manager
- Linux: Default package manager (apt/yum)
- Docker: Docker and Docker Compose (for containerized setup)
Linux Installation
- Download and Extract Kafkabash
1 mkdir -p /opt/kafka 2 wget https://downloads.apache.org/kafka/3.8.0/kafka_2.13-3.8.0.tgz 3 tar -xvzf kafka_2.13-3.8.0.tgz -C /opt/kafka --strip-components=1 - Create Log Directorybash
1 mkdir /tmp/kraft-combined-logs - Configure server.propertiesbash
1 cat <<EOL > /opt/kafka/config/kraft/server.properties 2 process.roles=broker,controller 3 node.id=1 4 controller.quorum.voters=1@localhost:9093 5 listeners=PLAINTEXT://localhost:9092,CONTROLLER://localhost:9093 6 log.dirs=/tmp/kraft-combined-logs 7 EOLConfiguration explained:
-
process.roles=broker,controller: Node serves both roles-
node.id=1: Unique node identifier-
controller.quorum.voters: Defines controller quorum voters-
listeners: Sets up client (9092) and controller (9093) listeners-
log.dirs: Directory for storing Kafka logs - Format Storagebash
1 /opt/kafka/bin/kafka-storage.sh format -t $(uuidgen) -c /opt/kafka/config/kraft/server.properties - Start Kafkabash
1 /opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/kraft/server.propertiesMac Installation
- Install Kafka via Homebrewbash
1 brew install kafka - Create Log Directorybash
1 mkdir /tmp/kraft-combined-logs - Configure server.propertiesbash
1 cat <<EOL > /usr/local/etc/kafka/server.properties 2 process.roles=broker,controller 3 node.id=1 4 controller.quorum.voters=1@localhost:9093 5 listeners=PLAINTEXT://localhost:9092,CONTROLLER://localhost:9093 6 log.dirs=/tmp/kraft-combined-logs 7 EOL - Format Storagebash
1 /usr/local/bin/kafka-storage format -t $(uuidgen) -c /usr/local/etc/kafka/server.properties - Start Kafkabash
1 kafka-server-start /usr/local/etc/kafka/server.propertiesWindows Installation
- Create Directory and Extractbash
1 mkdir C:\kafka 2 tar -xvzf kafka_2.13-3.8.0.tgz -C C:\kafka - Create Log Directorybash
1 mkdir C:\tmp\kraft-combined-logs - Configure server.propertiesbash
1 cat <<EOL > C:\kafka\config\kraft\server.properties 2 process.roles=broker,controller 3 node.id=1 4 controller.quorum.voters=1@localhost:9093 5 listeners=PLAINTEXT://localhost:9092,CONTROLLER://localhost:9093 6 log.dirs=C:\tmp\kraft-combined-logs 7 EOL - Format Storagebash
1 C:\kafka\bin\windows\kafka-storage.bat format -t $([guid]::NewGuid()) -c C:\kafka\config\kraft\server.properties - Start Kafkabash
1 C:\kafka\bin\windows\kafka-server-start.bat C:\kafka\config\kraft\server.propertiesDocker Installation
#### Create docker-compose.yaml
Create a directory for Kafka and prepare the Docker configuration:
yaml1version: '3.8' 2services: 3 kafka-init: 4 image: confluentinc/cp-kafka:7.5.0 5 command: > 6 bash -c "kafka-storage format -t $(kafka-storage random-uuid) 7 -c /etc/kafka/server.properties && echo 'Storage formatted successfully'" 8 volumes: 9 - ./kafka-logs:/var/lib/kafka/data 10 environment: 11 KAFKA_NODE_ID: 1 12 KAFKA_PROCESS_ROLES: broker,controller 13 KAFKA_LISTENERS: PLAINTEXT://localhost:9092,CONTROLLER://localhost:9093 14 KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093 15 KAFKA_LOG_DIRS: /var/lib/kafka/data 16 17 kafka: 18 image: confluentinc/cp-kafka:7.5.0 19 ports: 20 - "9092:9092" 21 volumes: 22 - ./kafka-logs:/var/lib/kafka/data 23 environment: 24 KAFKA_NODE_ID: 1 25 KAFKA_PROCESS_ROLES: broker,controller 26 KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093 27 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092 28 KAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka:9093 29 KAFKA_LOG_DIRS: /var/lib/kafka/data 30 depends_on: 31 - kafka-init#### Key Environment Variables
KAFKA_PROCESS_ROLES: Defines roles as broker and controllerKAFKA_CONTROLLER_QUORUM_VOTERS: Identifies nodes for controller quorumKAFKA_LISTENERS: Sets up listeners for client and controller communicationsKAFKA_LOG_DIRS: Points to log storage directory
#### Start Services
bash1docker-compose up -dThe
-dflag runs services in detached mode (background).
Summary
KRaft represents the future of Kafka architecture:
- Simpler deployment: No Zookeeper cluster needed
- Better performance: Native metadata management
- Easier operations: Single system to manage
- Improved scalability: Optimized for large-scale deployments
- Easy installation: Available on all major platforms
By embedding coordination directly into Kafka, KRaft reduces complexity while maintaining all the reliability and fault tolerance that Kafka is known for.