Design a Metrics Monitoring System (Datadog)
hard 50 minCollect 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
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
| labels | series | memory |
|---|---|---|
| service, status | 40 | 160 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
criticalEach value is a new series with memory and index of its own.
Wrongcount("requests", user_id=user.id)Rightcount("requests", plan=user.plan)trace.set("user_id", user.id)Averaging percentiles
majorThe result is not a percentile of anything.
Wrongfleet_p99 = mean(h.p99 for h in hosts)Rightfleet_p99 = merge(h.sketch for h in hosts).quantile(0.99)Silence read as zero
criticalA dead host looks like a quiet one, and nobody is called.
Wrongvalue = points.get(t, 0)if value > 90: fire()Rightvalue = points.get(t) # may be Nonestate = machine.check(value) # None can mean NO DATARollups without counts
majorWindows with few points weigh as much as full ones.
Wronghour = mean(five_minute_averages)Righthour = sum(w.sum for w in ws) / sum(w.count for w in ws)Unlimited agent buffer
majorDuring an outage the agent eats the host's memory.
Wrongbuffer.append(batch)Rightbuffer = 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.