How to achieve strong consistency in Kafka?
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 powerful distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since it operates in a distributed environment, ensuring consistency across its components is crucial but challenging. Here, we delve into how Kafka achieves strong consistency through its architecture and various configurations.
Understanding Consistency in Kafka
Consistency in the context of distributed systems, including Kafka, commonly refers to the guarantee that subsequent operations will see the results of previous operations. For Kafka, strong consistency means that once a record is returned as committed, all subsequent consumers see it, and this property remains true even in the face of various failures.
Replication for Fault Tolerance
Kafka achieves fault tolerance and consistency through replication. Data in Kafka is stored in topics which are split into partitions. Each partition can be replicated across multiple brokers (servers) to ensure high availability and durability.
- Leader and Follower Replicas: Each partition has one leader and zero or more followers. The leader handles all read and write requests for the partition, while followers passively replicate the leader's log.
- In-Sync Replicas (ISR): When a producer sends a message, the leader appends the message to its log and waits for followers to respond that they have successfully copied the data. Only those followers that have caught up with the leader's log are considered part of the in-sync replicas set.
Configurations Impacting Consistency
- Replication Factor: It defines the number of replicas for each partition and should be set based on the durability and consistency requirements. A higher replication factor increases fault tolerance.
- Min In-Sync Replicas: This setting dictates the minimum number of replicas that must acknowledge a write for it to be considered successful (committed). If this minimum is not met, the producer receives an error.
- Producer Acknowledgements (acks): Producers can control the durability and consistency of writes:
acks=0: The producer does not wait for any acknowledgment from the server. This is the fastest but least durable setting.acks=1: The producer receives an acknowledgment as soon as the leader has written the record. This setting is faster but does not ensure the record is replicated.acks=all: This setting ensures the highest level of durability and consistency as the producer waits for all in-sync replicas to acknowledge.
Handling Failures
To maintain consistency in the case of broker failures, Kafka implements several strategies:
- Ungleaned Leader Election: Previously, Kafka always assumed that the leader of a replica set was reliable and could not fail, which was problematic. More recent versions of Kafka use a more sophisticated protocol called the
Unclean Leader Election. In this protocol, if the leader fails, one of the followers will be elected as the new leader, ensuring continuity. - Controller Broker: Kafka designates one broker as the controller, responsible for maintaining the leader status for all partitions. This controller is also in charge of electing new leaders in case the current leader fails, ensuring no data inconsistency is introduced during broker failures.
Example Configuration
Here's how one might configure a Kafka producer to enhance strong consistency:
Summary Table
| Configuration | Purpose | Impact on Consistency |
replication.factor | Number of copies of data including leader | Higher is better for fault tolerance |
min.insync.replicas | Minimum number of replicas that must acknowledge a write | Directly impacts strong consistency |
acks | Producer acknowledgment levels | all ensures highest consistency |
| Leader and Follower Configurations | Proper leader selection and follower synchronization | Crucial for maintaining data order |
By understanding and utilizing these Kafka configurations and mechanisms, developers can achieve systems that are both highly available and consistent, ensuring data reliability and integrity, even in the face of failures. This connectivity between consistency and fault tolerance is critical for building robust distributed systems.
Related reading
- How to acknowledge consume message in kafka using php-rdkafka?
- How to acknowledge current offset in spring kafka for manual commit
- How to add a header keyvalue pair when publishing a message with pika
- How to add initial users when starting a RabbitMQ Docker container?
- How to add JVM parameters to Apache Kafka?
- How to add plugin to RabbitMQ docker image?
- How to alter the TTL for a particular topic in Kafka
- How to always consume from latest offset in kafka-streams

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.