AlgoViz

Design a Metrics Monitoring System (Datadog)

hard 50 min

Collect five million numbers a second from 100,000 hosts, keep them for a year, draw dashboards quickly and wake somebody when it matters.

See it run

Follow numbers from a program to the store, to a panel and to a person.

Operations

Collect metrics

From a counter in a program to the log.

Step 1 of 6

A program counts

ProgramAgentsame hostGatewaycheck, limitKafkalogLocal buffer

metric

http.requests

labels

service=api, status=200

It sends plus 1 to the agent by UDP and does not wait.

Step 1: A program counts

every 10 s:    batch = read_system() + take_local_totals()    buffer.push(batch)            # drops oldest if full    send(gzip(buffer.oldest()), jitter=True)gateway:    tenant = check(token)    keep = limit_series(clean_labels(batch))    log.append(shard(keep), keep)    return 202
Delay
about 10 s
Keeps
a bounded buffer

The agent must never harm its host: bounded memory, bounded CPU.

In the real world

The same design a metrics monitoring system (datadog) doing real jobs. Pick one and step through it.

Somebody adds user_id to a metric.

Step 1 of 4

Before

labelsseriesmemory
service, status40160 KB

One metric, 40 series.

Step 1: Before

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A label without an end

    critical

    Each value is a new series with memory and index of its own.

    Wrong
    count("requests", user_id=user.id)
    Right
    count("requests", plan=user.plan)trace.set("user_id", user.id)
  • Averaging percentiles

    major

    The result is not a percentile of anything.

    Wrong
    fleet_p99 = mean(h.p99 for h in hosts)
    Right
    fleet_p99 = merge(h.sketch for h in hosts).quantile(0.99)
  • Silence read as zero

    critical

    A dead host looks like a quiet one, and nobody is called.

    Wrong
    value = points.get(t, 0)if value > 90: fire()
    Right
    value = points.get(t)      # may be Nonestate = machine.check(value)  # None can mean NO DATA
  • Rollups without counts

    major

    Windows with few points weigh as much as full ones.

    Wrong
    hour = mean(five_minute_averages)
    Right
    hour = sum(w.sum for w in ws) / sum(w.count for w in ws)
  • Unlimited agent buffer

    major

    During an outage the agent eats the host's memory.

    Wrong
    buffer.append(batch)
    Right
    buffer = deque(maxlen=360)   # an hour, then drop oldest

Interview tips

  • Lead with series

    • 100K x 500 = 50M.
    • 4 KB each in memory (assumed).
    • One independent label multiplies it.
  • Show the byte sums

    • 16 B to 1.5 B.
    • 648 GB a day.
    • 24 TB against 259 TB for 400 days.
  • Make alerts honest

    • 27 s is the sum of five waits.
    • Pending, firing, recovering, no data.
    • One message for one problem.
  • Plan your own failure

    • Twin ingesters and a log.
    • Reserved alert queries.
    • A watcher outside.