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.

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

ProgramAgentdisk bufferGatewayKafka3 copiesIndexerElasticsearch

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

ProgramAgentdisk bufferGatewayKafka3 copiesIndexerElasticsearch
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

    critical

    A gateway restart loses what it answered for.

    Wrong
    queue.put(batch)return STORED
    Right
    kafka.append(batch, acks="all")return STORED
  • Committing before the write

    critical

    A crash between the two skips lines.

    Wrong
    kafka.commit()index.bulk(batch)
    Right
    index.bulk(batch)kafka.commit()
  • Rejecting unknown fields

    major

    A new field in a release makes its lines vanish.

    Wrong
    "dynamic": "strict"
    Right
    "dynamic": false   # stored, not indexed
  • The log call waits for the network

    critical

    The logging outage becomes a product outage.

    Wrong
    http.post(LOGS, line)   # in the request
    Right
    file.write(line)        # agent sends it
  • Silent drops

    major

    People trust a search that is missing lines.

    Wrong
    if full: return
    Right
    if 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.