Site Reliability Engineering — DevOps

SRE (Site Reliability Engineering) keeps an application reliable by measuring reliability as numbers. An SLI is the measured metric (requests that aren't 5xx…

The product team wants to ship a new feature this week. You object, because a week ago the Postgres disk filled up and notes-api was down for 35 minutes. An argument like this can last for hours, because both sides only have their feelings. What you need is one number agreed on in advance.

Site Reliability Engineering (SRE) is keeping an application reliable with a software engineering approach, which means measuring, writing code and automating. Reliability is measured as a number and given a target. The gap between that target and 100% is the "allowance for failure". What is left of it decides when you may ship features, and when you must stop and fix things. The discipline was born at Google. Picture an intercity bus company that sets itself a target: "99% of departures on time". They count on-time departures divided by all departures. Out of 3,000 departures a month, 1%, or 30 departures, may be late. If those 30 are used up by the 20th, new routes are postponed, and every mechanic works on fixing the buses. The ticket makes a promise to passengers: if the bus is more than an hour late, they get their money back. In this picture, each departure is one request to notes-api, and the 30 late departures allowed are the "allowance for failure". A new route is a new feature, and fixing the buses is fixing reliability. The "Bus" column in the next table pairs up the rest.

SLI, SLO, SLA

These three terms get mixed up all the time, but they mean different things:

Term What it means notes-api example Bus
SLI (Service Level Indicator) the metric you measure requests that aren't 5xx ÷ all requests on-time departures ÷ all
SLO (Service Level Objective) the internal target for the SLI 99.9% per 30 days 99% per month
SLA (Service Level Agreement) a contract with customers, with consequences 99.5% per month, 10% compensation if missed money back when an hour late

Calculating from a month of data

This is notes-api's data for the last 30 days (its SLO window, the period being measured), the same as in the Lab. A few key days:

Day Requests Failed Failed so far What happened
1 89,770 20 20 an ordinary day
8 92,180 20 197 an ordinary day
9 88,980 975 1,172 a deploy with a misspelt table name, 21 minutes of errors
18 61,400 25 1,421 a Sunday
19 92,100 2,271 3,692 Postgres' disk full, 35 minutes down
30 94,100 28 3,973 end of the window

The whole month has 2,506,020 requests, and 3,973 of them failed. The SLI: (1 − 3,973 ÷ 2,506,020) × 100 = 99.84%. It sounds high, but it is under 99.9%, so the SLO is not met. 99.84% and 99.9% look close, but what you have to compare is the remainder, the part that failed: 0.16% failed is 1.6 times the 0.1% allowance.

Error budget

The error budget is the "allowance for failure" from the bus company picture: how much failure the SLO still allows, which is 100% − SLO. For a 99.9% SLO, the error budget is 0.1%.

The budget used is the number of failed requests so far ÷ 2,506. It grows over the month: by day 8 only 8%, after day 9's incident 47%, by day 18 57%, and day 19's incident takes it to 147%: gone. The stricter the SLO, the smaller the budget and the sooner it is used up:

SLO 30-day error budget In this month's requests Used up on day
99% 7 hours 12 minutes 25,060 never (16%)
99.5% 3 hours 36 minutes 12,530 never (32%)
99.9% 43 minutes 2,506 19
99.99% 4 minutes 250 9

When the budget is used up

A team writes an error budget policy (rules for what to do when the budget is used up) before any incident, so the decision isn't argued over in a panic. This is an example policy from Google's SRE books, applied to notes-api:

On the other hand, a budget with plenty left may be spent on taking risks: shipping more often, or trying a new feature behind a feature flag. With an error budget, the product team and the operations team (the people who keep the servers running) share one number, instead of each side pulling its own way.

Toil

Toil is operational work that is manual, repetitive, automatable, adds no lasting value, and grows as the application grows. Google's SRE teams have a goal of keeping toil below 50% of each SRE's time (an SRE is also what a person in this job is called). The rest goes to engineering that removes that toil, such as writing the automation for it. Along this track we have already removed some toil:

Toil Replaced with
SSH into web-1 to deploy by hand a CI/CD pipeline
Restarting the app after a crash in the middle of the night systemd Restart= or restart: unless-stopped
Renewing the TLS certificate before it expires (90 days, shorter from 2027) certbot with an automatic timer
Running pg_dump yourself a 0 2 * * * cron and a restore drill

Calculating the error budget in JavaScript

In the exercise, calculateErrorBudget(sloPercent, periodDays) returns { slo, errorBudgetPercent, downtimeMinutes, downtimeFormatted }. There is one JavaScript trap: 100 - 99.9 gives 0.09999999999999432, not 0.1, because decimals are stored as floating point numbers, in binary (as 0s and 1s). Round to 2 decimal places with Number((100 - sloPercent).toFixed(2)): toFixed(2) makes a string with 2 decimal places, then Number turns it back into a number. Then:

⚠️ Targets and sums that mislead

TL;DR: SRE (Site Reliability Engineering) keeps an application reliable by measuring reliability as numbers. An SLI is the measured metric (requests that aren't 5xx ÷ all requests). An SLO is the internal target (99.9% per 30 days). An SLA is a contract with customers that is looser than the SLO and has consequences. Error budget (the allowance for failure) = 100% − SLO: for notes-api, 2,506 requests or 43 minutes a month. This month 3,973 requests failed, the SLI is 99.84%, and the budget was used up on day 19. When the budget is used up, the error budget policy (rules written before any incident) freezes feature releases until the SLI recovers. A budget with room left may be spent on taking risks. Toil (repetitive manual work) is replaced by automation, with a goal of keeping it below 50% of an SRE's time. In JavaScript, 100 - 99.9 isn't 0.1, so round 100 - SLO with toFixed(2).

⚡ SLI, SLO, SLA

Write:

SLI   requests that aren't 5xx ÷ all requests
SLO   99.9% per 30 days
SLA   99.5% per month, 10% compensation if missed

Meaning: These are three reliability numbers for notes-api. An SLI is the metric you measure, an SLO the internal target, an SLA a contract with customers that has consequences. The SLA is always looser than the SLO, and a 100% target is wrong.

⚡ Calculate it from a month of data

Guess first: a month has 2,506,020 requests and 3,973 failed, so the SLI is 99.84%. Is a 99.9% SLO met?

It isn't. What you compare is the part that failed: 0.16% failed is 1.6 times the 0.1% allowance (100% − 99.9%). It looks close, yet the allowance for failure (the error budget) was used up on day 19.

Meaning: Compare the failures, not the successes.

⚡ Error budget = 100% − SLO

Write:

const errorBudgetPercent = Number((100 - sloPercent).toFixed(2));

Meaning: The error budget is the allowance for failure the SLO still leaves, and sloPercent is the SLO in percent. For 99.9% over 30 days, the allowance is 2,506 failed requests or 43 minutes of downtime (time down). In JavaScript, 100 - 99.9 isn't 0.1, so round it first with toFixed(2).

⚡ When the budget is used up

Meaning: The error budget policy is a set of rules written before any incident. While the SLI is under the SLO, feature releases are frozen and only fixes may deploy. A budget with room left may be spent on taking risks. Toil, repetitive manual work, is replaced by automation.

What you will learn