Metrics Monitoring: Scaling It
Four things on this board break for four unrelated reasons, and minutes twenty to thirty take them one at a time. In the order the interviewer brings them:
- The rate you volunteered lands on the table, and they ask what a row per reading does at that size.
- Someone wants a month of the whole fleet on one chart, and the scan runs to billions of rows.
- One team's new label quietly multiplies what the store has to remember.
- The collector goes down, and the alert that ought to report it goes down with it.
Two Million Points
"Forget the query side. What does that table do at two million inserts a second?" It fills a 2 TB disk in under three hours before any query runs. But this data has a shape a general table never uses: every write is new, nothing is ever updated, and each series, one metric with one set of labels, arrives in time order. A time-series store is built on exactly that shape.
A point is appended to its series in memory. Every 2 hours, an assumed interval, that memory is written out as an immutable compressed block, a file that never changes once written. At 2 bytes a point, the blocks now take 4 MB a second instead of 200. Two hours at that rate is about 29 GB of memory a copy, so each node also appends every point to a small file on disk as it arrives, and a restart rebuilds that memory from the file. The file costs a second write of every point and a restart that waits on the replay.
Next, where points go. Partition by a hash of the series inside each time block. Time alone sends every write to the newest node, the moving hot spot from Chapter 3's Hot Partitions. Series alone cannot drop old data whole, because every partition holds every age. Series inside time gives you both: writes spread evenly, and an expired block goes in one delete.
Assume a node takes 500,000 points a second. Two million over 500,000 is 4 partitions, and 8 at half their limit. Each point goes to two nodes that do not coordinate, which keeps the promise that a point survives losing any one node, and that makes 16 nodes. A query reads either copy, so a silent node costs a read nothing.
Chapter 4's Concurrent Writes and Conflicts is about writers who disagree, and these two cannot. One series at one time is one value, so a repeat only writes what is already there.
The one collector goes too. A hundred thousand hosts every 10 seconds is 10,000 batches a second. Assume a stateless collector takes 1,000, so ten is the floor, and twenty at half their limit. Agents start at a random second. If they all posted at once, the flat rate from Scope would be a spike ten times over.
What it costs: no general SQL, so the any-query-on-day-one you praised is gone, and no updates to old points, so a wrong value stays wrong until its block ages out. You pay that once the rate is in the millions and the queries are ranges over series. A hundred servers stay on the table.
The Thirty-Day Chart
"An engineer drags the range out to thirty days across the whole fleet. Then what?" Thirty days at 8,640 points a day, for one metric on 100,000 servers, is about 26 billion points. As rows, that is 2.6 TB. A fast disk reads 10 GB a second, so that is over four minutes, for one panel, on a chart about 1,000 pixels wide.
Compression helps the disk and not the work. At 2 bytes a point it is 52 GB, about five seconds of reading, and 26 billion points still have to be decoded. Raw points only go back 14 days anyway, so the agreed retention already points at the answer.
The change is a background job that writes rollups, summaries of many points, as blocks age, 5-minute and then hourly. A query service, which answers the dashboards, picks the coarsest tier close to the chart's width. An hourly tier has 720 points a series, near enough for 1,000 pixels, and 360 times fewer. At four values a point, this chart is about 600 MB, under a tenth of a second of reading. The newest hours, not rolled up yet, come from the raw points.
A rollup keeps four values for each interval: minimum, maximum, sum and count. An average alone fails twice. An average of averages is wrong when intervals hold different counts, and a spike vanishes into it. Sum and count give a true average at any coarser tier, and the maximum keeps the spike alive. Those four suit a reading such as CPU. A counter of requests served only climbs, and falls to zero on a restart, so the sum of its readings means nothing. For a counter the job rolls up the increase in each interval, treating a fall as a restart, and the sum is then the requests served. So each metric has to carry its type.
What it costs: a job to run, a spike that shows as a maximum with no exact time, and percentiles that survive only if histograms were stored at the start. Minimum, maximum, sum and count cannot give you a 99th percentile afterwards.
The User ID Label
"One team just shipped a label holding the user ID. Why is the store's memory climbing?" Because a label is not a field on a row. Every new value makes a new series, and each series needs its place in memory and in the index before its first point is stored.
Multiply it out. A metric with 50 endpoints and 5 statuses on 1,000 servers is 250,000 series. A new label multiplies that, so the ceiling is 250,000 times the number of users. The real number is lower, and still bad. Even if each of 10 million users makes one series, that is half the fleet's 20 million, from one label.
Assume 2 KB of memory and index a series. Ten million series is 20 GB a copy before a point is written, on top of the 40 GB the fleet's own series take. And the next label multiplies again.
More memory is the answer that earns a second label. The change is a budget at the door, in the collector. Assume 500,000 series a metric: twice this counter, and five times a plain metric with one series on each of 100,000 hosts. Over it, new series are refused, series already admitted keep flowing, and the refusals are counted on the owner's dashboard. That is the refusal at the door from Chapter 6's Backpressure and Load Shedding. This label now costs at most another 250,000 series, about 500 MB a copy, in place of 20 GB.
Per-user questions go to logs and traces, as Observability Deep Dive's Label Discipline explains. What it costs: every reading now carries the label, so once the budget is spent each new combination is refused, and that counter undercounts until the team takes the label off, sometimes in the middle of an incident. Someone also has to own the exceptions.
Who Tells You
"The collector's gone, and so has the alert that would have told you. What's your move?" Start from what the first design promised. Agent, collector, store and alert job in series at 99.9 percent each are about 99.6, as Chapter 6's Regions and the Arithmetic of Nines sets out. That is about 175 minutes a month with nobody watching. A health alert on the same job reads through the same broken chain.
The first change is a log between collectors and store, an ordered record any number of readers can follow, from Chapter 6's Queues, Logs and Pub-Sub. Its records hold many points each, so the 200 MB a second it carries is a third of the 600 MB a second one vendor reports for three brokers on 1 KB messages. Assume it keeps an hour, about 720 GB. A store that stops for less loses nothing: a point that arrives after its block was written, up to the log's hour late, goes into a small block of its own for the same span, which a later merge folds in. At half their limit its partitions drain the backlog in about as long as they were down.
The second is a path of its own for critical rules. Assume they touch one series in a hundred, which is 20,000 points a second. Collectors copy those points to a small stream in the log, about 2 MB a second, so nothing has to filter the other 99 percent. A small evaluator in each of two zones reads that stream and checks the critical rules in memory, with no store and no query service. Both fire, and a notifier sends one page.
The third is a heartbeat: a rule that always fires, sent through the notifier to a watcher outside the system, which pages someone if it stops hearing it. Silence now counts as a signal.
Walk the failures out loud. The store dies: dashboards and the ordinary rules go quiet, and the critical alerts do not notice. Collectors or log die: the evaluators see silence, and silence pages. Evaluators or notifier die: the heartbeat stops, and the watcher pages. The 175 minutes of nobody watching have become pages.
Agents buffer meanwhile, and replay at a set pace, because a backlog is a burst, as Chapter 6's Backpressure and Load Shedding warned. Ten minutes is only about 1 MB on a server. Twenty collectors take twice the normal rate, so they replay at double pace, and a ten-minute outage takes ten more to drain.
The other rules stay where they were: the alert job, now reading the store, at about 170 checks a second at most, with its own call to the pager. If the store dies they go quiet, which is exactly why they are not the critical ones.
What it costs: a second path to run, two places a rule can be wrong, and an outside service to depend on. A pair of evaluators, at 99.9 percent each, are both down one time in a million only while they share nothing. So you name what they share: "The alert path shares three things with the rest: the agents, the collectors and the log. If the store and the dashboards die together, the page still goes out, and if the shared parts die, the silence is the page."
Sixteen Nodes and a Second Path
Agents with a small buffer post to collectors that enforce budgets, and collectors write to the log. The store reads the log: 8 partitions by series inside 2-hour blocks, two copies, 16 nodes. A rollup job writes the tiers, and a query service answers the dashboards from them. Beside all that, the log feeds two evaluators in two zones, then the notifier, then the pager, with a heartbeat to the outside watcher.
Some things moved without a box of their own. Rules, alert states and each metric's budget and owner were never points, so they sit in a small relational table, which the alert job and the collectors use. Each evaluator keeps a copy of the critical rules and its own alert states in memory, so it needs nothing from that table to fire, and the notifier's deduplication covers a restart. Labels stopped being free text in every row, because the series is now the key.
Two claims on that board are still unexplained, and you flag them before the interviewer does: "How compression gets to two bytes, and the alert storm: those are next."
- Sharding by time because the data is time-ordered. The moving hot spot takes one sentence to explain, and the interviewer will say it for you.
- Rolling up with an average alone, then being asked where the spike went. There is no answer once the points are gone.
- Answering the label with more memory. It is the answer that earns a second label, and the interviewer will offer one.
- Duplicating the collector and calling the alert path independent, while it still reads the store. One question about the store's failure ends that claim.
- The partition key given with its reason: by series, within blocks of time.
- A label multiplied out aloud, the ceiling and then the real number.
- The alert path's shared parts named, not only its separate ones.
Knowledge Check
What does an hourly rollup give up in order to answer the thirty-day chart?
- The spike itself, since an hour's points are reduced to an average
- A spike's exact time, and any percentile not stored as a histogram
- The count of readings, since only a minimum and maximum are kept
- Any way of charting the raw points from the last fourteen days
Why is the partition key not time alone?
- An old block could not be dropped whole once it passes retention
- Clocks on a hundred thousand agents disagree, so points go astray
- A fleet-wide query would have to read every one of the partitions
- Each new write would land on the newest node and leave the rest idle
A team's new label hits the series budget in the middle of an incident. What has the budget cost them?
- Readings past the budget, while series already admitted keep flowing
- All of that metric's points, until the team gets back under budget
- Every metric the team owns, until someone approves an exception
- Nothing at all, as refused series are buffered and written later
What does the heartbeat catch that a health alert on the same path cannot?
- A store node falling behind, since its lag never reaches a dashboard
- A rule with a wrong threshold, since the heartbeat tests every rule
- The alert path itself going dark, because something outside hears silence
- A single series that vanishes, since it reads every series as it goes
You got correct