Observability
How metrics, logs and traces show what a system is doing, how percentiles, SLOs and alerts are used, and what interviewers ask about monitoring.
On this page
- Where observability sits in a system
- Metrics, logs and traces
- Percentiles and why averages mislead
- What to measure for a service
- SLIs, SLOs and error budgets
- Alerting on symptoms
- Tracing an alert from metric to trace to log
- High-cardinality labels, log volume and trace sampling
- In the interview
- What to practise next
Observability is the ability to tell from the outside whether a system is working for its users and, when it is not, where the problem is and why. Services produce three kinds of data for this purpose: metrics, logs and traces. Teams then define targets such as service level objectives and raise alerts when users are being affected.
Without it, the first sign of a problem is a customer complaint, and finding the cause means logging in to individual servers and guessing. A dashboard that shows only average latency can stay green while a few percent of requests take many seconds. In a system with many services, nobody can say which one caused a slow request unless each request is recorded as it passes through them.
Where observability sits in a system
Metrics, logs and traces
Metrics
A metric is a number measured over time, such as requests per second, the share of requests that fail, the 99th percentile of latency or the memory in use. Metrics answer the question "is something wrong, and since when?". The cost depends on the number of distinct metrics and not on the traffic.
Metrics are kept for months and drive dashboards and alerts, but a metric can show that 2% of requests fail without showing which requests or why.
Logs
A log is a record of one event, for example "payment 9f2 timed out after 800 ms on attempt 2". Logs answer the question "what happened in this particular case?", and they can answer questions nobody planned for, because they keep the detail that a metric throws away. Write them as structured records with named fields and include the trace id, so a log line can be matched to a trace.
Logs are the most expensive signal, because the volume grows with traffic and every request can write several lines. They are searched only after something else, such as a metric or a trace, has narrowed the problem down.
Traces
A trace follows one request through all the services it touches. The first service creates a trace id and passes it to each service it calls, usually in an HTTP header. Each service records a span, which has a name, a start time and a duration, and the spans of one trace form a tree. Traces answer the question "which call made this request slow?", which neither a metric nor a log can answer when ten services are involved.
Tracing only works if every service forwards the trace id, because one service that drops it hides everything below it.
Percentiles and why averages mislead
The average latency hides slow requests. Take ten requests that took 20, 22, 24, 25, 26, 28, 30, 32, 43 and 950 ms. The average is 120 ms, which is not the time any request took, and the dashboard looks fine while one user in ten waited almost a second.
A percentile describes the distribution instead. The p50, or median, is the latency that half of the requests beat, and here it is 27 ms. The p99 is the latency that 99% of requests beat, so only 1 request in 100 is slower. Teams watch p50 for the typical experience and p99 for the slow tail.
The tail matters more than its share suggests, because one page often needs many backend calls. If a page makes 20 calls and each is slow 1% of the time, then 1 − 0.9920, about 18% of pages, contain at least one slow call.
Percentiles cannot be averaged. The p99 of a fleet is not the mean of each server's p99. Servers should record latency as a histogram, which counts requests in latency buckets, so that the histograms can be added together and the percentile calculated from the sum.
What to measure for a service
The usual set is the three RED metrics: the rate of requests, the number or share of those that fail, and the duration of requests as percentiles. A fourth signal, saturation, describes how full a resource is, for example CPU, memory, the connection pool or the length of a queue. Saturation often warns of a problem before errors and latency rise.
SLIs, SLOs and error budgets
A service level indicator (SLI) is a measurement of one aspect of the service that users care about, written as a ratio of good events to all events. An example is "the share of checkout requests that succeed in under one second". A service level objective (SLO) is the target for that ratio over a period, for example 99.9% over 30 days. The difference between the target and 100% is the error budget. A 99.9% SLO allows 0.1% of requests to be bad.
An SLO is an internal target. A service level agreement (SLA) is a contract with customers that says what the provider owes them, such as a refund, when a target is missed. The SLO is set stricter than the SLA, so the team is warned before the contract is broken.
While budget remains, the team ships features and takes risks. When it is nearly spent, the team slows releases and works on reliability.
Alerting on symptoms
An alert should fire when users are being harmed and a person needs to act, not when some internal number looks unusual. High CPU on one server may harm nobody, while a slow query that causes timeouts harms users even though CPU is low. Alert on symptoms, which are failing or slow requests measured against the SLO, and keep cause-level signals such as CPU, disk and queue depth on dashboards, where an engineer looks at them while investigating.
Burn-rate alerts
The standard way to alert on an SLO is the burn rate, which is how fast the error budget is being used compared with a steady, sustainable pace. A burn rate of 1 uses exactly the whole budget in the SLO period. A common rule pages a person when the burn rate over the last hour is above 14.4. At that speed a 30-day budget would be gone in about two days. A slower burn creates a ticket instead of a page.
Total outage with no traffic
An alert based on the share of failed requests cannot fire when no requests arrive, so a total outage needs synthetic checks, which are test requests sent from outside the system every minute.
Tracing an alert from metric to trace to log
The tracer below follows an alert from a metric to a trace and then to a log line.
- checkout p99
- 2.1 s
- SLO
- 99.9% under 1 s
1An alert pages the on-call engineer because the checkout error budget is burning fast.
- rate
- normal
- errors
- normal
- checkout p99
- 2.1 s
2The request rate and error rate are normal, but the p99 latency of checkout rose from 250 ms to 2.1 s. This is a slowness problem in checkout or in something it calls.
- trace
- 4bf9
- gateway
- 2,050 ms
- checkout
- 2,040 ms
3One slow trace shows that the gateway span took 2,050 ms and that checkout accounts for 2,040 ms of it, so the gateway is not the problem.
- trace
- 4bf9
- inventory
- 15 ms
- payment
- 1,980 ms
4The call to the payment provider took 1,980 ms, which accounts for almost all of the time.
- attempt 1
- 800 ms, timeout
- attempt 2
- 800 ms, timeout
- attempt 3
- 300 ms, ok
5The logs for trace 4bf9 show two payment attempts that timed out after 800 ms each and a third that succeeded in 300 ms, with a 40 ms backoff before each retry. Together that is 800 + 40 + 800 + 40 + 300 = 1,980 ms.
- checkout p99
- back to 280 ms
6The payment provider is slow. The team reduces the impact, for example by switching to a backup provider, and adds payment latency to the dashboard.
High-cardinality labels, log volume and trace sampling
High-cardinality labels
A metric is stored once for every distinct combination of its label values, and each combination is a separate time series. A request counter labelled by endpoint and status code might have a few hundred series. If someone adds the user id or the request id as a label, the number of series grows with the number of users, and the metrics store runs out of memory or the bill multiplies. Keep labels to values with a small, fixed set, such as endpoint, status class and region. Per-user detail belongs in logs and traces.
Log volume
Log cost grows with traffic, and most of the volume is successful requests nobody will read. Reduce it by turning off debug logs in production, sampling the logs of successful requests while keeping every error, and keeping logs for less time than metrics.
Trace sampling
Storing every trace is too expensive at high traffic, so systems keep a fraction. In head-based sampling the first service decides at the start of the request, for example keeping 1 request in 100, and every service follows that decision. It is cheap, but it can miss the rare slow or failed request. In tail-based sampling a collector holds the spans of each trace until it finishes and then keeps all traces that had an error or were slow, plus a small share of the rest. It captures the interesting traces, but it needs memory and a collector that sees all the spans of a trace.
In the interview
The question usually comes near the end of a design, as "How would you know it is working?" or "How would you debug a slow request?". A weak answer lists tools. A strong answer starts from what users experience, names the one or two actions that matter, gives each an SLI and an SLO, alerts on the SLO, and then describes how you would go from the alert to a cause using metrics, traces and logs.
Questions to expect:
- "What is the difference between metrics, logs and traces?" Metrics are cheap aggregated numbers that show that something changed. Logs record the detail of one event and explain why it happened. Traces follow one request across services and show where its time was spent.
- "Why not just use average latency?" An average hides the slow requests that users complain about, and a few very slow requests can leave it looking normal. Percentiles show the tail, and with many backend calls per page the tail affects far more users than its percentage suggests.
- "What is an error budget, and what do you do with it?" It is the share of requests that the SLO allows to fail, for example 0.1% for a 99.9% target. The team keeps shipping while budget remains, and when it is nearly used up the team pauses risky releases and works on reliability.
- "What would you page on?" A fast burn of the SLO error budget, because it means that users are affected and the budget will be gone soon. CPU, disk and similar causes go to dashboards, and a slow burn becomes a ticket, so that pages stay rare and are taken seriously.
- "Your observability bill is growing quickly. What do you cut?" I would first look for metric labels with many distinct values, because they multiply the number of series. Then I would sample successful-request logs, shorten log retention and sample traces, keeping the slow and failed ones.
- "What is the difference between an SLO and an SLA?" An SLO is the internal reliability target that the team alerts on. An SLA is a contract with customers that has consequences, such as refunds, when it is missed, so it is set looser than the SLO.
Your notification system is live. How do you know it is working?
The promise to users is that a notification arrives within 10 seconds. So my SLI is the share of notifications delivered within 10 seconds, and the SLO is 99.9% over 30 days. I would page when the error budget burns fast. Each service also has request rate, error rate and latency percentiles, and the queue has consumer lag, which usually rises before delivery times get worse.
The SLO alert fires. What do you do?
I would check the dashboards to see which stage has rising lag or latency, then open a few slow traces to find the span that takes the time, and then read the logs for those trace ids. I would also run a synthetic check that sends a test notification every minute, so that a total outage is noticed even when no real traffic arrives.
A dashboard shows an average latency of 120 ms and looks healthy, but users report that checkout hangs. What should the team look at?
Which alert should wake someone at 3 a.m.?
After someone adds the user id as a label on a request-count metric, the metrics store runs out of memory. Why?
You want to keep the rare requests that failed or were very slow, but cannot store every trace. Which sampling method fits?
What to practise next
Read Reliability and Failure for what the alerts are protecting, and use these ideas in Design a Metrics Monitoring System and Design a Notification System.