Kafka - This server is not the leader for that topic-partition
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 distributed streaming platform capable of handling trillions of events a day, providing functionalities like high-throughput publishing and subscribing to streams of records, storing streams of records efficiently, and processing streams as they occur. Kafka is designed to be durable, scalable, and fault-tolerant. On this platform, data is stored in topics which are split across a number of partitions. This partitioning of topics not only allows for scalability but also introduces the complexity of managing data consistency and availability.
When interacting with Kafka, users may occasionally encounter the error: "This server is not the leader for that topic-partition." This error indicates that the client has attempted to write to or read from a partition for which the broker it connected to is not the leader. Understanding why this occurs and how Kafka manages partitions and leaders is crucial for effective troubleshooting and system design.
Kafka Basic Architecture
To comprehend the error message, one must understand two core components of Kafka’s architecture: topics and partitions.
- Topics: A logical channel to which producers send data and from which consumers read data. Each topic can be subdivided into multiple partitions.
- Partitions: These are append-only logs where the records are stored. Partitions allow a topic’s data to be scaled by splitting the data across multiple brokers.
Each partition has one server acting as the leader and zero or more servers acting as followers. The leader handles all read and write requests for the partition, while the followers replicate the data of the leader. This setup provides redundancy and fault tolerance.
What Does the "Not the Leader" Error Mean?
When an application tries to perform a read or write operation on a partition and connects to a broker that isn't the leader for that particular partition, Kafka responds with the "This server is not the leader for that topic-partition" error.
Causes and Resolution of "Not the Leader" Error
Causes:
- Re-elections: Leaders can change due to broker failures or network partitions. During this leadership change, there may be a brief period when there is no clear leader.
- Stale Metadata: The client may have outdated metadata, pointing to an old leader.
Resolution:
- Metadata Refresh: Clients should refresh their metadata upon encountering this error. Most client libraries handle this automatically.
- Error Handling: Enhance client error handling to retry requests with a backoff strategy.
Example Scenario
Consider a scenario where you have a Kafka cluster with three brokers and a topic, "transactions", which has three partitions:
- Partition 1 is led by Broker A.
- Partition 2 is led by Broker B.
- Partition 3 is led by Broker C.
If Broker A crashes, Kafka’s controller will promote a new leader from the eligible set of followers for Partition 1. If a producer tries to write to Partition 1 in this transition period, it might hit a broker that believes Broker A is still the leader, resulting in the described error.
Key Points Summary
Here is a summary of key points from the topic discussed:
| Key Concept | Description |
| Topic | A category or feed name to which records are stored and published. |
| Partition | A split of the topic log to allow for scalability; each partition has one leader at any time. |
| Leader | The broker responsible for all reads and writes for the partition. Manages replication to followers. |
| "Not the Leader" Error | Occurs if a client interacts with a non-leader broker for a partition. |
| Resolution | Refresh client metadata, implement retry mechanisms. |
Further Considerations
- Monitoring and Alerting: Implement monitoring to catch and alert on frequent leader changes or "not the leader" errors, as these might indicate bigger issues like network instability or broker failures.
- Client Configuration: Standard client configurations often include automatic retries and metadata updates, but understanding and tuning these settings can improve resilience and performance.
By keeping these aspects in mind, users and administrators can better manage Kafka systems and reduce the impact of "not the leader" errors, ensuring smoother operations and reliability of the Kafka ecosystem.
Related reading
- Kafka - Unable to send a message to a remote server using Java
- Kafka - What are the better alternatives than poll() to listen to a topic in Java?
- Kafka - why new topic partition leader is not elected?
- Kafka 0.10 Java Client TimeoutException Batch containing 1 record(s) expired
- Kafka 0.8, is it possible to create topic with partition and replication using java code?
- kafka ack=all and min-isr
- kafka 0.9.0.1 fails to start with fatal exception
- Kafka 1.0 stops with FATAL SHUTDOWN error. Logs directory failed

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.