0
0
KafkaDebug / FixBeginner · 4 min read

How to Fix 'Leader Not Available' Error in Kafka

The leader not available error in Kafka happens when the broker responsible for a partition leader is down or not ready. To fix it, ensure all Kafka brokers are running and the topic partitions have elected leaders by checking broker health and restarting if needed.
🔍

Why This Happens

This error occurs because Kafka clients try to send messages to a partition leader that is not currently assigned or available. This usually happens if the Kafka broker that was the leader crashed, is offline, or has not finished electing a new leader for the partition.

When Kafka starts, it needs time to elect leaders for each partition. If a client tries to produce or consume before this process finishes, it will get the leader not available error.

bash
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-topic
> This message will fail
Output
ERROR Error when sending message to topic my-topic with key: null, partition: 0, leader not available
🔧

The Fix

First, check that all Kafka brokers are running and connected to Zookeeper or Kafka's internal metadata system. Restart any stopped brokers.

Next, verify the topic exists and has partitions with leaders assigned. Use Kafka's command-line tools to describe the topic and confirm leader status.

If leaders are missing, restarting brokers or reassigning partitions can help Kafka elect new leaders.

bash
bin/kafka-topics.sh --describe --topic my-topic --bootstrap-server localhost:9092
Output
Topic: my-topic PartitionCount: 1 ReplicationFactor: 1 Configs: Topic: my-topic Partition: 0 Leader: 1 Replicas: 1 Isr: 1
🛡️

Prevention

To avoid this error in the future, always ensure your Kafka cluster is healthy with all brokers running and communicating properly.

  • Use monitoring tools to track broker status and partition leaders.
  • Configure proper replication and fault tolerance so leaders can failover quickly.
  • Wait for leader election to complete before producing or consuming messages after broker restarts.
⚠️

Related Errors

Other errors similar to leader not available include:

  • NotLeaderForPartition: Happens when a client sends data to a broker that is not the current leader.
  • BrokerNotAvailable: Occurs if the broker is down or unreachable.
  • TimeoutException: When leader election or broker response takes too long.

Fixes usually involve checking broker health and topic partition assignments.

Key Takeaways

Ensure all Kafka brokers are running and connected to avoid leader unavailability.
Check topic partition leaders with Kafka tools before producing or consuming messages.
Restart brokers or reassign partitions if leaders are missing to trigger new elections.
Monitor cluster health to prevent leader-related errors.
Wait for leader election to complete after broker restarts before client operations.