ealeng

Episode 52 of 55 · Operations

Distributed tracing: metrics blamed three services, one trace found the slow one, and recording every checkout cost 27% more CPU

One of five checkout services was made 50 ms slower, in secret. Per-service metrics flagged three services in the rounds where payments was slow, because every caller's time includes waiting for the service it calls. One trace per request, read by its critical path, named the slowed service in 50 of 50 traces in every round. Then the cost: recording every checkout took 27% more CPU at 400 checkouts a second, and recording one in ten still took 20%. At 800 a second, recording everything roughly doubled the median checkout.

Lab 21 · SLOs and Observability11 min read
Video transcript8:53

0:00Cold open

0:01

A checkout in my shop crosses five services. I made one of them fifty milliseconds slower, and kept it secret from the code that looks for it.

0:11

The per-service metrics blamed three services, when only one was slow.

0:18

One trace, the record of a single checkout, named the right one. And so did every one of the next forty-nine.

0:27

Then the bill: on my laptop, recording every checkout took about a quarter more processor time.

0:34

So why do the numbers point at too many services, and what does it cost to see the right one?

0:42Intro

0:43

Hands-on system design, part fifty-two.

0:47

Today: tracing. What metrics can’t tell you about a slow service, what a trace can, and what tracing costs.

0:55Meet Lucía

0:57

I’m Lucía, a platform engineer. A checkout goes through a gateway, then orders, which asks inventory and pricing at the same time, then payments.

1:07

After the answer has gone back, two more services finish the job: a relay, which picks up new orders from the database, and a notifier that sends “order placed”.

1:18

Metrics are numbers per service, counted over many requests: for example, its typical time to answer, called the p50.

1:28

A trace is one request’s whole journey. It’s made of spans: one per step, with which service, when it started and when it ended.

1:37

Think of a parcel through five depots. Metrics are each depot’s typical time, waiting included. A trace is one parcel’s tracking log. The critical path: the stops it actually waited on.

1:5201 · What you need

1:53

To run it yourself: Docker, for the services, Prometheus, which collects the metrics, and Jaeger, which stores the traces. The services record their spans with OpenTelemetry, an open standard for tracing, with a library for each language.

2:10

The quick tests, including the code that reads a trace, run without Docker in a couple of seconds. No cloud, nothing to pay for.

2:2102 · The test

2:22

The test: six rounds of a hundred checkouts a second, every one traced. First a normal round, then each service in turn made fifty milliseconds slower, in a shuffled order.

2:35

Reading the metrics: compare each service’s typical time with the normal round. Up by more than forty milliseconds: a suspect.

2:44

Reading the traces: one trace at a time, no comparison. Blame the service with the most time on that request’s critical path.

2:54

Both readings are done by code, standing in for a person looking at a dashboard. They show what each view contains, not how fast a person would read it.

3:0603 · The metrics

3:07

Take the round where payments was the slow one.

3:12

How many services do you think the metrics flagged?

3:17

Three: the gateway, orders and payments. All three rose about the same. The same happened when inventory or pricing was slow; when orders was slow, two; when the gateway was slow, just one.

3:32

Why? A service’s time includes waiting for the services it calls. When payments is fifty milliseconds slower, orders waits fifty more, and so does the gateway. The numbers alone don’t say who called whom, so all three look guilty.

3:4904 · The buckets

3:50

And notice: in the gateway’s numbers, a fifty-millisecond delay showed up as sixty-seven and a half.

3:58

Each service doesn’t keep every time. It counts requests in buckets: under two milliseconds, two to five, five to ten, and so on up to fifty to a hundred.

4:10

Nearly every request fell in one bucket, so the best guess is its middle: seven and a half before, seventy-five after. A rise of sixty-seven and a half. So the bucket edges, not the delay, set the number you see.

4:2605 · One trace

4:27

Now one trace from the same round. Of the time this checkout waited, payments took fifty-four milliseconds. Everyone else, two or less.

4:37

All fifty traces I read in that round named payments. And in every other round, the same: fifty of fifty, right.

4:47

The callers’ own work, the time they weren’t waiting on a call, didn’t change. Only their waiting did, and the trace shows where the waiting went.

4:56

To be fair: the delay was a pause inside that service’s own step, which normally takes three or four milliseconds. The trace couldn’t miss it. A real slowdown is rarely that clean.

5:1006 · In code

5:12

In code: start at the end of a step, and find the call that finished last before it. That’s what the step was waiting for.

5:20

The time after that call ended is the step’s own. Then do the same inside that call.

5:27

Then move back to when that call started, and ask again. With inventory and pricing running at once, only the one that finished last is on the path while they overlap.

5:4007 · After the answer

5:41

A trace always has a top, even when nothing is wrong. In the normal round it was payments, waiting three milliseconds on the card network. A top only means something next to a normal trace.

5:55

And the same trace kept going after the answer: the “order placed” message went out typically sixty-one to seventy-six milliseconds after the checkout returned, depending on the round.

6:08

Most of that is the relay: it looks for new orders every hundred milliseconds, so a new one waits about fifty on average.

6:1708 · The cost

6:19

Now the price, at four hundred checkouts a second, four times the test’s rate, starting with tracing off.

6:27

Just switching the tracing library on, recording nothing: ten percent more processor time.

6:34

Recording only one checkout in ten, called sampling: how much do you think that saves, compared with recording all of them?

6:44

Not much. Every checkout recorded: twenty-seven percent more processor time, and about a millisecond on the typical checkout. One in ten: still twenty percent.

6:57

Every request still builds its spans and passes the trace’s id along: that alone was ten percent. Recording one in ten still cost another ten points, more than its share, and this run can’t tell me why. Don’t count on sampling to cut processor time.

7:1609 · Closer to full

7:17

At eight hundred a second, leaving out the repeats that stalled, recording everything roughly doubled the typical checkout: about nine to seventeen. Pricing was using nine tenths of a core, so extra work turns into waiting in line.

7:33

The slowest one in a hundred wasn’t clean on this busy laptop: it swung several times over between repeats, and in the repeat that stalled, with tracing off, the slowest one in a hundred took over five seconds.

7:48

And the processor time counts the services only. The collector and Jaeger, which receive and store the traces, come on top.

7:58Back to Lucía

7:59

So, three lessons.

8:02

Per-service metrics point at a slow service and everyone who calls it.

8:08

A trace shows where one request actually waited. Read it next to a normal one.

8:15

And tracing everything costs real processor time. Sampling saves less processor time than you’d think; its bigger saving is likely storage, which I didn’t measure.

8:27

Next time: the alerts from last time, written as Prometheus rules and tested like code. And what a million requests cost.

8:36

For today: when the numbers point at three services, follow one parcel.Lucía: Follow one parcel.

8:42Summary

8:44

Metrics blame the callers too, one trace finds the slow step, and recording every request has a price.

Key takeaways

  • ✓Per-service metrics blame every caller of a slow service: in the rounds where payments was 50 ms slower, the gateway, orders and payments all showed a p50 rise of 67 to 71 ms, because each service's time includes waiting for the services it calls.
  • ✓A trace is one request as a tree of timed steps (spans), and its critical path is the chain of steps that request actually waited on: in the lab, the top of the critical path was the slowed service in 50 of 50 traces in each of the five slowed rounds.
  • ✓Recording every checkout at 400 a second cost 27% more CPU per checkout (3.29 ms to 4.19 ms, across the seven service processes) and about 1 ms more at the median (8.27 ms to 9.31 ms).
  • ✓Switching the tracing library on while recording nothing cost 10% more CPU, and recording one checkout in ten cost 20%, so sampling saved about 7 points of CPU in this run; the bigger saving that sampling offers is storage, which the lab did not measure.
  • ✓At 800 checkouts a second, recording everything roughly doubled the median repetition's p50, from about 8.7 ms to about 16.9 ms, because pricing was using about 0.9 of a core and extra work turned into waiting in line.
  • ✓The 50 of 50 result is the easy case by construction: the 50 ms was a planted pause inside the slowed service's own step, which normally takes three or four ms, so a real slowdown is rarely this clean.

A checkout passed through five services. One of them was made 50 ms slower, and the code that looks for it was not told which. The per-service metrics flagged three services in the rounds where payments was the slow one. One trace per request named the right service every time. Then the bill: recording every checkout took about a quarter more processor time. This chapter takes the two views apart, shows what each one can and cannot say, and then measures what tracing costs.

The examples come from Lab 21 (SLOs and Observability) of the system design labs. The scripts are 03_tracing.py for the slow-service test and 04_overhead.py for the cost, and their recorded results are in data/results/. The lab ran on an Apple M4 Max with Docker Desktop (16 virtual CPUs). The five services, Prometheus, Jaeger and the collector all ran in Docker on that one laptop. Each number below is from one recorded run of the lab, so it is a measurement of this setup, not a general claim about tracing.

The checkout path

A checkout goes through a gateway, which hands it to orders. Orders asks inventory and pricing at the same time, and then calls payments. After the answer has gone back to the user, two more services finish the job: a relay, which picks up new orders from the database every 100 ms, and a notifier, which sends the "order placed" message.

Two words recur. Prometheus stores metrics: numbers per service, counted over many requests. Jaeger stores and shows traces: one record per request. The services record their trace data with OpenTelemetry, an open standard with a library for each language. A library that records the data for a request is part of what this chapter calls tracing.

Why the metrics blame three services

A metric is a number per service, counted over many requests. The lab's metric is the p50 of each service's server time: the typical time it took to answer, where half the requests were faster and half slower. A p99 is the slowest one in a hundred.

The trouble is that a service's time includes the time it spent waiting for the services it calls. Think of a parcel moving through five depots. Each depot's typical time includes the time its parcels sat waiting for the next depot. When one depot is slow, every depot before it reports a slow typical time, so the report shows several depots in trouble, and it cannot say which one is at fault.

In the lab's runs, a 50 ms delay in payments raised three services' p50:

  • Gateway rose 67.5 ms, and orders rose 67.5 ms, though neither was slowed.
  • Payments rose 71.4 ms, the only one that had been slowed.

So the metrics named three suspects, using a rule of a p50 rise above 40 ms. When orders was slowed, the two suspects were the gateway and orders itself, because inventory, pricing and payments are called by orders and never wait on it. When the gateway was slowed, it was the only suspect, because nothing above it waits on anything.

One detail: a 50 ms delay read as 67.5 ms. Each service does not keep every time. It counts requests in buckets, such as 50 to 100 ms, and the p50 is read off the bucket. Nearly every request fell in one bucket, so the lab's best guess is the bucket's middle: 7.5 ms before and 75 ms after, a rise of 67.5. The bucket edges set the number you see.

What a trace is

A trace is one request's whole journey. It is a tree of spans: one span per step, each with a service name, a start time and an end time. The checkout's trace in the lab had 11 spans, including the async steps that ran after the response.

A trace shows where one request spent its time, but a trace also has a problem: with parallel calls, the sum of all the spans is not the time that mattered. Inventory and pricing ran at the same time, so only the one that finished last delayed orders. The critical path is the chain of steps the request actually waited on. The lab computes it by walking backwards from the end of the request: find the call that finished last before this point, count the gap after it as the parent's own time, then jump to that call's start and repeat.

The widget below shows the two views side by side, for the same round. The left side is the metrics. The right side is one trace from that round, showing the critical-path time for each service.

Try it · Metrics vs one traceLab 21 · 03_tracing.py · recorded, seed 21

Only payments got 50 ms slower. Checkout p50 rose from 8.6 ms to 59.4 ms.

Metrics: p50 rise vs normal, ms

gateway67.5suspect
orders67.5suspect
inventory0.0
pricing0.0
payments71.4suspect

Suspect = rise above 40 ms. The 50 ms delay reads as 67–74 ms because the p50 comes from histogram buckets (50 to 100 ms).

3 suspects: gateway, orders, payments

One trace: critical-path time, ms

gateway0.4
orders2.0
inventory0.3
pricing0.9
payments54.4

The first of the round's 50 traced checkouts. Critical path: the stops this request actually waited on, which with parallel calls is one of inventory or pricing, not both.

Top: payments · in 50 of 50 traces of this round

In every round, the top of the critical path was the slowed service in 50 of 50 traces. The lab's first trace in the payments round had payments at 54.39 ms of critical path, and every other service at 2.00 ms or less. In the normal round, the top was payments at 3.27 ms, because it waits on the card network in the lab. A trace always has a top, so it is only meaningful next to a normal trace.

Two more things happened after the checkout returned. The "order placed" message went out a median of about 61 to 76 ms after the checkout returned, depending on the round. Most of that is the relay: it polls every 100 ms, so a new order waits about 50 ms on average. A trace that keeps going after the response shows this, which is part of why an asynchronous tail is worth looking at in the same trace.

The test and its workflow

The lab's test has six rounds, each 20 seconds at 100 checkouts a second, with every request traced. The first round is normal. Each of the next five slows one service by 50 ms, in a seeded random order. The script learns which service was slowed only after it has scored its readings, so the readings cannot be tuned to the answer.

The two readings are automated. For metrics, the script compares each service's p50 with the normal round and calls every service whose p50 rose by more than 40 ms a suspect. For traces, it reads one trace at a time, with no comparison, and blames the service with the most time on that trace's critical path. These are stand-ins for a person looking at a dashboard and at Jaeger. They show what each view contains, not how quickly a person would read it.

Slowed service Checkout p50 Metrics: suspects Trace: top of the critical path
none (normal) 8.6 ms none payments, 50 of 50
orders 59.6 ms gateway, orders orders, 50 of 50
pricing 59.4 ms gateway, orders, pricing pricing, 50 of 50
inventory 59.2 ms gateway, orders, inventory inventory, 50 of 50
payments 59.4 ms gateway, orders, payments payments, 50 of 50
gateway 59.6 ms gateway gateway, 50 of 50

The metrics named every caller of the slow service, and the trace named only the slow service. The slowed service's own step was the one that had grown; its callers' own time did not change. Reading the metrics alone, a person would have to know the call graph to tell the callers from the culprit.

What tracing costs

The cost test ran the same checkout load at 400 and at 800 checkouts a second, 20 seconds per run, five repetitions per mode, with the order rotated between them. It compared four modes:

  • Tracing off: no OpenTelemetry library at all.
  • Library on, nothing recorded: the library runs, but no spans are kept.
  • Record 1 in 10: one checkout in ten is recorded (this is sampling: keeping only a fraction of traces).
  • Record every checkout: all 11 spans of each checkout are built, serialized and sent to the collector (the OpenTelemetry collector, a service that receives spans and passes them on to storage).

At 400 checkouts a second, the pooled results over five repetitions were:

Mode p50 CPU per checkout (7 services) CPU vs tracing off
Tracing off 8.27 ms 3.29 ms
Library on, nothing recorded 8.64 ms 3.63 ms +10%
Record 1 in 10 9.07 ms 3.95 ms +20%
Record every checkout 9.31 ms 4.19 ms +27%

Recording every checkout cost 1.04 ms at the median, and about a quarter more processor time. Two things about sampling surprised. First, the library being on, with nothing recorded, already cost 10%: every request still creates a span that is not recorded, passes the trace's identifier along, and runs the sampler (the code that decides whether a trace is kept). Second, recording one checkout in ten still cost another 10 points, which is more than its tenth of the full 27%. In this run, the lab cannot say why, so the lesson is narrow: do not count on sampling to cut processor time. Sampling saves the recording, the sending and the storing, and the bigger saving in production is likely storage, which the lab did not measure.

At 800 checkouts a second, the pooled p50 rose from 9.87 ms with tracing off to 17.37 ms with every checkout recorded, and CPU rose 22%. Two repetitions stalled, one in each of two modes: one with tracing off had a p50 of 300 ms and 202 failed requests, and one with 1 in 10 recorded had 756 failed requests. Those stalls pull the pooled p50s up. Taking each mode's median repetition instead (computed from the lab's per-repetition file) gives about 8.7 ms with tracing off and about 16.9 ms with everything recorded. The lab's explanation is queueing: pricing used about 0.9 of a core in every fully traced repetition, so the extra work turned into waiting in line.

Try it · What tracing costsLab 21 · 04_overhead.py · recorded, 5 repetitions per mode
Tracing offbaseline
p50 ms8.27CPU ms3.29
SDK on, nothing recordedCPU +10%
p50 ms8.64CPU ms3.63
Record 1 in 10CPU +20%
p50 ms9.07CPU ms3.95
Record every checkoutCPU +27%
p50 ms9.31CPU ms4.19
Record every checkout: CPU
+27%
Record every checkout: p50
+1.0 ms

Pooled p50 over 5 repetitions, 20 s each. CPU is the processor time of the 7 service processes per checkout. The collector and Jaeger are not counted.

Caveats

  • The delay was planted, and it was clean. The 50 ms was a sleep inside the slowed service's own step, which normally takes three or four ms. By construction the trace could not miss it. A real slowdown is rarely that tidy, so the 50 of 50 result is the easy case.
  • The readings are automated. The script stood in for a person reading a dashboard and Jaeger. It does not time how long a person needs.
  • One busy laptop. The machine had a load average of about 10 to 12 on 16 CPUs during the cost runs, and other containers were running. The p99 varied about 4 times between repetitions of the same mode, and one repetition with tracing off had a p99 of 5.2 seconds. The lab reports p50 and CPU as its measurements and does not rely on the tail.
  • CPU counts only the seven service processes. The collector, which receives the spans, and Jaeger, which stores them, were not counted, so the true cost of tracing is higher.
  • Storage was not measured. The lab did not record how much data the traces took, so it cannot say how big the storage bill is or how much sampling saves there.
  • Not covered: tail-based sampling (keeping the slow and failed traces and sampling the rest), more than five services, other request types, loads above 800 a second, and the cost of the collector and Jaeger themselves.

The checklist

  1. Read the metrics as a list of callers, not culprits. A slow service raises every service above it by the same amount.
  2. Read a trace by its critical path. Walk back from the end, find the step the request waited on, and compare with a normal trace.
  3. Count tracing's fixed cost. The library on, with nothing recorded, cost 10% CPU at 400 checkouts a second, before any sampling.
  4. Measure the tail on a quiet machine. The median was stable across repetitions here; the p99 was not.
  5. Price storage separately. Sampling's bigger saving is likely in storage, and this lab did not measure it.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.When only payments is 50 ms slower, the per-service metrics also show gateway and orders about 67 ms slower. Why do the callers look guilty?

  2. 2.One trace shows payments at 54.4 ms on its critical path and every other step at 2 ms or less. What does that tell you about this request?

  3. 3.At 400 checkouts a second, recording one checkout in ten (sampling) cost about 20% more CPU per checkout than tracing off. Why not about 2%?

Check your understanding

Try each question before opening its answer.

Why did the gateway and orders show a p50 rise of about 67 ms when only payments was slowed?

Because each service's time includes the time it spent waiting for the services it calls. The gateway waits on orders, and orders waits on payments, so a 50 ms delay in payments reaches both of them. The metrics alone cannot tell which service caused the delay, because they do not show who called whom. The trace shows which step the request waited on.

A trace has payments at 54.4 ms on its critical path, and nothing else above 2 ms. Does that prove payments did 54 ms of work?

Not by itself. The critical path shows where the request waited, and in the lab the delay was a planted pause inside payments' own step, so the time was payments' own. In a real system the wait could also be a call that payments made to something else. The trace shows where to look next, so the next step is to compare with a normal trace and check what that step was doing.

Why did recording one checkout in ten still cost about 20% more CPU at 400 a second, instead of about 2%?

Because the library's fixed work for each request runs whether or not the request is recorded: creating the span, passing the trace's identifier along and running the sampler. That work alone cost 10%. Sampling removes only the recording and sending. The lab's run recorded one in ten and still lost another 10 points, which it could not fully explain, so do not count on sampling to cut processor time.

At 800 checkouts a second, the pooled p50 with everything recorded is 17.4 ms, but the median repetition is about 16.9 ms. Which number should you quote?

Say which one you mean. The pooled p50 is the lab's summary over all five repetitions. The median repetition ignores a single outlier, and it is the fairer comparison for the tracing-off and one-in-ten modes, where a stalled repetition pulled the pooled p50 far up. For the full-recording mode no repetition stalled, so the two numbers are close.

Next up: Cost per request, the next episode: SLOs written as Prometheus rules and tested like code, and what a million requests cost.

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.