Kafka
Consistency
Data Streaming
System Architecture
Message Queueing

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.

Practice system design

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

  1. 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.
properties
    replication.factor=3
  1. 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.
properties
    min.insync.replicas=2
  1. 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:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "kafka-broker1:9092,kafka-broker2:9092");
3props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
4props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
5props.put("acks", "all");
6props.put("replication.factor", "3");
7props.put("min.insync.replicas", "2");
8
9Producer<String, String> producer = new KafkaProducer<>(props);

Summary Table

ConfigurationPurposeImpact on Consistency
replication.factorNumber of copies of data including leaderHigher is better for fault tolerance
min.insync.replicasMinimum number of replicas that must acknowledge a writeDirectly impacts strong consistency
acksProducer acknowledgment levelsall ensures highest consistency
Leader and Follower ConfigurationsProper leader selection and follower synchronizationCrucial 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
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.