BOOK DEEP READ · System Design Interview Vol 2 · Ch 5

Metrics Monitoring and Alerting System

System Design Interview Vol 2 · Ch 5 · Alex Xu & Sahn Lam · 2022

中文 →

What is this chapter about?

That wall-mounted dashboard at the office, packed with squiggly lines: how hot the machines are running, whether checkout got slower. And the phone call at 2am telling you a server is unwell. This chapter is about how those lines get collected, and about the thing that decides whether to wake you up — built from scratch.

A strange thing first

Your company's core storage system breaks. You open the monitoring dashboard to see what went wrong — and discover the monitoring system won't open either, because its data happens to live in the very storage system that just died.

That is the first iron law of monitoring: the fire alarm cannot be wired to the burning building's electricity. It has to be sturdier and more independent than everything it watches. Google's own monitoring system keeps all its data in memory purely to escape this loop — absurdly expensive, and worth it.

Why the old world was hard

When one machine misbehaves, you log in and look. What about ten thousand? And these numbers never stop arriving: a hundred measurements per machine, reported every ten seconds, which adds up to a million fresh numbers every second, around the clock.

Worse, the job has a strange shape: the incoming volume is enormous and utterly relentless, while almost nobody is reading — it keeps writing when nobody is watching the dashboard. Ordinary databases are designed for roughly balanced reads and writes, and get flattened here.

How it actually pulls this off

1. Collecting. Either every machine keeps a little blackboard by its door with its current numbers, and a meter reader walks the route copying them down (if a door doesn't open, you've learned something about that house for free); or each machine sends a runner who drops a note at the mail room (the only option for machines hiding behind a firewall, or for jobs that don't live long enough to be visited).

2. Storing. Consecutive readings look remarkably alike: time ticks along evenly, and the values often don't budge at all. So don't transcribe each one in full — just record "how much this differs from the last", the way a ledger says "unchanged, unchanged, plus one" instead of "10032, 10032, 10033". The same data gets more than ten times slimmer.

3. Old data. Nobody, a year later, cares what the CPU was doing at 3:07pm last Wednesday. So the last few days are kept verbatim, anything past a month is squeezed into one summary per minute, and older still into one per hour — like a diary gradually abbreviated into a year-end review. The cost is real: a spike that flashed by once is gone forever after the summary smooths it away.

4. When to wake you. One data centre drops off the network and a thousand machines shout "I'm unwell" at once. Send each one and your phone rings a thousand times in thirty seconds — after which you silence it and miss the next real emergency. So before anyone gets woken, the thousand shouts about one event must be gathered into a single sentence.

How to remember it

A monitoring system is not "a website with charts on it". It is an assembly line running from a million devices to your phone: the first half is about swallowing a flood of numbers quickly and cheaply, the second about waking exactly one person when something is truly wrong — and not pestering them when it isn't.

One sentence to keep

Monitoring is a peculiar write-heavy, read-light system: it shrinks data by recording only changes, holds costs down by abbreviating old data into summaries, and protects humans by folding a thousand duplicate shouts into one. Plus one iron law — it must never depend on the systems it is watching.

Want the mechanisms, the numbers and the diagrams? → Switch to the deep read