Kafka began as an internal LinkedIn project for moving activity data, such as page views and clicks, from the website to everything that needed it. Its authors threw away most of what a message broker was expected to do and kept one data structure: a file you only ever append to. That decision let a single machine publish hundreds of thousands of messages a second in their 2011 tests, and the reasons are operating systems fundamentals that any student can follow.
This post follows the 2011 paper by Jay Kreps, Neha Narkhede and Jun Rao, and Kreps's later essay on logs. Kafka has grown a great deal since, so treat the details here as the original design, and the ideas as the part that lasted.
The problem: activity data outgrew the tools
The paper starts with a kind of data it calls log data: user activity events such as logins, page views, clicks, likes and searches, plus operational metrics from every machine. This data used to be something analysts looked at later. By the time of the paper it was feeding live features directly: search relevance, recommendations, advertising, abuse detection and the news feed.
The difficulty is volume. One page view can generate a record for every item shown on the page, clicked or not, so activity data is orders of magnitude larger than the data users deliberately create. LinkedIn looked at two families of existing systems and found neither fitted.
Enterprise message brokers, the kind built around the JMS standard, offered rich delivery guarantees such as transactions across queues and per-message acknowledgement. The paper argues that these were excessive for log data, where losing an occasional page view is tolerable, and that they cost throughput. It gives specific complaints: no way in the JMS API to batch several messages into one request, weak support for spreading a queue over many machines, and performance that fell away when unread messages piled up, which is exactly what happens when a nightly batch job is the consumer.
Log aggregators such as Facebook's Scribe, Yahoo's Data Highway and Cloudera's Flume were built to collect log files and load them into Hadoop or a data warehouse for offline analysis. Most pushed data at consumers, and they were not designed for applications that wanted events within a few seconds.
LinkedIn needed both at once. In his 2013 essay, Kreps describes the wider mess behind this. LinkedIn was moving off one central database on to many specialised systems: search, a social graph store, a key-value store, Hadoop and others. Each one needed data from the rest. If every pair of systems gets its own custom pipeline, the number of pipelines grows roughly with the square of the number of systems. A single shared pipeline that every system writes to and reads from brings that back to one connection per system.
The central idea: a log
Kreps defines a log as an append-only sequence of records ordered by time. New records go on the end. Nothing in the middle is ever changed. Each record's position identifies it. This is the same structure a database uses internally as its commit log, and Kafka's bet was to make it the whole product.
The vocabulary from the paper:
- A topic is a named stream of messages of one kind.
- A producer publishes messages to a topic. A message is just a payload of bytes.
- Brokers are the servers that store the messages.
- A topic is divided into partitions, and each partition is one log.
- A consumer pulls messages from the brokers.
A message has no separate ID. It is addressed by its offset in the partition's log. In the 2011 design the offset was a byte position, so the next message's offset is the current offset plus the current message's length. That removed the need for an index mapping IDs to disk locations, which the authors point out would have meant random disk access on every lookup.
A consumer's request is correspondingly simple: give me up to this many bytes from this partition, starting at this offset.
Storage: append to a file and trust the page cache
On the broker, a partition is a set of segment files of roughly equal size, around 1 GB in the paper's example. A publish appends to the last segment. The broker keeps a small sorted list in memory holding the first offset of each segment, so finding the right file for any offset is a search over that list.
Two properties of this layout matter for speed.
First, all access is sequential. Producers write at the end. Consumers read forwards from wherever they are. On a spinning disk the expensive operation is moving the head to a new track, and a sequential workload almost never has to. Even on a solid state drive, sequential access is what the operating system is best at predicting.

Second, Kafka does not keep its own cache of messages in the process. The paper calls this an unconventional choice. It relies on the operating system's page cache, the memory the kernel already uses to hold recently read and written file pages. The authors list the benefits:
- No double buffering. Data sits in memory once, in the page cache, not once there and again in the application.
- The cache survives a restart of the broker process, because it belongs to the kernel.
- Kafka runs on the JVM, and a process that holds very little data on its heap spends very little time in garbage collection.
- Because consumers usually read just behind producers, the kernel's ordinary caching heuristics, read-ahead in particular, work well without tuning.
The paper reports that production and consumption performance stayed consistent as stored data grew to many terabytes. A queue that does not slow down as it fills is what lets the same system serve a live consumer and a nightly batch job.
One trade-off sits here. For speed, the 2011 broker flushed a segment to disk only after a configurable number of messages or amount of time, and only showed messages to consumers after the flush.
Batching and zero-copy on the network path
Sending one small message per network round trip wastes most of the effort on overhead. Kafka batches at both ends. A producer can send a set of messages in one request, and a consumer's fetch returns many messages at once, typically hundreds of kilobytes.
The paper measured the effect on a single producer sending 200-byte messages: about 50,000 messages a second one at a time, and about 400,000 a second in batches of 50. The storage format was also lean, with about 9 bytes of overhead per message against 144 in ActiveMQ, one of the two brokers they compared it with.
The second optimisation is about copying. The usual way for a server to send a file over a socket is to read it into a buffer and write the buffer to the socket. The paper counts what that really does: the data goes from disk to the page cache, from the page cache to the application's buffer, from that buffer to another kernel buffer, and from there to the socket. That is four copies and two system calls, and two of the copies exist only to carry bytes into user space and straight back out.
Linux and other Unix systems provide sendfile, a system call that tells the kernel to move bytes from a file to a socket directly. The paper says this avoids two of the copies and one of the system calls. It works for Kafka because the broker never needs to look inside the messages it serves. The bytes on disk are already in the form the consumer expects.
The broker does not track what you have read
In a traditional broker, the server records which messages each consumer has acknowledged. Kafka turns this around: the consumer keeps track of its own offset. The paper calls the result a stateless broker.
This creates an obvious problem. If the broker does not know who has read what, when can it delete anything? Kafka's answer is a time limit. Messages are kept for a fixed period, typically 7 days in the paper, and then the old segment files are removed, whether or not anyone read them.
The side benefit turned out to be one of Kafka's defining features. A consumer can deliberately move its offset backwards and read old data again. If a bug in a consumer corrupted its output, you fix the bug and replay. A classic queue, where a message is gone once acknowledged, cannot do this.
For scaling out consumers, the paper introduces consumer groups. Each message in a topic is delivered to one consumer within a group, while separate groups each see everything. The unit of parallelism is the partition: at any moment a partition is read by only one consumer in a group. That avoids locking between consumers, and it is why Kafka guarantees order within a partition but not across partitions. Coordination, such as detecting that a consumer has died and reassigning its partitions, was done through ZooKeeper. Delivery was at least once: after a crash, the consumer that takes over may see some messages again.
What the 2011 design gave up, and what changed
The paper is direct about its limits:
- The producer did not wait for an acknowledgement, so a message could be lost without the sender knowing.
- There was no replication. If a broker went down, its unread messages were unavailable until it returned, and if its disk was destroyed they were lost.
- There were no transactions and no exactly-once delivery.
The authors also caution that their benchmark was not meant to show the other brokers were inferior, since those had more features. The point was what a specialised system could gain.
At the time of the paper, Kafka had been in production at LinkedIn for more than six months, carrying hundreds of gigabytes and close to a billion messages a day, with an average end-to-end delay of about 10 seconds into the offline Hadoop pipeline. Replication and stream processing were listed as future work. Kafka was open sourced in early 2011 and graduated from the Apache Incubator in October 2012.
By Kreps's essay in December 2013, each partition was replicated with one replica acting as leader, a feature called log compaction could keep just the latest record per key, and LinkedIn was writing over 60 billion unique messages a day through Kafka.
The computer science underneath
The log as a data structure. An append-only, totally ordered sequence is the simplest durable structure there is, and it is the same idea as a database's write-ahead log. Kreps's essay points out that if two deterministic processes read the same log in the same order they end in the same state, which is the basis of replication. The lesson on Kafka and log-based streaming covers partitions, offsets and consumer groups, and message queues and async work shows the classic queue model Kafka departed from.
The page cache. Every read() on a file goes through kernel memory first, and the kernel keeps those pages around in case they are wanted again. The virtual file system and the page cache traces one read through the kernel. Kafka's design is a decision to let that existing cache do the job.
Zero-copy I/O. Each crossing between user space and kernel space costs a copy and a mode switch. Zero-copy I/O: sendfile and splice counts the copies exactly as the Kafka paper does.
Putting it together. Design a distributed message queue works through segments, replication and failover as an interview design problem.
What an interviewer could ask from this
Why is Kafka fast if it writes everything to disk? It only appends and reads sequentially, it lets the OS page cache hold hot data, it batches messages, and it sends file data to sockets without copying it through the application.
How does Kafka differ from a traditional message queue? A queue removes a message once it is acknowledged and the broker tracks each consumer. Kafka keeps messages for a retention period and each consumer tracks its own offset, so many consumers can read the same data and replay it.
Why is ordering only guaranteed within a partition? Each partition is a single log read by one consumer per group. Different partitions are separate logs, possibly on separate brokers, with no shared sequence between them.
What does sendfile save? The copy from the page cache into a user buffer, the copy back into a kernel socket buffer, and one system call.
How do you scale consumption of a topic? Add partitions and add consumers to the group. A partition is the smallest unit, so a group cannot usefully have more active consumers than partitions.
What does at-least-once delivery mean for the consumer? After a failure some messages may be delivered again, so processing should be idempotent or should deduplicate using the offset or a key in the message.
Takeaways
- Kafka's speed comes from doing less: no per-message IDs, no per-consumer state on the broker, no cache of its own.
- Sequential access plus the page cache makes a disk-backed log perform well however much data is stored.
- Batching and
sendfileremove per-message network cost and per-byte copying cost. - Letting consumers hold their own offsets turns a queue into a replayable history.
- The 2011 design traded durability for throughput, and replication came later. Good designs state what they give up.












