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.
See it run
Follow a line from the program to a search result, and see what happens when the index stops or the traffic is ten times higher.
Operations
A line arrives
The answer to the agent comes from Kafka. The index works later, at its own speed.
Step 1 of 9
The program writes
time
0 s
One line in a local file. Why: the program must not wait for a network.
Step 1: The program writes
line = file.read() # agentbuffer.add(line)if buffer.size >= MB or buffer.age >= 5: ack = gateway.ingest(buffer) # redact, quota # gateway: kafka.append(batch, acks="all") if ack == STORED: checkpoint.save()batch = kafka.poll() # indexerindex.bulk(batch, ids=True)kafka.commit()- Latency
- safe in 6 s, found in 18 s
- Load
- 1M lines/s
- Target to search
- 30 s
- Peak (3x)
- 3M lines/s
Ask at each step: if this box vanished now, where is the line?
In the real world
The same log collection system doing real jobs. Pick one and step through it.
An error loop writes 1 GB/s of compressed lines for ten minutes.
Step 1 of 5
Three times is fine
- Pipeline
- 300 MB/s
Up to 300 MB/s everything flows.
Step 1: Three times is fine
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Acknowledging from memory
criticalA gateway restart loses what it answered for.
Wrongqueue.put(batch)return STOREDRightkafka.append(batch, acks="all")return STOREDCommitting before the write
criticalA crash between the two skips lines.
Wrongkafka.commit()index.bulk(batch)Rightindex.bulk(batch)kafka.commit()Rejecting unknown fields
majorA new field in a release makes its lines vanish.
Wrong"dynamic": "strict"Right"dynamic": false # stored, not indexedThe log call waits for the network
criticalThe logging outage becomes a product outage.
Wronghttp.post(LOGS, line) # in the requestRightfile.write(line) # agent sends itSilent drops
majorPeople trust a search that is missing lines.
Wrongif full: returnRightif full: dropped[service, level] += 1
Interview tips
Lead with two promises
- Durable in seconds.
- Searchable in 30 s.
- They fail separately.
Size the buffers
- 500 MB on a server: 27.8 hours.
- Kafka: 24 hours.
- Catch-up: spare speed.
Spend on purpose
- 60 nodes for a week.
- 3.15 PB of chunks for a year.
- Unknown fields are not indexed.
Plan the flood
- Errors first.
- Sample debug, merge equal lines.
- Count each drop.