System design paper · 2011

Kafka

Kreps, Narkhede and Rao · NetDB 2011

How LinkedIn built Kafka: a simple log on disk that lets many programs read huge streams of events quickly, and what has changed since 2011.

A stream of messages is split into three parts, called partitions, each kept as a log on a different server. Producers add to the end of a part, picking one at random or by a key. Among consumers that share the work, each part has just one consumer.

You’ll learn to

  • Explain why Kafka keeps messages in an append-only log and finds them by position instead of by id.
  • Work out how partitions and consumer groups share one stream between many consumers.
  • Say what the first version did not promise about lost and repeated messages, and how replication and transactions added those promises.
  • Name what replaced ZooKeeper inside Kafka, and why.

The idea in short

A big website produces a flood of small records: page views, clicks, errors. LinkedIn needed to move them between systems within seconds, and the message systems of the day were too slow or too heavy for that volume. Kafka’s answer is to treat the stream as a log, a file that only ever grows at the end.

Programs that write messages (producers) add them to the end of the log. Programs that read them (consumers) pull from it at their own pace, each remembering its own position, so a slow consumer never holds back a fast one and any consumer can go back and read again. The log is split into parts so that many servers and many consumers share the work. The first version gave up some guarantees, such as never losing a message, to be fast. Later versions added them back.

The problem

Every large website produces a constant stream of small records. A page view, a click, a login, a search, an error, a reading of how busy one machine’s processor is. The paper calls all of this log data: records of things that happened, in the order they happened. It is not the “real” data, such as profiles and messages, which sits in databases.

For years log data was used after the fact. Servers wrote log files, and once a day or once an hour a job copied them somewhere and built reports. By 2011 LinkedIn wanted more. Search relevance, recommendations, ad targeting, protection against spam and scraping, and the news feed all used the activity stream directly. The authors say most of these real-time uses needed delays of no more than a few seconds, not a day.

That changes the size of the problem. The authors point out that this stream is orders of magnitude larger than the real data. Working out click-through rates, for example, logs a record for every click and also for the dozens of items on each page that were not clicked.

Two kinds of existing tool were on offer, and neither fitted.

Enterprise messaging systems, such as those built on the Java Message Service (JMS) standard, pass messages between programs through a : a buffer where one program leaves messages and another takes them later. The authors found four mismatches:

  • Too many guarantees. These systems let a consumer acknowledge each message separately, even out of order, and some add transactions across queues. For logs that is waste. In the authors’ words, “losing a few pageview events occasionally is certainly not the end of the world”.
  • Throughput was not the goal. JMS had no way to send several messages in one request, so every message cost a full network round trip. That could not keep up with the volume.
  • Weak spreading across machines. There was no easy way to split a queue over several machines.
  • They assumed consumers keep up. Performance dropped sharply once unread messages piled up. That is exactly what happens when a data warehouse loads data once a day.

Log aggregators, such as Facebook’s Scribe, Yahoo’s Data Highway and Cloudera’s Flume, were built for logs. But they were meant for offline loading into a data warehouse or Hadoop (a system for processing very large files). Some leaked details to consumers, such as the files they rolled over every minute. Most pushed data to consumers at the sender’s speed, so a consumer that was slower than the sender was flooded.

So the question the paper answers is: can one system have the volume of a log aggregator and still let programs read new events a few seconds after they happen, like a messaging system?

The design choices

The authors made a handful of unusual choices. Everything else follows from them.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan

About the paper

Kafka: a Distributed Messaging System for Log Processing by Kreps, Narkhede and Rao, NetDB 2011.

Read the original paper

More System design papers

All papers