Part 5 of Learning Kafka
Why Kafka is fast
Module 5 · 7 min read
Why Kafka is fast
TL;DR
- Appending to the end of a log avoids disk seeks, and SSDs like it too.
- The OS page cache serves consumers that keep up straight from RAM. Replication keeps the data safe, Kafka doesn’t force flushes.
sendfileskips CPU copies (except under TLS), and batching with compression spreads the per-message cost.
A disk-based system that moves millions of messages
Kafka writes every message to disk, yet one cluster can take in millions of messages a second. That surprised me. Disks are only slow when used one way, and Kafka has four ideas that each skip work.
1. Sequential writes
A spinning disk’s head has to move to the right spot before it writes. That seek takes milliseconds, the write itself microseconds. Scattered writes pay for a seek almost every time, writing to the next spot along almost never. Kafka’s log is append-only (Module 1), so every write goes to the end of a file.
SSDs have no head, but they erase and rewrite large blocks, so scattered small writes cost far more there too. Sequential appends keep throughput high and reduce wear.
The grid is a strip of disk blocks. The numbers are made up for illustration (a seek costs 8 ms, a write 0.1 ms), so it’s no benchmark.
2. The page cache, RAM for free
Kafka hands written bytes to the OS, which keeps them in the page cache (RAM for recently used file data) and flushes to disk in the background, so the write returns immediately. A consumer that keeps up reads from RAM, one far behind reads from disk. So a lagging consumer (Module 3) is slower and makes the broker do more disk work.
3. Zero-copy
The obvious way to serve a consumer, read the file into the app and write it to the socket, takes four copies and four switches between user and kernel mode.
- The disk controller copies the data into the kernel’s page cache (DMA, no CPU).
- The CPU copies it into the application’s buffer.
- The CPU copies it into the kernel’s socket buffer.
- The network card copies it out (DMA again).
But the application only passes bytes along, and Kafka stores messages on disk in the format it sends them, so nothing needs changing. Linux’s sendfile tells the kernel “send this file range to that socket”, moving data from page cache to network card without entering the application. Kafka calls it through Java’s transferTo.
One catch. Zero-copy needs the bytes to go out exactly as stored. TLS encrypts in user space, so the broker must read the data into memory and loses the shortcut. Some speed traded for encryption in transit.
4. Batching and compression
Sending one message at a time spends most of the effort on the envelope (a network round trip, a request header, a disk write). So Kafka batches everywhere.
Producers collect messages per partition and send when the batch is full (batch.size) or after a short wait (linger.ms). A small delay buys a much bigger batch. Compression on a whole batch works far better than on single messages. The broker stores the batch as it arrived without recompressing, and consumers decompress it. Consumers fetch many messages per request too.
The price is latency, a bigger linger.ms can hold a message a few milliseconds. For notifications I think that’s a good deal. Nobody notices 5 ms on a push, and the cluster does a fraction of the work.
The four ideas together
| Idea | Work it avoids |
|---|---|
| Append-only log | Disk seeks on every write |
| Page cache | Disk reads for recent data, and a second cache inside Kafka |
| Zero-copy (sendfile) | Two CPU copies and two mode switches per read |
| Batching and compression | Per-message round trips, headers, and bytes on the wire |
Each lets the OS and hardware do what they’re already good at, and the boring append-only log makes all four possible.
Quick check
A consumer has been down for a day and now reads from the start of the topic. Why is it slower than one that is keeping up?
A day-old backlog has mostly left the page cache, so the broker reads it from disk. Messages are stored the same way at every age.
Q&A
If writes only reach RAM first, can I lose data?
A single broker that crashes before the OS flushes could lose its last few messages. Kafka relies on replication (Module 4) for this. With acks=all a message is acknowledged only once the in-sync replicas have it. Forced flushes exist but are rarely used, because they cost a lot of speed.
Does Kafka cache data itself?
Mostly no. Using the OS page cache instead of a cache in the JVM heap keeps the heap small (no extra memory to manage, no garbage-collection pauses) and avoids storing data twice. The cache also survives a Kafka restart.