Design an Alerting System
🚨 What is an alerting system? It is the part of a monitoring stack that watches numbers and wakes people up. Services send it data, such as CPU usage, error rates or "is the API answering?". Rules say what "wrong" looks like. When a rule has been wrong for long enough, the system notifies the on-call engineer, and when things recover it says so.
You may hear this prompt as "design a monitoring and alerting system", "alert on health checks" or "threshold alerts on metrics". For a low-level design round they are the same problem: two kinds of checks (a query check that compares a metric to a threshold, and a health check that counts failed probes) feeding one shared alert lifecycle that decides when to notify. Both kinds are part of the main design below. Storing billions of data points across a fleet is a separate, high-level question, covered in /system-design/metrics-monitoring.
Requirements
"Design an alerting system. Services report metrics and health-check results. When something is wrong, the right people should be notified."
The questions below turn that into rules we can test.
Clarifying Questions
You: What kinds of checks do we need?
Interviewer: Two. A metric rule, like "average CPU over the last minute is above 80". And a health check, like "the API failed three probes in a row".
You: Should a single bad data point page someone at 3 a.m.?
Interviewer: No. A metric rule can say how long the condition must hold before it fires, for example "for two minutes". Until then it is only pending. Health checks already have their own threshold, the number of failures, so they fire right away.
You: When do rules run? On every data point, or on a schedule?
Interviewer: On a schedule. Something calls the engine every few seconds with the current time, and it evaluates all the rules. Pass the time in; don't read a clock.
You: When do we notify, and how often?
Interviewer: Once when an alert starts firing, and once when it resolves. No repeats while it keeps firing. Delivering to email, chat or a pager is someone else's job; we just hand them the message.
You: What if a metric has no data in the window?
Interviewer: Treat it as not breaching for now.
You: Who runs the health probes?
Interviewer: A separate prober sends the HTTP requests. We just receive "target X: passed" or "failed".
You: Can data arrive while rules are being evaluated?
Interviewer: In real life, yes. Assume a single thread first.
The second answer is the heart of the problem. Without it, an alert is a boolean: breaching or not. With it, an alert has memory: it must remember when the condition started holding. That is a state machine, and the rest of the design is about keeping that state machine in one place while the checks vary around it.
💡 Interview tip: Ask "when does evaluation happen?" early. If rules only run when data arrives, a service that goes silent never resolves its alert and a pending timer never completes. A clock-driven
evaluate(now)avoids both problems and makes your walkthrough deterministic.
Final Requirements
- A metric rule has an id, a metric name, a window in seconds, a comparison (
>or<), a threshold and a "for" duration. It breaches when the average of the metric's points in the lastwindowseconds is strictly above (or below) the threshold. A window with no points does not breach. - A health check has an id, a target and a failure count. It breaches when the target's last
failuresprobe results were all failures. A success resets the count. - Every rule is in one of three states:
OK,PENDINGorFIRING. evaluate(now)runs every rule, in the order they were added:- breaching while
OKmakes itPENDING, starting the timer now; - breaching while
PENDINGfor at least the "for" duration makes itFIRING(health checks use a duration of 0); - not breaching makes it
OK.
- breaching while
- Moving to
FIRINGsends"FIRING <id>". Moving fromFIRINGtoOKsends"RESOLVED <id>". Nothing else sends anything. - Rule ids are unique across both kinds. Anyone can ask for a rule's current state.
- Notifications go to one or more channels, and new channels and new kinds of checks will be added.
Out of scope
- Storing and querying metrics at scale, downsampling and retention policies.
- Running the probes themselves, and delivering emails or pages.
- Dashboards, editing rules through a UI, and on-call schedules.
- Several threads at once (we come back to this in Extensibility).