Kafka
CAP Theorem
Data Management
Distributed Systems
Consistency in Databases

Why Kafka is not P in CAP theorem

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, a well-known distributed streaming platform, excels in handling high-throughput, fault-tolerant data feeds. However, its architectural design and operational objectives mean that it sits in a specific position with respect to the CAP (Consistency, Availability, Partition tolerance) theorem. Understanding why Kafka does not align with the 'P'—Partition tolerance—of the CAP theorem requires a deeper dive into how Kafka operates and the theorem itself.

Understanding the CAP Theorem

The CAP theorem, proposed by computer scientist Eric Brewer, states that a distributed system can only simultaneously provide two out of the following three guarantees:

  1. Consistency (C): Every read receives the most recent write or an error.
  2. Availability (A): Every request receives a response, without guarantee that it contains the most recent write.
  3. Partition Tolerance (P): The system continues to operate despite an arbitrary number of messages being dropped or delayed by the network between nodes.

In essence, if a network partition failure happens, a system has to choose between consistency and availability.

Kafka and the CAP Dimensions

Apache Kafka is designed primarily as a high-throughput, horizontally scalable messaging system, which tends to be configured for high availability and partition tolerance by default. Let’s examine how Kafka addresses each aspect of the CAP theorem:

  • Consistency: Kafka guarantees consistency on a per-partition basis. It uses a leader-follower model where each partition has one leader and multiple followers. The leader handles all read and write requests for the partition while followers passively replicate the leader’s data. Once data is written to the leader and replicated to the followers (up to a configured number of replicas), it is considered committed and will be consistent from then on.
  • Availability: Kafka strives to be highly available and resilient to node failures. If a leader fails, one of the followers will automatically be elected as the new leader. Using replication, Kafka ensures that as long as a partition has at least one functioning replica, the data will be available.
  • Partition Tolerance: Despite its design for distributing data across multiple servers, Kafka requires a majority of partition replicas (i.e., a quorum) to be available for the leader election and data write operations. Therefore, if more than half of the replicas for a partition become unavailable (because of network failures or node outages, for example), the partition becomes unreadable and unwritable, violating the partition tolerance property.

Technical Examples and Scenarios

In a setup where you have a Kafka cluster with three replicas for a partition, consider the following scenarios:

  1. Single Replica Failure: If one of the replicas goes down, the other two ensure the continuation of data availability and consistency.
  2. Two Replica Failures: With only one replica left, the partition is technically still available, but a new leader cannot be elected if the leader replica is one of those that failed. This results in losing both availability and consistency for that partition.

Implications of Kafka's Design

The focus for Kafka is on high availability and maintaining strong consistency when possible, accepting that during certain failure modes (particularly when more than half of the nodes are unreachable), it will sacrifice availability to maintain consistency rather than violate the atomicity of writes across the replicas of a partition.

Summary of Kafka’s Position with the CAP Theorem

Below is a table summarizing Kafka's behavior regarding the CAP dimensions:

AttributeSupportedCompromised UnderNotes
Consistency (C)YesMultiple replica failuresAlways ensures consistent replication across available replicas.
Availability (A)YesMajority of replicas downCan become unavailable if a leader cannot be elected.
Partition Tolerance (P)NoScenarios exceeding fault toleranceKafka prioritizes consistency and availability, losing write capability when a sufficient number of nodes are down.

Conclusion

Kafka, by its architectural choice, aligns primarily with consistency and availability, ensuring these as long as a quorum of nodes in the cluster is operational. It does not provide full partition tolerance, as its efficacy is bounded by the necessity of maintaining a quorum for operation, compromising availability and consistency under extensive network partitions.


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.