At its core, Apache Kafka is built around a simple idea: an append-only log of records that can be stored, replicated, and read by multiple applications independently.
This design is what makes Kafka different from traditional message queues. Instead of treating messages as temporary items that disappear once they are processed, Kafka stores them in an ordered log and allows consumers to read them at their own pace.
To understand how Kafka works internally, we first need to understand what a commit log is and why Kafka uses it.
A commit log is an ordered sequence of records where new records are added to the end.
Imagine an application that processes customer orders. Every time something happens to an order, the application writes an event:
This is a simplified example of a log.
Each record represents something that happened, and records are stored in the order they were appended.
A commit log has three important properties.
When a new event arrives, it is added to the end of the log. Kafka doesn't need to find a particular location in the middle of the log to insert it.
Once a record is written, its contents are not updated in place.
For example, if an order changes from CREATED to SHIPPED, we don't modify the original event. Instead, we append a new event describing the change.
This gives us a history of what happened.
Kafka identifies this position using an offset.
Offsets allow consumers to track their progress and resume reading from a particular position.
These three properties form the foundation of Kafka's storage model.
In Kafka, producers publish records to topics.
A topic is a logical category of records, such as orders, payments, or user-events.
However, Kafka doesn't store an entire topic as one giant log. Instead, each topic is divided into one or more partitions.
Each partition is an independent, ordered, append-only log.
For example, an orders topic with three partitions might look like this:
Each partition maintains its own sequence of offsets.
Offset 2 in partition 0 is completely different from offset 2 in partition 1.
Kafka guarantees ordering within a partition, not across an entire topic. If records A, B, and C are appended to the same partition in that order, consumers reading that partition see them in that order, but there is no single global ordering across partitions.
Partitions allow Kafka to distribute data and workload across multiple brokers.
Instead of one machine handling every record, different partitions can be hosted on different machines.
Partitions also enable parallel processing. Multiple consumers in the same consumer group can process different partitions simultaneously.
For example, suppose we publish 100,000 order events per second.
If all events go to one partition, that partition becomes a potential bottleneck.
With multiple partitions, the workload can be distributed across multiple brokers and consumers.
The trade-off is that ordering is maintained only within each partition.
This is why choosing a partitioning strategy matters.
For order events, we might use order_id as the record key. With a consistent key-based partitioning strategy, events belonging to the same order are routed to the same partition.
That allows us to preserve their order while processing different orders in parallel.
So far, we've described an append-only log. What makes Kafka a distributed commit log?
The answer is that Kafka distributes partitions across multiple brokers and can maintain replicas of each partition for fault tolerance.
A Kafka broker is a server responsible for storing partition data and serving producer and consumer requests.
Consider a Kafka cluster with three brokers and a topic configured with a replication factor of three.
For one partition, the replicas might be arranged as follows:
Each partition has one leader replica and, when replication is configured, one or more follower replicas.
The leader handles normal writes for that partition.
When a producer sends a record, the leader appends it to its local log and followers replicate the data from the leader.
If the leader fails, Kafka can elect an eligible follower as the new leader.
This allows the partition to remain available even when individual brokers fail, provided the cluster has sufficient healthy replicas.
Kafka also tracks which records have been successfully replicated.
The high watermark identifies the boundary up to which records are committed in a partition. Records below this boundary are available to ordinary consumers, subject to their isolation settings.
For stronger durability, producers commonly use acks=all together with an appropriate min.insync.replicas configuration.
This requires the record to be acknowledged by the necessary in-sync replicas before the producer receives a successful response.
Replication reduces the risk of losing data when a broker fails. However, a successful acknowledgment does not necessarily mean every replica has physically flushed the record to disk.
The important idea is that Kafka combines an ordered log with replication to provide fault-tolerant storage.
One of the biggest differences between Kafka and traditional message queues is what happens after a consumer reads a record.
In many traditional queues, a message is removed once it has been successfully processed and acknowledged.
Kafka works differently.
Reading a record does not remove it from the log.
Suppose an orders partition contains 1,000 records.
A consumer reads the first 500 records and successfully processes them.
Those records are still stored in Kafka.
Another consumer can read those same records independently, assuming they are still retained.
This is possible because Kafka separates two responsibilities:
Kafka doesn't need to delete a record simply because one application has finished processing it.
This separation is incredibly useful when multiple applications need the same data.
Imagine an e-commerce application that publishes order events to Kafka.
Three independent services consume those events:
All three services can read from the same orders topic.
The inventory service might be caught up with the latest records, while the analytics service is several thousand records behind.
Neither service blocks the other.
Each consumer group tracks its own progress, so applications can process the same data independently.
This makes Kafka useful not only for messaging but also for connecting multiple systems through a shared stream of events.
Since records remain in the log after consumption, Kafka needs a way to track how far each consumer has progressed.
This is where offsets come in.
Suppose a partition contains records with offsets from 0 to 999.
A consumer has processed records through offset 499.
It can commit offset 500 to indicate that the next record it should consume is at offset 500.
If the consumer crashes and restarts, it can resume from its committed position.
Kafka stores committed consumer group offsets in an internal topic called __consumer_offsets.
The important distinction is that the consumer's committed offset is separate from the records stored in the partition.
The records themselves are not changed when the consumer makes progress.
Kafka consumers can also deliberately move their reading position backward.
For example, suppose a bug in your analytics service caused incorrect calculations for yesterday's orders.
After fixing the bug, you can reset the consumer group's offsets and process those records again, provided Kafka still retains them.
You don't need the original producers to send the events again.
This ability to replay data is one of Kafka's most valuable features.
It is useful for recovering from processing bugs, rebuilding application state, and developing new services that need historical events.
However, replay also means applications must consider duplicate processing.
If an event causes an external side effect, such as charging a customer, processing it again must not accidentally repeat that action. Techniques such as idempotency help make replay safe.
If Kafka doesn't delete records after consumption, does that mean its logs grow forever?
Not necessarily.
Kafka uses configurable retention policies to control how long records remain available.
The two main approaches are:
Time-based retention: Kafka removes old log segments after their records exceed the configured retention period, subject to its cleanup rules.
Size-based retention: Kafka removes older log segments when a partition's retained log exceeds a configured size limit.
For example, a topic might retain records for seven days.
During those seven days, consumers can read or replay available records regardless of whether other consumers have already processed them.
After retention removes the records, they are no longer available for replay.
Kafka also supports log compaction, which can retain the latest value for each key rather than preserving every historical update indefinitely.
Kafka's retention is independent of consumer progress. A record can be deleted even if a consumer hasn't processed it yet, and it can remain available long after every consumer has finished processing it.
The distributed commit log model gives Kafka several useful properties.
Efficient writes: New records are appended to logs, allowing Kafka to make extensive use of sequential disk I/O and batching instead of performing arbitrary updates throughout a data structure.
Independent consumers: Applications can process records at different speeds without interfering with one another.
Replayability: Consumers can revisit retained records to recover from failures or rebuild application state.
Scalability: Partitions distribute storage and processing workloads across multiple brokers and consumers.
Fault tolerance: Replicated partitions allow Kafka to recover from broker failures while preserving committed data under its configured replication guarantees.
These properties explain why Kafka works well for event-driven architectures, data pipelines, change data capture, stream processing, and systems that need to distribute large volumes of events reliably.
There are also trade-offs.
Partitioning means Kafka doesn't offer global ordering across a topic. Retention means records aren't always available forever. And because records can be processed more than once, consumers must handle retries and duplicate processing appropriately.
Understanding these trade-offs is just as important as understanding Kafka's strengths.
The most important thing to understand about Kafka is that it isn't simply a system that moves messages from producers to consumers.
Kafka stores ordered streams of records in partitioned, replicated logs. Consumers independently decide where to read from those logs.
This simple separation between storage and consumption is what enables Kafka's scalability, fault tolerance, and replay capabilities.
10 quizzes