Algorithms · Huge scale

The append-only log

LinkedIn’s big idea: the humble log. Write events to the end of a list and never change them. Every reader keeps their own bookmark.

The problem

Picture a big site like LinkedIn. Every click, page view and signup is an event. Lots of systems want those events: one sends emails, one builds analytics, one watches for abuse, and more keep showing up.

Wiring every producer to every consumer by hand turns into a tangle. This was the pain behind Apache Kafka, built at LinkedIn by Jay Kreps, Neha Narkhede and Jun Rao, and open-sourced in 2011.

The obvious way

Use a classic queue: a producer drops messages in, and once a service takes a message, it's deleted. That's fine for one reader. With two, they compete for messages, so email never sees what analytics took. A new fraud service arrives late and finds only leftovers, because the past is gone.

The clever idea

Think of a notebook where you only ever write at the bottom and never tear out pages. Each reader keeps their own bookmark. Nobody's reading affects anyone else, and a newcomer can open to page one.

That notebook is an append-only log. Kafka splits it into partitions, and keeps records for a retention period. Each consumer group tracks its own offset, so many readers go at their own pace and can replay from any point.

How the code does it

In the queue loop, every pass removes one message and gives it to just one random service. The two bars add up to 30, so each service saw only a slice of the events. By the end, queue is empty.

In the log loop, each service advances its own offset over the same log, so both bars hit 30. Then the fraud replay starts at 0 and sees everything, while the last line shows the log still holds all 30.

Trade-offs

A log keeps data, so it costs storage until retention drops old records. Readers must track their own offsets. Strict order holds only within one partition, not across the whole topic. If you just need one worker to handle each job exactly once, like a to-do list, a plain queue is simpler.

Where else you'll see it

Jay Kreps' 2013 essay, "The Log: What every software engineer should know about real-time data's unifying abstraction", argued the log is the backbone connecting data systems. Read the real thing at github.com/apache/kafka.

In the log design, a service finishes reading all 30 events. What does the email service's offset do to the analytics service's offset?

  • Nothing, each has its own
  • It resets analytics to 0
  • It splits the events between them
Show the answer

Nothing, each has its own. Offsets belong to each reader. Reading never deletes or alters the log, so readers are independent.

Change it and see
  • Change range(30) to range(10)
  • Add a third service named "audit"
  • Change fraud to count "click" events

The code

Python, with what each line means beside it. It compares the clever way with a queue that deletes each message once one reader takes it.

import randomBring in the random tools
random.seed(7)Fix the dice so results repeat
kinds = ["page_view", "signup", "click"]List the kinds of events
events = [random.choice(kinds) for _ in range(30)]Producer writes 30 random events
services = ["email", "analytics"]Name the two services
# (a) queue: a message vanishes once readDesign A: the classic queue
queue = list(events)Copy events into the queue
seen_q = {"email": 0, "analytics": 0}Start both service counters at zero
while queue:Repeat until the queue is empty
who = random.choice(services)A random service grabs the next one
queue.pop(0)Remove the front message forever
seen_q[who] += 1Credit that service with one event
# (b) log: nothing is deleted, each reader has an offsetDesign B: the log
log = list(events)Copy events into the log
offset = {"email": 0, "analytics": 0}Each service's bookmark starts at zero
for name in offset:Go through each service
while offset[name] < len(log):Keep reading until the log's end
offset[name] += 1Move that bookmark forward one
show(list(seen_q.values()), "Queue: email vs analytics")Bar chart: what the queue delivered
show(list(offset.values()), "Log: email vs analytics")Bar chart: what the log delivered
# new "fraud" service replays from offset 0Newcomer: the fraud service
fraud, signups = 0, 0Fraud starts at offset zero
while fraud < len(log):Read until the end of the log
if log[fraud] == "signup":If the event is a signup
signups += 1count it
fraud += 1Move fraud's bookmark forward
print("fraud replayed", fraud, "events;", signups, "were signups")Report what fraud replayed
print("queue left:", len(queue), "| log still holds:", len(log))Compare queue leftovers with log size

Read more

Run it. Then change it.

The code runs right in the page and draws as it goes. Free, nothing to install.

More on huge scale