The idea in one minute#
Start from what users are trying to do — a critical user journey (CUJ) such as “log in”, “search”, “pay”. For each one, define a service level indicator (SLI): the ratio of good events to valid events, measured as close to the user as you can. “Good” is precise: status not 5xx, finished within 400 ms, response complete.
An SLI has two halves. The specification says what you mean in plain words (“checkout
requests that succeed within 1 s”). The implementation says how it is measured (“load
balancer logs, status < 500 and request_time < 1.0, excluding health checks”). Different
implementations of the same specification can give very different numbers.
An analogy#
A restaurant does not measure “the oven is on”. It measures whether diners got the dish they ordered, hot, within twenty minutes. The oven matters only because of that. CPU, memory and queue depth are ovens; the SLI is the diner’s plate.
A picture#
flowchart LR
U[":i-user: <b>User</b>"] --> CL[":i-smartphone: <b>Client</b><br/><small>RUM: truest, noisiest</small>"]
CL --> CDN[":cloudflare: <b>CDN / edge</b>"]
CDN --> LB[":envoyproxy: <b>Load balancer</b><br/><small>best default for SLIs</small>"]
LB --> APP[":go: <b>Service</b><br/><small>app metrics: easy, misses LB failures</small>"]
APP --> DB[(":postgresql: <b>DB</b>")]
PR[":i-radar: <b>Prober</b><br/><small>synthetic: works with zero traffic</small>"] -.-> CDN
class U neutral
class CL,CDN,LB io
class APP compute
class DB memory
class PR queueThe closer to the user, the more failures the SLI sees — and the more noise it picks up from things you do not control (bad mobile networks). The load balancer is usually the best compromise: it sees requests that your service never received because it was crashed.
How it really works#
Finding critical user journeys#
List what users do, then rank by how much they care. For a web shop:
| Journey | Why it matters | Typical SLI kinds |
|---|---|---|
| Browse and search products | First impression, volume | availability, latency |
| Add to cart | Intent to buy | availability |
| Checkout and pay | Revenue | availability, latency, correctness |
| View order status | Support load if broken | availability, freshness |
| Admin: update prices | Internal, rare | availability (looser) |
Pick 3–5 journeys. A dozen SLOs per service is a sign nobody chose.
The SLI menu#
| SLI kind | Good event | Valid events | Good for |
|---|---|---|---|
| Availability | Response not 5xx (and not a timeout) | All requests except health checks and client errors | Request/response services |
| Latency | Finished in under a threshold | Successful requests (or all) | Interactive paths |
| Quality | Full answer, not degraded | Requests | Services with fallbacks |
| Freshness | Data younger than a threshold when read | Reads | Pipelines, caches, search indexes |
| Correctness | Output matches an independent check | Sampled outputs | Billing, data processing |
| Coverage / throughput | Records processed in time | Records expected | Batch jobs |
| Durability | Object still readable | Objects stored | Storage |
Latency SLIs use a threshold, not an average: “99% of requests under 400 ms” is an SLI,
“mean latency 120 ms” is not, because the mean hides the slow tail that users actually feel.
It is common to have two latency thresholds: one for typical experience (90% < 200 ms) and one
for the tail (99% < 1 s).
What counts as “valid”#
Deciding what to exclude is where SLIs go wrong.
- Exclude health checks, synthetic load tests, and requests that were the client’s fault (400, 401, 404 for a bad URL).
- Include 429s you returned because you were out of capacity — that is your failure.
- Include timeouts and connection resets, even though no status code was logged. A common bug is an SLI that only counts responses, so a crashed service with no responses looks 100% available.
- Decide explicitly about retries: is a request that failed once and succeeded on retry good? From the user’s point of view, usually yes — so measure at the layer that sees the final result.
Where to measure#
| Source | Sees | Misses | Use |
|---|---|---|---|
| Client (RUM) | Everything the user saw | Nothing, but includes their network | Latency SLIs for web and mobile, as a check |
| Edge / load balancer logs | Every request that arrived, including when the app was down | Failures before the edge (DNS) | Default for availability and latency |
| Service metrics | What the process handled | Requests the process never got | Fine when an LB-level view also exists |
| Synthetic probes | A scripted journey, constant rate | Real user variety | Low-traffic services; end-to-end checks (3.b) |
Request-based versus window-based SLIs#
- Request-based:
good requests / valid requestsover the SLO window. Use when traffic is steady. - Window-based (time-slice): divide time into 1-minute slices, call a slice good if, say,
its p99 was under 400 ms or its error rate under 1%, then
good slices / total slices. Use when traffic is very low or very spiky, where a handful of requests would otherwise dominate.
A worked example#
Checkout API. Specification: “Checkout requests succeed within 1 second.” The raw access log has ten requests:
| # | Path | Status | Duration | Valid? | Good? |
|---|---|---|---|---|---|
| 1 | /checkout | 200 | 230 ms | yes | yes |
| 2 | /healthz | 200 | 2 ms | no — health check | — |
| 3 | /checkout | 400 | 15 ms | no — malformed request | — |
| 4 | /checkout | 200 | 1.8 s | yes | no — too slow |
| 5 | /checkout | 503 | 30 ms | yes | no |
| 6 | /checkout | 200 | 640 ms | yes | yes |
| 7 | /checkout | 429 | 5 ms | yes — we shed load | no |
| 8 | /checkout | 200 | 310 ms | yes | yes |
| 9 | /checkout | — (reset) | 30 s | yes | no — timeout |
| 10 | /checkout | 200 | 410 ms | yes | yes |
Valid: 8. Good: 4. SLI = 50%. A naive “non-5xx over all lines” would say 9 of 10, 90% — wrong on every count. The program below implements the correct rules.
Code#
// sli.go — compute availability and latency SLIs from access log lines, with exclusions.
package main
import (
"bufio"
"fmt"
"strconv"
"strings"
"time"
)
// method path status duration ; status "-" means the connection broke with no response.
const accessLog = `POST /checkout 200 230ms
GET /healthz 200 2ms
POST /checkout 400 15ms
POST /checkout 200 1800ms
POST /checkout 503 30ms
POST /checkout 200 640ms
POST /checkout 429 5ms
POST /checkout 200 310ms
POST /checkout - 30000ms
POST /checkout 200 410ms`
type event struct {
path string
status int // 0 = no response
duration time.Duration
}
func parse(line string) (event, error) {
f := strings.Fields(line)
if len(f) != 4 {
return event{}, fmt.Errorf("bad line %q", line)
}
d, err := time.ParseDuration(f[3])
if err != nil {
return event{}, err
}
st := 0
if f[2] != "-" {
if st, err = strconv.Atoi(f[2]); err != nil {
return event{}, err
}
}
return event{f[1], st, d}, nil
}
// valid excludes traffic that is not a user's attempt at the journey, and the user's own mistakes.
func valid(e event) bool {
if e.path == "/healthz" {
return false
}
if e.status >= 400 && e.status < 500 && e.status != 429 { // 429: our capacity, our fault
return false
}
return true
}
func available(e event) bool { return e.status != 0 && e.status < 500 && e.status != 429 }
func main() {
const threshold = time.Second
var validN, availN, fastN, naiveGood, naiveAll int
sc := bufio.NewScanner(strings.NewReader(accessLog))
for sc.Scan() {
e, err := parse(sc.Text())
if err != nil {
fmt.Println(err)
continue
}
naiveAll++
if e.status != 0 && e.status < 500 {
naiveGood++
}
if !valid(e) {
continue
}
validN++
if available(e) {
availN++
if e.duration < threshold {
fastN++
}
}
}
fmt.Printf("valid events: %d\n", validN)
fmt.Printf("availability SLI: %.1f%%\n", 100*float64(availN)/float64(validN))
fmt.Printf("good (available and <%v): %.1f%% ← the checkout SLI\n", threshold, 100*float64(fastN)/float64(validN))
fmt.Printf("naive non-5xx / all lines: %.1f%% ← what a careless SLI reports\n", 100*float64(naiveGood)/float64(naiveAll))
}Remember this#
- Start from critical user journeys; pick 3–5.
- SLI = good events ÷ valid events. Write the specification in words and the implementation in detail.
- Measure close to the user — the load balancer is the usual default.
- Count timeouts and resets; count your own 429s; exclude health checks and client errors.
- Use thresholds for latency, never averages. Use time-slice SLIs for low-traffic services.
Try it#
- Run
sli.go. Decide that request 7 (429) should be excluded instead. How much does the SLI change, and why would that be the wrong decision for checkout? - List the critical user journeys for a product you use daily. Write one SLI specification for each.
- For one of them, write the implementation: which log or metric, which fields, which filters.
Check yourself#
- Why does a service-level metric miss the worst outages?
- Is a 429 a good or bad event? Does it depend on who caused it?
- When would you choose a time-slice SLI over a request-based one?
- Give an example of an SLI specification that has two plausible implementations with different results.
Sources#
- Google SRE workbook, chapter 2: Implementing SLOs — SLI specification vs implementation, the SLI menu
- Google SRE book, chapter 4: Service Level Objectives
- Google Cloud Architecture Center: Defining SLOs