Skip to content
System design · Chapter 4 of 21 Day 2: Measuring properly

Day 2: Measuring properly

Goal: Raise the load step by step until the system can no longer keep up, predict the bottleneck with Little’s Law and prove it, and understand the difference between the open and the closed load model · Duration: approx. 75 min · Prerequisite: Day 1

Briefing

  • An average lies. What matters are the percentiles: p50 (half are faster), p95, p99. Commitments to users are made on p95 or p99, because the slow requests are exactly the ones they notice.
  • Every system has a saturation point: beyond a certain load, throughput no longer rises, only waiting time does. Finding this point is the most important measurement of a system.
  • Little’s Law connects three quantities, for any stable system, without assumptions about distributions:
  L  =  λ  ×  W
  in progress at once  =  throughput (per s)  ×  time in system (s)
  • If you know two, you know the third. This lets you calculate a bottleneck before you go looking for it.

Drill 1: The staircase

The same script as yesterday, with a growing rate:

# on s1
mkdir -p ~/sd/tag02 && cd ~/sd/tag02
cp ~/sd/tag01/codes.txt ~/sd/tag01/mix.js .
for r in 500 1000 2000 4000; do
  echo "== $r/s"
  RATE=$r DAUER=20s k6 run --quiet mix.js \
    | grep -E 'http_req_duration\.|http_req_failed\.|http_reqs\.|dropped_iterations|  vus\.' \
    | sed 's/  */ /g'
done

Expected:

== 500/s
 http_req_duration..............: avg=3.94ms min=795.95µs med=1.6ms max=110.55ms p(90)=3.27ms p(95)=4.77ms
 http_req_failed................: 0.00% 0 out of 9999
 http_reqs......................: 9999 499.883025/s
 dropped_iterations.............: 2 0.099987/s
 vus............................: 1 min=0 max=29
== 1000/s
 http_req_duration..............: avg=2.75ms min=714.83µs med=1.62ms max=139.47ms p(90)=3.7ms p(95)=5.31ms
 http_req_failed................: 0.00% 0 out of 19927
 http_reqs......................: 19927 996.240531/s
 dropped_iterations.............: 74 3.699593/s
 vus............................: 3 min=2 max=9
== 2000/s
 http_req_duration..............: avg=6.53ms min=697.01µs med=3.24ms max=74.02ms p(90)=15.96ms p(95)=22.45ms
 http_req_failed................: 0.00% 0 out of 39816
 http_reqs......................: 39816 1987.94208/s
 dropped_iterations.............: 186 9.286649/s
 vus............................: 11 min=3 max=46
== 4000/s
 http_req_duration..............: avg=167.52ms min=1.05ms med=172.96ms max=386.2ms p(90)=225.48ms p(95)=238.97ms
 http_req_failed................: 0.00% 0 out of 53234
 http_reqs......................: 53234 2635.308122/s
 dropped_iterations.............: 26768 1325.1292/s
 vus............................: 500 min=173 max=500
Planned rate Achieved p95 Dropped
500/s 500/s 4.8 ms 2
1000/s 996/s 5.3 ms 74
2000/s 1988/s 22 ms 186
4000/s 2635/s 239 ms 26,768

Up to 1000/s, almost nothing changes. At 2000/s, p95 quadruples and the median doubles: the requests start waiting. At 4000/s, that is it. Throughput 2635/s no matter how much more you send, p95 a quarter of a second, and k6 drops every third planned request because all 500 virtual users are busy (dropped_iterations). Not a single error, by the way: an overloaded system is often not broken, just slow.

Save your progress? Sign up for free

Drill 2: Where is the bottleneck?

4000/s once more, and look in the middle of the run:

# on s1
(RATE=4000 DAUER=25s k6 run --quiet mix.js > k6-4000.txt 2>&1 &)
sleep 12
top -bn1 -o %CPU | sed -n 7,9p | cut -c1-80
PGPASSWORD=lab-kurz-123 psql -h s3 -U kurz -d kurz -tAc "select state, wait_event_type, count(*)
  from pg_stat_activity where usename = 'kurz' group by 1, 2"
# on s3
uptime

Expected:

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   9040 ubuntu    20   0 1443152 190612  32520 S 135.7   1.6   0:26.79 k6
   8571 kurz      20   0 1337196  38752   9856 S 107.1   0.3   1:35.18 kurz
active||1
idle|Client|4
 19:08:20 up 27 min,  1 user,  load average: 0.38, 0.15, 0.13

s1 has four cores; k6 and kurz together use just under two and a half. The database on s3 is bored (load 0.38). And in PostgreSQL, exactly four connections from kurz are open; the active line is your own query. Four? Without a setting, the pgx connection pool opens at most as many connections as the machine has cores, but at least four.

Save your progress? Sign up for free

Drill 3: Little’s Law does the math

Each request holds one of the four connections for one round trip to the database: network under 1 ms (Day 0) plus query 0.1 ms (Day 1) plus driver and scheduling. Assume roughly 1.5 ms. Then:

  λ  =  L / W  =  4 connections / 0.0015 s  ≈  2700 requests/s

Measured: 2635/s. The formula predicted the bottleneck without you searching for it. Proof by cross-check: more connections. pgx reads the pool size from the connection URL. A drop-in file overrides the environment variable of the unit:

# on s1
sudo mkdir -p /etc/systemd/system/kurz.service.d
for n in 8 16; do
  printf '[Service]\nEnvironment=KURZ_DB=postgres://kurz:lab-kurz-123@s3:5432/kurz?pool_max_conns=%s\n' $n \
    | sudo tee /etc/systemd/system/kurz.service.d/pool.conf >/dev/null
  sudo systemctl daemon-reload && sudo systemctl restart kurz && sleep 1
  echo "== pool $n"
  RATE=4000 DAUER=20s k6 run --quiet mix.js \
    | grep -E 'http_req_duration\.|http_reqs\.|dropped_iterations' | sed 's/  */ /g'
done

Expected:

== pool 8
 http_req_duration..............: avg=75.74ms min=657.65µs med=68.45ms max=446.67ms p(90)=139.63ms p(95)=177.46ms
 http_reqs......................: 68509 3404.081457/s
 dropped_iterations.............: 11491 570.965859/s
== pool 16
 http_req_duration..............: avg=42.16ms min=690.69µs med=25.92ms max=582.49ms p(90)=90.44ms p(95)=128.17ms
 http_reqs......................: 66679 3328.077828/s
 dropped_iterations.............: 13313 664.477574/s

With eight connections, throughput rises by 30%. With sixteen, it no longer rises. The bottleneck has moved on.

Save your progress? Sign up for free

Drill 4: The next bottleneck

# on s1
(RATE=4000 DAUER=25s k6 run --quiet mix.js > k6-p16.txt 2>&1 &)
sleep 12
top -bn1 | sed -n 3p
top -bn1 -o %CPU | sed -n 8,9p | cut -c1-80

Expected:

%Cpu(s): 75.4 us, 14.0 sy,  0.0 ni,  5.3 id,  0.0 wa,  0.0 hi,  3.5 si,  1.8 st
   9379 ubuntu    20   0 1375128 120804  32332 R 253.8   1.0   0:30.62 k6
   9295 kurz      20   0 1271332  29828   9984 R  92.3   0.2   0:43.33 kurz

s1 is 95% busy, and most of it is eaten by k6 itself with two and a half cores. The load generator is now the bottleneck. This is the most common trap in measuring: if you run the load generator and the system on the same machine, from some point on you are measuring the load generator. Remember the limit: On s1, measurements are meaningful up to about 3000/s.

For the next days, the pool goes back to the default. The right pool size is a topic of its own (Day 7):

# on s1
sudo rm /etc/systemd/system/kurz.service.d/pool.conf
sudo systemctl daemon-reload && sudo systemctl restart kurz

Save your progress? Sign up for free

Drill 5: Open or closed

mix.js sends a fixed rate, no matter how fast the system responds. That is an open model; this is how many independent users on the internet behave. Many load tests instead work closed: a fixed number of users, each sending the next request only after the response.

# on s1
cat > geschlossen.js <<'EOF'
import http from "k6/http";
import { SharedArray } from "k6/data";
const codes = new SharedArray("codes", () => open("./codes.txt").split("\n").filter(Boolean));
export const options = { vus: Number(__ENV.VUS || 50), duration: "20s" };
export default function () {
  const code = codes[Math.floor(Math.random() * codes.length)];
  http.get(`http://localhost:8080/${code}`, { redirects: 0 });
}
EOF
for v in 10 50 200; do
  echo "== $v VUs"
  VUS=$v k6 run --quiet geschlossen.js | grep -E 'http_req_duration\.|http_reqs\.' | sed 's/  */ /g'
done

Expected:

== 10 VUs
 http_req_duration..............: avg=3.03ms min=666.48µs med=2.67ms max=89.04ms p(90)=4.9ms p(95)=5.98ms
 http_reqs......................: 59723 2985.530858/s
== 50 VUs
 http_req_duration..............: avg=17.43ms min=1.39ms med=15.75ms max=96.53ms p(90)=24.81ms p(95)=30.38ms
 http_reqs......................: 56030 2799.405347/s
== 200 VUs
 http_req_duration..............: avg=67.02ms min=6.36ms med=62.89ms max=274.53ms p(90)=89.86ms p(95)=102.86ms
 http_reqs......................: 59286 2954.191626/s

Throughput stays at just under 3000/s, no matter how many users. Only the response time grows. Little’s Law, this time solved for W:

Users (L) Throughput (λ) W = L / λ Measured avg
10 2986/s 3.3 ms 3.0 ms
50 2799/s 17.9 ms 17.4 ms
200 2954/s 67.7 ms 67.0 ms

The trap of the closed model: a slow system throttles the load test itself, because the users wait instead of sending more. A test with 10 users looks excellent (p95 6 ms), but says nothing about what happens at 4000 requests per second. For capacity questions, this course therefore uses the open model.

Save your progress? Sign up for free

Exercise: Speed drills

  1. Show p99 instead of p90 in the k6 summary. Target time: 1 min
  2. Use Little’s Law to calculate how many connections 5000 requests/s need at 2 ms database time. Target time: 30 s
  3. Show how many connections kurz currently holds to PostgreSQL. Target time: 1 min

Solution

# on s1
k6 run --quiet --summary-trend-stats 'med,p(95),p(99),max' mix.js | grep 'http_req_duration\.'

5000/s × 0.002 s = 10 connections (without reserve).

# on s1
PGPASSWORD=lab-kurz-123 psql -h s3 -U kurz -d kurz -tAc \
  "select count(*) from pg_stat_activity where usename = 'kurz' and pid <> pg_backend_pid()"

Save your progress? Sign up for free

Checkpoint

# on s1
ls /etc/systemd/system/kurz.service.d/ 2>/dev/null | wc -l

Expected: 0 (pool size no longer overridden)

# on s1
cd ~/sd/tag02 && RATE=1000 DAUER=10s k6 run --quiet mix.js > /dev/null; echo "Exit: $?"

Expected: Exit: 0

Challenge

A colleague suggests simply setting the pool size to 500, “then the problem is solved forever”. What speaks against it? Think of PostgreSQL and of Day 3, where a second instance is added.

Hint: Ask PostgreSQL for its limit.

Solution

# on s1
PGPASSWORD=lab-kurz-123 psql -h s3 -U kurz -d kurz -tAc "show max_connections"

Expected:

100

Out of the box, PostgreSQL allows 100 connections in total, for all instances and tools together. Each connection is a separate process with its own memory. Two instances with 500 each would have brought the database to its knees long before it got any faster. More connections help only as long as the database has headroom; after that, they only lengthen the queue inside PostgreSQL. How to choose the size properly and what a pool proxy like PgBouncer brings is shown on Day 7.

Save your progress? Sign up for free

Clean-up

# on s1
rm -f ~/sd/tag02/k6-*.txt

Interview note

  • What is Little’s Law? L = λ × W: concurrently in progress = throughput × time in system. With it, you calculate pool sizes, thread counts or queues before you measure.
  • Why p99 and not the average? Because users feel the slow requests, and because a page with 20 calls almost certainly has one of them in the slowest percent.
  • How do you recognize saturation? Throughput stagnates, latency rises steeply, queues grow. Errors often come only later.

Enterprise note

  • In production, load tests run from dedicated machines or as a distributed test from several regions, never from the system itself. The load generator is monitored too.
  • Many teams measure coordinated omission wrong: a closed test that waits when the system hangs suppresses exactly the worst response times. Tools with a fixed arrival rate (like k6 with constant-arrival-rate) avoid this.

Contents

More