ealeng

Episode 53 of 55 · Operations

Cost per request: halving the app's CPU saves a tenth of the bill, and alert rules can be tested like code

A million redirects on a URL shortener cost 11.9 cents to provision for a 3× peak all day, or 9.5 cents with capacity that follows the traffic. Storing links for five years was the biggest line at 40%, egress 30% and all the compute 30%, so halving the app's CPU saves about a tenth. The one-hour page rule was generated from the SLO and tested with promtool, which fired within 2 minutes of the calculator at every burn, and live every page started and stopped within 2.6 seconds of it.

Lab 21 · SLOs and Observability12 min read
Video transcript9:26

0:00Cold open

0:01

Two tests this time. First, my alarms, written as code, checked against my own math.

0:08

Live, every page fired, and cleared, within three seconds of when the calculator said it should.

0:16

Second, the bill. A million redirects on my URL shortener cost about twelve cents.

0:23

And all the computing in that was under a third. Making the app use half the processor time would save about a tenth, in this model.

0:33

So how do you know an alert works before the night it has to? And where does the money really go?

0:41Intro

0:42

Hands-on system design, part fifty-three.

0:46

Today: alerts as code, tested like code, and what a million requests cost, part by part.

0:53Meet Lucía

0:55

I’m Lucía, a platform engineer. Checkout has an objective, an SLO: ninety-nine and a half percent of checkouts good, over twenty-eight days. Good means answered within three tenths of a second. The half percent that may fail is the error budget.

1:10

The burn rate is how fast that budget is being spent. At a burn of one, it lasts exactly twenty-eight days. An alert pages when it burns much faster.

1:22

Prometheus is the monitoring system. It reads running totals from the services, here every second, and checks rules. A recording rule keeps a number fresh; an alerting rule fires, and a page wakes someone.

1:37

Think of a smoke detector. The alerting rule is the detector. A rule tester is its test button, with fake smoke. And a cost per request is the electricity bill, split by appliance.

1:5101 · What you need

1:52

To run it yourself: Docker, for Prometheus and the checkout’s services, all on one laptop.

2:00

The quick tests check the SLO math and the cost model, without Docker, in a couple of seconds.

2:0802 · Rules as code

2:09

A small program writes the Prometheus rules from the SLO’s own definition. The same definition drives my calculator, a short script that replays every request and does the exact math, so the two can’t drift apart.

2:25

Here’s the one-hour page. It fires when the share of bad checkouts over the last hour is above six point seven two percent, and over the last five minutes too.

2:37

Where does that come from? Twenty-eight days is six hundred seventy-two hours. Spending two percent of the budget in one hour is spending it thirteen point four four times too fast. Times the half-percent budget: six point seven two percent.

2:54

The short five-minute window is there so the alert clears soon after the problem stops.

3:0103 · The test button

3:03

First, the test button: promtool, Prometheus’s own rule tester. I fed it fake counters: three days of normal traffic, then a constant burn, checked minute by minute.

3:16

At a burn of twenty, it paged after forty minutes. At two hundred, a full outage, after four. The formula says forty point three, and four.

3:26

At a burn of five, the six-hour page never fired. That’s below its threshold of five point six: a burn that slow should open a ticket, not wake anyone.

3:38

My calculator fired two minutes after promtool each time. One minute is a bug in my replay: it started the burn a minute late. The other is Prometheus: at one sample a minute, its last hour really spans fifty-nine minutes, so it crosses the line a minute sooner.

3:5804 · Live

3:59

Second, live. The real Prometheus, reading the real checkout every second. To fit in fifteen minutes, every window is divided by sixty: one hour becomes one minute.

4:12

Payments then fails some charges: first five percent, later half. My calculator replays the requests the test client sent, timed by the same clock.

4:23

How close do you think Prometheus came?

4:28

Every page started and stopped within three seconds of the calculator. Prometheus was always a second or two later, because it only reads and checks once a second. The five percent, a burn of ten, set off the six-hour page after two hundred six seconds; the formula says two hundred two.

4:49

At half failing, the six-hour page was back within five seconds, and the one-hour page fired after twelve; the formula says eight. The one-hour page cleared six seconds after the failures stopped: that’s its short window.

5:0505 · Already open

5:06

One surprise. The ticket was already firing when the run began.

5:12

The ticket fires when ten percent of the budget goes in three days. Divided by sixty, its long window is seventy-two minutes and its short one six. Both still held a load test that had ended about a minute before, with plenty of slow checkouts. My calculator’s windows start at this run, so they only held a few minutes.

5:33

Long windows remember. For three days after an incident, a small new burn reopens the ticket much sooner than it otherwise would. That’s correct, and surprising if you don’t expect it.

5:46

So test the rules like code. A typo in a window doesn’t show until the night it should have paged. A rule tester catches it before.

5:5706 · Pricing a request

5:58

Now the bill, for the URL shortener from earlier in the series. I measured how much processor time each part used per redirect, at three thousand a second, and priced it at AWS’s list price, as plain containers.

6:14

Then the bytes each redirect sends out to the internet, called egress: about four hundred, the design’s estimate. And the disk: each new link, five hundred bytes, kept twice for five years.

6:27

Two assumptions matter: machines run sixty percent busy, and the busiest hour is three times the average. The cache and the database are priced as containers too; managed versions usually cost more, which would make computing a bigger share.

6:4507 · Where it goes

6:47

A million redirects: about twelve cents. Which part do you think costs the most?

6:54

Not the computing. The biggest slice, forty percent, is storing the new links for five years: four point eight cents. Sending the responses out: three point six cents, thirty.

7:07

All the computing together is the other thirty percent. The app itself, about twenty.

7:13

So an app using half the processor time saves about a tenth here; with capacity that follows traffic, nearer four percent. A smaller response, or keeping links for less time, would save more.

7:2808 · Idle for the peak

7:30

And the computing line is mostly idle machines. Sized for the busiest hour, all day long: three point six cents per million.

7:39

If capacity followed the traffic: about one point two. A peak three times the average, assumed here, means you pay for three times what the average needs. That’s what autoscaling, from part forty-two, is for, when it keeps up.

7:56

On screen, the monthly figure for the design’s average traffic. It counts each new link’s full five years of storage, so it isn’t one month’s invoice.

8:0809 · Tracing’s bill

8:09

The checkout from the tracing episode, processor time only, capacity following traffic: about six cents per million with tracing off, seven and a half with every checkout traced.

8:22

Tracing everything raised the processor bill by twenty-seven percent in the services alone, before the collector and before storing the traces.

8:33Back to Lucía

8:35

So, three lessons.

8:38

Write alerts as code, from one SLO definition, and test them like code.

8:44

Long windows remember: a ticket can outlive the incident that opened it.

8:50

And price a request by its parts. Here, storing links was the biggest part, sending bytes out tied with all the computing, and paying for the peak all day multiplies the computing by the peak-to-average ratio, three here.

9:06

Next time: clocks. What time is it, on two machines at once?

9:11

For today: press the test button, and read the bill.Lucía: Press the test button. Read the bill.

9:16Summary

9:17

Generate alerts from the SLO, test them like code, and price a request by its parts.

Key takeaways

  • ✓Cost per request is the bill divided by the requests: in the lab's model, a million redirects of a URL shortener cost 11.9 cents with capacity provisioned for a 3× peak all day, and 9.5 cents with capacity that follows the traffic.
  • ✓The largest line was not compute: storing the new links for five years cost 4.8 cents (40%), sending 400-byte responses to the internet cost 3.6 cents (30%), and all the compute together cost 3.6 cents (30%).
  • ✓Halving the app processes' CPU saves about 1.2 of the 11.9 cents, a tenth of the bill, because the app is only 20% of it; with capacity that follows the traffic, the same halving saves about 4% of 9.5 cents. That saving is arithmetic on the cost model, not a measured result.
  • ✓The one-hour page rule fires when more than 6.72% of checkouts are bad over the last hour and the last five minutes, which is 2% of a 28-day error budget spent in one hour (a burn of 13.44, times the 0.5% the budget allows).
  • ✓promtool, Prometheus's own rule tester, fired that page after 40 minutes at a burn of 20, 16 minutes at 50 and 4 minutes at 200, matching the formula's 40.3, 16.1 and 4.0 minutes; the calculator fired two minutes later each time.
  • ✓Live, with every window divided by 60 so that the run takes 15 minutes, each page started and stopped within 2.6 seconds of the calculator's prediction, and the ticket was already firing at the start because long windows remember an earlier failure.

A million redirects on the URL shortener from earlier in the series cost about 12 cents in this model, and under a third of that was computing. Halving the app's processor time would save about a tenth. This chapter does two things. It prices a request by its parts, to see where the money goes, and it writes the alert rules for the SLO from code and tests them, the way you would test any other code.

The examples come from Lab 21 (SLOs and Observability) of the system design labs: script 06_cost.py for the bill, and script 05_rules.py for the alerts. Both ran on the same laptop setup as the earlier chapters, an Apple M4 Max with Docker Desktop and 16 virtual CPUs. Prometheus, the checkout services and the load tests all ran in Docker on that one laptop. Each result is one recorded run.

Cost per request

Cost per request is the bill divided by the requests. The lab computes it per million requests, which keeps the numbers readable. Each part of the bill has a rule for turning a measurement into dollars:

  • Compute. Measured CPU-seconds per request, divided by the share of the machine you actually use (the lab assumes 60% utilization), times the price of a vCPU-hour. The lab prices containers on AWS Fargate's ARM rate, plus 2 GB of memory per vCPU.
  • Egress. The bytes each response sends out to the internet, times the price per gigabyte. The design estimates 400 bytes a redirect. For a million redirects, that is 0.4 GB, and at $0.09 a gigabyte it costs $0.036, or 3.6 cents.
  • Storage. The bytes each new link writes, times the number of copies, times the price per gigabyte-month, times the months kept. The design writes 500 bytes per link, one link per 100 redirects, two copies, for 60 months at $0.08 per gigabyte-month. That is 4.8 cents per million redirects.

Every price is an AWS list price for us-east-1, fetched on 4 November 2026. None of the prices is a negotiated or account-specific price.

Why the biggest line is not the CPU

Here is the bill for a million redirects, with capacity provisioned for a busy hour that is three times the average (the design's peak-to-average ratio), priced as in the lab:

Part of the bill CPU per redirect Cents per million, provisioned for the peak Share Cents per million, capacity follows traffic Share
Link storage, 5 years, 2 copies 4.8 40% 4.8 50%
Egress, 400 bytes a redirect 3.6 30% 3.6 38%
App processes 0.440 ms 2.4 20% 0.8 8%
HAProxy 0.081 ms 0.4 4% 0.1 2%
Redis 0.068 ms 0.4 3% 0.1 1%
Postgres, primary and replica 0.063 ms 0.3 3% 0.1 1%
Total 0.651 ms 11.9 9.5

Storage and egress are bytes, so their cost follows what the design writes and sends, not how busy the processors are. Storage is the largest line because links are kept for five years on two copies. Egress is the second because every redirect sends a response out. All the compute together is 3.6 cents, the same as egress. The app processes are the biggest compute part, at 2.4 cents.

Nothing in this table is a managed service. The lab prices Redis, Postgres and HAProxy as containers. A real deployment would use ElastiCache, RDS and a load balancer, each with its own price, which would make the compute share larger.

Try it · Where a million redirects' bill goesLab 21 results, cents per million redirects
  • Link storage, 5 years, 2 copies4.8¢40%
  • Egress, 400 bytes a redirect3.6¢30%
  • App processes2.4¢20%
  • HAProxy0.4¢4%
  • Redis0.4¢3%
  • Postgres primary0.0¢0%
  • Postgres replica0.3¢3%
Per million redirects
11.9¢
Saved by halving app CPU
—
Recorded total
11.9¢

Recorded: every bar, both capacity modes, and the recorded totals, from the lab's 06_cost.py run (60% utilization, 400 bytes of egress per redirect, 500 bytes per new link kept twice for five years). Arithmetic: the halved app line and the saving. Prices are AWS list prices from 4 November 2026. Nothing here was re-measured.

Halving the CPU saves a tenth

Halving the app's processor time is a small change to the bill. The app's compute line is 2.4 cents at peak provisioning, so halving it saves 1.2 cents of the 11.9, about a tenth. With capacity that follows the traffic, the app's line is 0.8 cents of 9.5, so the same halving saves about 4%.

The lesson is the shape of the bill, not the number. Making the code faster is worth doing, but a smaller response, or links kept for less time, would save more than halving the CPU here. The lab's model is linear in CPU, so this halving is arithmetic on its output, not a measured result.

Idle machines for the peak

The compute line is mostly idle machines. Provisioned for the busiest hour and kept all day, the compute costs 3.6 cents per million. If capacity followed the traffic, it would cost about 1.2 cents. The ratio between them is the peak-to-average ratio of 3, which the model assumes. You pay for three times what the average needs.

Autoscaling is what closes that gap, when it keeps up with the traffic. The lab's monthly figures make the same point at scale: at the design's average of about 1,169 requests a second, the bill is $361 a month provisioned for the peak and $289 following the traffic. Those monthly figures include each new link's full five years of storage, so they are not one month's invoice.

What tracing costs

The checkout pipeline in the lab has seven services. Its processor time per checkout is 3.29 ms with tracing off, 3.95 ms with 10% of checkouts traced, and 4.19 ms with every checkout traced. Priced as in the rest of the chapter, with capacity following traffic, that is 6.0, 7.2 and 7.7 cents per million checkouts (7.65 in the results). Tracing every checkout raised the processor bill by 27%, before the collector and before storing the traces.

SLO alert rules, generated from code

The SLO is 99.5% of checkouts good, over 28 days. A checkout is good if it is answered with a success within 300 ms. The error budget is what the SLO allows to be bad: 0.5%. The burn rate is how fast the budget is being spent. A burn of 1 spends it in exactly 28 days, and a burn of 200 is a full outage.

The lab keeps the SLO in one place, in slo.py. From it, tools/gen_rules.py writes the Prometheus rules that Prometheus evaluates, and the calculator in the same module replays the same requests with the exact arithmetic. Both come from one definition, so they cannot drift apart.

There are three alert rules, each defined by the share of the budget it should catch:

  • The one-hour page fires when 2% of the 28-day budget is spent in one hour. That is a burn of 2% × 672 hours ÷ 1 hour = 13.44, and 13.44 × 0.5% = 6.72% of checkouts bad. It also needs the last five minutes to be above the same line, so the alert clears soon after the problem stops.
  • The six-hour page fires when 5% of the budget is spent in six hours. That is a burn of 5.6, or 2.8% of checkouts bad.
  • The three-day ticket fires when 10% of the budget is spent in three days, a burn of about 0.93, or 0.467% of checkouts bad. It opens a ticket, not a page, because nothing needs a person tonight.

The generated Prometheus file holds the recording rules, which keep the error ratio over each window fresh, and the three alerts. Each alert is a plain expression. The one-hour page is rate1h > 0.0672 and rate5m > 0.0672 in the lab's file, with the same threshold written as a share of checkouts. A typo in a window or a threshold would show up only on the night it should have paged, so the lab tests the rules before that happens. The rate1h and rate5m in that expression are the error ratio over the last hour and the last five minutes.

Test one: promtool, on fake counters

promtool is Prometheus's own rule tester. The lab feeds it synthetic counters at one-minute samples: three days of normal traffic, then a constant burn, checking each minute for when the one-hour page fires.

Burn promtool: page fired after Calculator: page fired after Formula
5 never never never (5 is under the six-hour page's 5.6)
20 40 min 42 min 40.3 min
50 16 min 18 min 16.1 min
200 4 min 6 min 4.0 min

promtool matched the formula to the minute. The calculator fired two minutes later at every burn. The lab's README calls it a fixed offset in how the two count a minute's samples, because Prometheus's rate() extrapolates to the edges of its window. Burn 5 never paged, which is right: that burn belongs on the ticket.

Test two: live, against the calculator

The live test runs the real Prometheus, scraping the real gateway every second, with the same rules scaled down. Every window is divided by 60, so one hour becomes one minute and the run takes 15 minutes. The load is 200 checkouts a second. Payments fails 5% of charges from 420 to 720 seconds, and then 50% from 780 to 825 seconds. The calculator replays the client's own record of the same requests.

Alert Prometheus: firing, seconds into the run Calculator, same requests Fault
Six-hour page 625.6 to 735.4 624 to 733 5% from 420 s (a burn of 10)
Six-hour page, again 784.6 to 855.3 782 to 854 50% from 780 s
One-hour page 791.6 to 831.2 789 to 830 50% from 780 s to 825 s
Ticket firing at the start, off at 197 s, on again at 457 s on from 464 s

Every page started and stopped within 2.6 seconds of the calculator. The six-hour page took 206 seconds to fire after the 5% failure began, where the formula says 202. The one-hour page fired 12 seconds after the 50% failure began, where the formula says 8 plus the time for Prometheus to scrape and evaluate, and it cleared 6 seconds after the failures stopped, because of its short window.

The ticket is the surprise. It was already firing when the run began, because its 72-minute window at this scale still held the failed checkouts from an earlier test. Prometheus remembered them and the calculator's record of this run did not. Long windows remember: for days after an incident, a small new burn can reopen a ticket much sooner than you would expect.

The workflow and the results

Rules. 05_rules.py generates the rules, runs the promtool tests, and runs the live Prometheus for about 16 minutes. Its results are in data/results/05_rules.json. Each live alert's start and end times are recorded in seconds, next to the calculator's times for the same requests.

Cost. 06_cost.py takes about a second. It reads the CPU per redirect from Capstone A's load run at 3,000 redirects a second (Capstone A is the URL shortener design in the capstone labs), adds the design's egress and storage figures, and prices everything with the list prices in data/prices.json. Its results are in data/results/06_cost.json. The price file can be refreshed with tools/fetch_prices.py, which needs read-only AWS credentials.

Caveats

  • List prices from one day. The prices are AWS us-east-1 on-demand list prices from 4 November 2026. They are not negotiated, not account prices, and not current as of this chapter.
  • Measured versus assumed. The CPU per request and the live alert times were measured, once each. The 60% utilization, the 3× peak, the 400-byte responses, and the 500-byte links kept twice for five years are assumptions or the design's estimates, not measurements.
  • Containers, not managed services. Redis, Postgres and HAProxy are priced as containers on Fargate. Managed services have their own prices and minimums, and they would change the shares.
  • Only what the model prices. Requests charges, cross-zone traffic and other line items are not in the model. The compute shares could also change with a real load balancer.
  • The 2-minute offset has one explanation in the lab. The README attributes it to how the calculator and Prometheus count a minute's samples. It is the same at every burn, and the rules match the formula either way.
  • The ticket is outside the 3-second claim. The 3-second bound applies to the pages. The ticket's recorded restart at 457 seconds came 7.5 seconds before the calculator's 464.
  • One recorded run. Each table is one run on one laptop, and the live numbers come from a single 15-minute run.

The checklist

  1. Write the SLO once, and generate the alerts from it. The calculator, the Prometheus rules and the dashboard should all come from the same definition.
  2. Test alert rules like code. promtool on synthetic counters, run before the rules ship, catches a wrong window or threshold before the night it matters.
  3. Expect long windows to remember. A ticket can outlive the incident that opened it, and that is correct behavior.
  4. Price a request by its parts. Storage, egress and compute are different lines with different levers: retention, response size and autoscaling, and faster code.
  5. Check the peak multiplier. If capacity is sized for a peak that is three times the average and kept all day, that multiplier is the first thing to look at, before the code.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.In the lab's model, what was the largest part of the bill for a million redirects?

  2. 2.The app's CPU is halved in the model. Roughly what share of the bill does that save when capacity is provisioned for the peak?

  3. 3.Why was the ticket alert already firing when the live run began, although nothing was failing?

Check your understanding

Try each question before opening its answer.

Why is storage the biggest line in the lab's bill, when the app's processes are the biggest compute line?

Storage is 4.8 cents per million redirects because each new link is kept for five years on two copies, and the design writes one link per 100 redirects. The app processes cost 2.4 cents at peak provisioning, and they are the largest compute line. Storage and egress depend on bytes, not on how busy the processors are, so making the code faster leaves them unchanged.

When capacity follows the traffic, halving the app's CPU saves about 4% of the bill, not a tenth. Why the difference?

The tenth comes from provisioning for the busiest hour all day, which makes the app's compute line 2.4 cents of 11.9. With capacity following the traffic, the 3× headroom is not multiplied in, so the app's line is 0.8 cents of 9.5. Halving it saves 0.4 cents, about 4%.

The six-hour page fired 206 seconds after the 5% failure began, but the formula says 202. Is that a problem?

Not really. The formula is the time for the six-hour window's bad share to reach the 2.8% line. The extra four seconds are the scrape and evaluation delay of a live Prometheus, which reads and checks once a second. The lab's 3-second bound is measured against the calculator on the same requests, and this page met it.

Why does a one-minute sample in promtool matter for an alert that fires after 40 minutes?

The test checks each minute after the burn starts, and the first minute that fails is when the page would have fired. A one-minute resolution means the answer is accurate to the minute, which is enough to catch a wrong window (a 3-minute window and a 1-hour window would page at very different times). A typo in a window usually moves the page by much more than a minute, so promtool catches it.

Next up: Clock skew and logical clocks, part 54: what time is it on two machines at once, and how do you order events when the clocks disagree?

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.