Design a distributed log collection system
hard 45 minCollect a million log lines a second from 100,000 servers, keep every acknowledged line, make recent lines searchable within 30 seconds and keep a year of them at a bearable cost.
Video lesson · Chapter 1 of 5
A cancellation is acknowledged before support can find it
Theory
Read or listen first. About 1 min.
What it is
A log collection system gathers the lines that programs write about themselves, from every server, into one place where people can search them, watch them live and be warned by them.
It is a pipeline with two very different ends. One end must accept a steady flood without losing anything. The other must answer a rare, urgent question: what happened to this request, ten minutes ago?
Why it exists
A request may cross twenty services. Reading twenty servers one by one is slow, and a server that was replaced takes its files with it.
The system is busiest exactly when something is broken, because broken programs write more. A design that only works on calm days fails when it is needed.
A short history
August 2001
RFC 3164 writes down the BSD syslog protocol, which programs had already been using to send messages to a log server.
March 2009
RFC 5424 replaces it with a standard syslog format that has structured fields.
June 2011
LinkedIn engineers publish the Kafka paper at the NetDB workshop: a messaging system built for log processing.
December 2018
Grafana announces Loki, which indexes only labels and searches inside compressed chunks.
Words to know
- Agent (shipper)
- A small program on each server that reads log files and sends them on.
- Checkpoint
- The place in a file up to which lines were acknowledged.
- Durable
- Stored on several machines, so one failure does not lose it.
- Consumer lag
- How far a reader of a queue is behind the newest record.
- Inverted index
- For each word, the list of lines that contain it.
- Tier
- A class of storage: fast and expensive, or slow and cheap.
- Live tail
- Matching lines shown as they arrive.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
Understand it
One person can read a lake house. Nobody can read three streams at once.
The task
What are we building?
A service like the Elastic Stack, Splunk, Datadog Logs, Grafana Loki, Graylog, Sumo Logic, Papertrail or Amazon CloudWatch Logs. It collects lines, makes them searchable, shows them live and raises alerts. Distributed logging explains good log lines and trace ids; this page is about moving and keeping them.
Collect
From files, containers and devices, in any format.
Search
Words, fields and a time range; recent lines in seconds.
Warn
Rules over the stream, live tail, error counts for dashboards.
Keep
A retention time for each kind of line, and a price for it.
The hard part
Accept a flood, stay affordable
Storing lines is easy and indexing lines is expensive. The hard parts are keeping the promise *acknowledged means kept* while the index is slow or down, and choosing how much of the data deserves an index at all.
The finished system
Where the design ends up
The whole system
Where each idea is taught
| Idea | Here | Full page |
|---|---|---|
| A log as a buffer | Kafka between agents and readers | Log-based queues |
| Inverted index | Seven days of lines | Full-text search |
| Cheap durable bytes | The archive | Object storage |
| Windows and late data | Alert rules | Stream processing |
| Safe repeats | Line ids | Idempotency |
| Numbers instead of lines | Out of scope here | Metrics system |
The Interviews page follows the nine tabs: Approach, Scope, Numbers, API and data, Design, Deep dives, Explain, By level and Mock.
Summary
The whole topic in one minute.
A log system separates durable from searchable. The agent gets its answer when Kafka holds the batch on several machines. Indexing, archiving and alerting read from there at their own speed.
Cost is controlled by tiers: seven days in a full-text index, a year as compressed chunks in object storage. Overload is controlled by buffers on 100,000 disks, and by rules that say what may be dropped, and count it.
- Acknowledge after Kafka, not after the index: durable and searchable are two promises.
- The agent's disk is the first buffer, and 100,000 of them make the largest buffer you own.
- Nothing in the path is exactly once; a stable line id makes a repeat harmless in the index.
- Index a week, archive a year. The index sets the machine count.
- Decide what may be dropped before the flood, and count every dropped line.