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 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 toolsrandom.seed(7)Fix the dice so results repeatkinds = ["page_view", "signup", "click"]List the kinds of eventsevents = [random.choice(kinds) for _ in range(30)]Producer writes 30 random eventsservices = ["email", "analytics"]Name the two services # (a) queue: a message vanishes once readDesign A: the classic queuequeue = list(events)Copy events into the queueseen_q = {"email": 0, "analytics": 0}Start both service counters at zerowhile 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 loglog = list(events)Copy events into the logoffset = {"email": 0, "analytics": 0}Each service's bookmark starts at zerofor 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 deliveredshow(list(offset.values()), "Log: email vs analytics")Bar chart: what the log delivered # new "fraud" service replays from offset 0Newcomer: the fraud servicefraud, signups = 0, 0Fraud starts at offset zerowhile 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 forwardprint("fraud replayed", fraud, "events;", signups, "were signups")Report what fraud replayedprint("queue left:", len(queue), "| log still holds:", len(log))Compare queue leftovers with log size- deletion line 12pop(0) destroys the message. Whoever wins it, everyone else loses it forever.
- own bookmark lines 19–20Each service moves only its own offset. Reading never changes the log, so readers can't steal from each other.
- replay lines 26–30A brand-new reader starts at 0 and walks the entire history. That's impossible once a queue has deleted it.
Read more
- github.com: The append-only log
- The real code: apache/kafka
- Ask Bento: What are partitions in Kafka?
- Ask Bento: How does retention drop old records?
- Ask Bento: How do consumer groups share work?
Run it. Then change it.
The code runs right in the page and draws as it goes. Free, nothing to install.