AlgoViz

Design a distributed log collection system

hard 45 min

Collect 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

0:00 / 4:42

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

  1. August 2001

    RFC 3164 writes down the BSD syslog protocol, which programs had already been using to send messages to a log server.

  2. March 2009

    RFC 5424 replaces it with a standard syslog format that has structured fields.

  3. June 2011

    LinkedIn engineers publish the Kafka paper at the NetDB workshop: a messaging system built for log processing.

  4. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

Understand it

Bottles from many streams
Village 1Village 2Village 3

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

Servers run an agent with a disk buffer. Agents send batches to gateways, which check quotas and redact, then write to Kafka with three copies. From Kafka, indexers fill Elasticsearch, archivers fill object storage and an alert engine calls a pager. Engineers use a query service that reads Elasticsearch for recent lines and object storage for older ones. PostgreSQL holds rules and access rights.
Blue brings lines in, pink answers people, orange warns. The Design tab shows which number forces each box.

Where each idea is taught

IdeaHereFull page
A log as a bufferKafka between agents and readersLog-based queues
Inverted indexSeven days of linesFull-text search
Cheap durable bytesThe archiveObject storage
Windows and late dataAlert rulesStream processing
Safe repeatsLine idsIdempotency
Numbers instead of linesOut of scope hereMetrics 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.

Sources