Skip to content
System design on Kubernetes · Chapter 16 of 20 Day 14: Capacity and the design document

Day 14: Capacity and the design document

Goal: Measure what the whole stack from 13 days carries, where the CPU goes and how much of it remains when a zone fails – and write everything you have measured into a design document that another team can read and review · Duration: approx. 120 min · Prerequisite: Day 13

Briefing

  • On Day 2, kurz alone managed about 1200 requests per CPU second. Since then, more has been added: a gateway with rate limit and circuit breaker, a mesh, a cache cluster, three database instances with a pooler, NATS with a worker, autoscalers. Each decision was measured on its own. Today, the sum counts.
  • Capacity doesn’t mean “what still just about works”, but: which load does the system hold at its target (SLO), even when a zone is missing?
  • The design document is the result of the course: requirements, architecture, numbers, failure behavior, decisions with their reasons. Every number in it comes from a drill.

Drill 1: The staircase

Load in steps through the gateway, the mix from Day 2 (90 % redirects, 10 % new links), 40 seconds per step. After 28 seconds of a step, the CPU of the nodes is recorded.

# on s1
mkdir -p ~/sdk/tag14 && cd ~/sdk/tag14
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
cp ../tag13/mix.js ../tag13/codes.txt .
for r in 500 1000 1500 2000 2500; do
  k6 run -q -e ZIEL=http://172.16.0.126 -e RATE=$r -e DAUER=40s mix.js > k6-$r.txt 2>&1 &
  sleep 28; top=$(kubectl top nodes --no-headers | awk '{printf "%s=%s ", $1, $3}'); wait
  echo "$r/s: erreicht=$(grep -E 'http_reqs\.' k6-$r.txt | awk '{print $3}') $(grep -E 'http_req_duration\.' k6-$r.txt | grep -oE 'p\(95\)=[0-9.]+[a-zµ]+') fehler=$(grep -E 'http_req_failed\.' k6-$r.txt | grep -oE '[0-9.]+%' | head -1) | $top"
  sleep 20
done

Expected:

500/s: erreicht=499.522559/s p(95)=52.31ms fehler=0.00% | s1=51% s2=67% s3=51%
1000/s: erreicht=980.89916/s p(95)=170.52ms fehler=0.00% | s1=80% s2=89% s3=90%
1500/s: erreicht=1444.167932/s p(95)=287.38ms fehler=4.49% | s1=85% s2=92% s3=95%
2000/s: erreicht=1797.486758/s p(95)=412.05ms fehler=24.64% | s1=90% s2=87% s3=97%
2500/s: erreicht=1959.401577/s p(95)=416.86ms fehler=31.44% | s1=86% s2=96% s3=95%

(erreicht = achieved, fehler = errors.) The knee lies between 500 and 1000: at 1000 per second there are no errors yet, but p95 has tripled and the nodes are at 80–90 %. From 1500 on, errors come in (circuit breaker, Day 11); from 2000 on, the system can’t even handle the requests k6 wants to send any more.

Save your progress? Sign up for free

Drill 2: Where does the CPU go?

# on s1
# 70 s at 1000/s, measure all containers after 50 s
k6 run -q -e ZIEL=http://172.16.0.126 -e RATE=1000 -e DAUER=70s mix.js > k6-cpu.txt 2>&1 &
sleep 50; kubectl top pods -A --containers --no-headers > top.txt; wait
# Sum up by component (millicores)
awk '{c=$4; sub(/m/,"",c); ns=$1; n=$2; k=$3; g=ns
  if (k=="linkerd-proxy") g="linkerd-proxy (alle)"
  else if (ns=="kurz" && n ~ /^kurz-/) g="kurz (App)"
  else if (ns=="kurz" && n ~ /^db-[0-9]/) g="PostgreSQL"
  else if (ns=="kurz" && n ~ /pooler/) g="PgBouncer"
  else if (ns=="kurz" && n ~ /valkey/) g="Valkey"
  else if (ns=="kurz" && n ~ /zaehler/) g="zaehler"
  else if (ns=="envoy-gateway-system" && n ~ /^envoy-kurz/) g="Envoy (Gateway)"
  s[g]+=c} END {for (g in s) print s[g], g}' top.txt | sort -rn | head -9
grep -E 'http_reqs\.' k6-cpu.txt | sed 's/  */ /g'

Expected:

1051 Envoy (Gateway)
1031 linkerd-proxy (alle)
879 kurz (App)
648 nats
384 Valkey
329 PostgreSQL
83 PgBouncer
49 zaehler
24 keda
 http_reqs...........: 69576 992.531207/s

About 4.5 cores for 1000 requests per second – 4.5 ms of CPU per request. kurz itself needs 0.9 ms of that; the rest is what you built around kurz. Gateway and mesh together cost more than twice as much as the application. On top come k6 and the k3s server on s1, which are missing from this list.

Component Share introduced on
Envoy (gateway, 2 replicas) 23 % Days 3, 4, 11
Linkerd proxies 23 % Day 12
kurz 19 % Day 1
NATS 14 % Day 8
Valkey 8 % Day 5
PostgreSQL + PgBouncer 9 % Days 6, 7

Save your progress? Sign up for free

Drill 3: What the scheduler believes

# on s1
# Reserved CPU and reserved memory per node
for n in s1 s2 s3; do kubectl describe node $n | awk -v n=$n '/Allocated resources/{f=1} f&&/cpu /{c=$2" "$3} f&&/memory /{print n, "CPU-Requests", c, "Speicher-Requests", $2" "$3; exit}'; done
kubectl get nodes -o custom-columns=NAME:.metadata.name,CPU:.status.allocatable.cpu

Expected:

s1 CPU-Requests 1410m (35%) Speicher-Requests 1644Mi (13%)
s2 CPU-Requests 960m (48%) Speicher-Requests 1012Mi (25%)
s3 CPU-Requests 460m (23%) Speicher-Requests 596Mi (15%)
NAME   CPU
s1     4
s2     2
s3     2

The scheduler sees 2.8 of 8 cores reserved and considers the cluster one third full. In reality, at 1000 per second it needs 4.5 cores plus k6 – the nodes run at 80–90 %. Without CPU limits (Day 2) that is allowed, but every new application that is scheduled by free requests lands on nodes that are already full. Requests should reflect the measured consumption at the target load, not the consumption at idle.

Save your progress? Sign up for free

Drill 4: What remains when a zone is missing

Capacity with reserve means: the target load must also run on two zones. Since Day 13, the primary is in zone-a, so a failure of zone-b only hits compute.

# on s2: cut off zone b, release after 200 s
for ip in 172.16.0.126 172.16.0.83; do
  sudo iptables -I INPUT -s $ip -j DROP -m comment --comment tag14
  sudo iptables -I OUTPUT -d $ip -j DROP -m comment --comment tag14
done
sudo systemd-run --quiet --on-active=200 --unit=tag14-frei \
  sh -c 'iptables -S | grep tag14 | sed s/^-A/-D/ | while read r; do iptables $r; done'
# on s1: wait until s2 is no longer ready, 30 s of light load to settle, then two steps
until [ "$(kubectl get node s2 -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}')" != True ]; do sleep 5; done
for i in $(seq 1 6); do k6 run -q -e ZIEL=http://172.16.0.126 -e RATE=100 -e DAUER=5s mix.js >/dev/null 2>&1; done
for r in 400 700; do
  k6 run -q -e ZIEL=http://172.16.0.126 -e RATE=$r -e DAUER=30s mix.js > n1-$r.txt 2>&1
  echo "N-1 $r/s: $(grep -E 'http_reqs\.' n1-$r.txt | awk '{print $3}') $(grep -E 'http_req_duration\.' n1-$r.txt | grep -oE 'p\(95\)=[0-9.]+[a-zµ]+') fehler=$(grep -E 'http_req_failed\.' n1-$r.txt | grep -oE '[0-9.]+%' | head -1) | $(kubectl top nodes --no-headers | awk '{printf "%s=%s ", $1, $3}')"
done

Expected:

N-1 400/s: 399.935208/s p(95)=47.33ms fehler=0.00% | s1=65% s3=67% s2=<unknown>
N-1 700/s: 681.784647/s p(95)=241.82ms fehler=3.62% | s1=76% s3=84% s2=<unknown>

400 per second run on two zones without compromise, 700 no longer do. That gives the number for the design document: capacity with zone reserve is about 400–500 requests per second. Anything above that is performance without reserve.

Save your progress? Sign up for free

Drill 5: Storage and growth

# on s1, after s2 is ready again
kubectl get pvc -A -o custom-columns=NS:.metadata.namespace,NAME:.metadata.name,GROESSE:.spec.resources.requests.storage --no-headers
P=$(kubectl -n kurz get cluster db -o jsonpath='{.status.currentPrimary}')
kubectl -n kurz exec $P -c postgres -- psql -d kurz -tAc "SELECT pg_size_pretty(pg_database_size('kurz')), (SELECT count(*) FROM links)"
N="nats -s nats://kurz:lab-nats-123@$(kubectl -n nats get svc nats -o jsonpath='{.spec.clusterIP}'):4222"
$N stream info KLICKS --json | jq -c '{messages: .state.messages, bytes: .state.bytes}'

Expected:

kurz   db-1             5Gi
kurz   db-2             5Gi
kurz   db-3             5Gi
nats   nats-js-nats-0   1Gi
nats   nats-js-nats-1   1Gi
nats   nats-js-nats-2   1Gi
507 MB|2777656
{"messages":4987652,"bytes":228047193}

The database: 507 MB for 2.8 million links, about 180 bytes per link including indexes – 5 GiB is enough for about 25 million. The stream: 228 MB for 5 million clicks, and the volume has 1 GiB. The warning from Day 8 (exercise 3) is no longer a calculation: after roughly another 17 million clicks, the volume is full, and JetStream rejects new messages. At 500 clicks per second, that is just under ten hours.

Save your progress? Sign up for free

Drill 6: The design document

The document follows a fixed outline. Every number carries the day on which it was measured – so a reviewer can retrace it.

# on s1
cat > ~/sdk/DESIGN.md <<'EOF'
# Design: kurz auf Kubernetes

Stand: Tag 14 des Kurses. Alle Zahlen gemessen im Labor (k3s, 3 Knoten = 3 Zonen,
s1 4 vCPU, s2/s3 je 2 vCPU), Last mit k6 von s1 über das Gateway.

## 1. Ziel und Anforderungen
- URL-Kürzer: Links anlegen (POST), weiterleiten (GET /<code>, 302), Klicks zählen.
- Verhältnis 90 % Weiterleitungen, 10 % neue Links.
- SLO: p95 der Weiterleitungen < 100 ms, Fehlerquote < 0,1 %, auch bei Ausfall einer Zone.
- Klicks dürfen verzögert gezählt werden, nicht doppelt und möglichst nicht verloren.

## 2. Architektur
Gateway (Envoy Gateway, 2 Replikate in zone-b/c) → kurz (3 Pods, je Zone einer, Linkerd) →
Valkey-Cluster (3 Shards), PostgreSQL (CNPG, 3 Instanzen, Lesen über db-ro, Schreiben über
PgBouncer), NATS JetStream (3 Server, Stream KLICKS R3) → zaehler (KEDA, 0–10).

## 3. Kapazität
| Last | p95 | Fehler | Quelle |
|---|---|---|---|
| 500/s | 52 ms | 0 % | Tag 14, Drill 1 |
| 1000/s | 171 ms | 0 % | Tag 14, Drill 1 |
| 1500/s | 287 ms | 4,5 % | Tag 14, Drill 1 |
| 400/s, eine Zone fehlt | 47 ms | 0 % | Tag 14, Drill 4 |
| 700/s, eine Zone fehlt | 242 ms | 3,6 % | Tag 14, Drill 4 |
Kapazität im SLO mit Zonenreserve: ~400–500/s. CPU-Kosten: ~4,5 ms je Anfrage, davon 0,9 ms kurz
(Tag 14, Drill 2).

## 4. Ausfallverhalten
| Ausfall | Wirkung | Maßnahme | Quelle |
|---|---|---|---|
| Pod-Absturz (kill -9) | 287 → 16 Fehler | Retry bei connect-failure | Tag 4 |
| Rollout | 13/47/44 → 0/0/1 Fehler | preStop 5 s | Tag 4 |
| Primary-Switchover | Schreiben ~2 min verklemmt | connect_timeout=2 | Tag 7 |
| Failover | ~25 s, 6 % Fehler | Pooler mit login_retry 1 | Tag 7 |
| NATS-Leader | 48 Klicks still verloren | PublishAsync mit Ack | Tag 8 |
| Rate-Limit-Dienst weg | Limit wirkungslos (fail open) | Zähler failure_mode_allowed überwachen | Tag 11 |
| langsamer Pod | Gateway p95 2 s, Mesh 26 ms | Versuchs-Timeout 300 ms | Tag 12 |
| Zone b (mit Primary) | 42 % → 6 % im ersten Fenster | Readiness ohne Abhängigkeiten | Tag 13 |

## 5. Entscheidungen
| Entscheidung | statt | Begründung | Quelle |
|---|---|---|---|
| keine CPU-Limits für kurz | Limit 500m | 2000/s: 69 % Fehler mit Limit, 0 ohne | Tag 2 |
| Cache vor PostgreSQL | nur Datenbank | p95 131 → 85 ms, DB-Last −90 % | Tag 5 |
| Klicks über NATS | synchron | 453 → 994/s, p95 1,24 s → 54 ms | Tag 8 |
| Skalieren nach Rückstau | nach CPU | Worker bei 2 % CPU mit 60 000 offen | Tag 9/10 |
| globales Limit pro Nutzer | lokal pro Envoy | lokal = Limit × Instanzen | Tag 11 |
| Mesh für Pod-zu-Pod | ohne Mesh | mTLS, Latenz-Balancing; Preis ~400m CPU je Proxy | Tag 12 |

## 6. Risiken und offene Punkte
- Stream KLICKS: 228 MB von 1 GiB, voll nach ~17 Mio. weiteren Klicks → maxBytes setzen (Tag 8).
- Requests spiegeln den Verbrauch nicht (2,8 reserviert, 4,5 genutzt) → anpassen (Tag 14, Drill 3).
- zone-a trägt k3s-Server, CoreDNS (1 Pod), Linkerd-Control-Plane: Ausfall von zone-a nicht getestet.
- Gateway und Mesh kosten zusammen mehr CPU als die Anwendung – bei Wachstum zuerst dort messen.

## 7. Betrieb: was überwacht wird
p95 und Fehlerquote am Gateway · ratelimit failure_mode_allowed · KEDA ScalingActive ·
Rückstau des Consumers zaehler · Füllstand des Streams · Replikationsverzug (CNPG) ·
CPU der Knoten gegen die Kapazität aus Abschnitt 3.
EOF
wc -l ~/sdk/DESIGN.md; grep -c '^## ' ~/sdk/DESIGN.md

Expected:

59 /home/ubuntu/sdk/DESIGN.md
7

Seven sections: goal and requirements, architecture, capacity, failure behavior (“Ausfallverhalten”), decisions (“Entscheidungen”), risks and open points, operations. A good design document is short enough to get read, and precise enough that you can disagree with it. The column “Quelle” (source) is the difference between an opinion and a measurement.

Save your progress? Sign up for free

Exercise: Speed drills

  1. How many requests per second does the cluster carry at 0 % errors, and what is its p95 at that load? Take the number from your files, not from memory. Target time: 1 min
  2. What percentage of the stream’s capacity is used? Target time: 1 min
  3. How much CPU per request does the system consume, and how much of it is kurz? Target time: 2 min

Solution

# on s1
# 1: highest step without errors
for r in 500 1000 1500; do echo "$r $(grep -E 'http_req_failed\.' k6-$r.txt | grep -oE '[0-9.]+%' | head -1) $(grep -E 'http_req_duration\.' k6-$r.txt | grep -oE 'p\(95\)=[0-9.]+[a-zµ]+')"; done
# 2: fill level of the stream in percent of 1 GiB
$N stream info KLICKS --json | jq '.state.bytes / 1073741824 * 100 | floor'

Expected (number 2 grows with every run):

500 0.00% p(95)=52.31ms
1000 0.00% p(95)=170.52ms
1500 4.49% p(95)=287.38ms
21

On 3: sum from Drill 2 (~4.5 cores) divided by 992 requests per second ≈ 4.5 ms; kurz 879m / 992 ≈ 0.9 ms.

Save your progress? Sign up for free

Challenge

After a campaign, the product team expects 2000 redirects per second, and the SLO should hold even when a zone fails. How many nodes with 2 vCPU does the cluster need if everything else stays as it is? And what would you change first instead of buying nodes?

Solution

Calculation with the measured costs: 2000 × 4.5 ms = 9 cores at full utilization. The SLO held at 500 per second with ~2.3 cores and nodes around 50–65 % – that is, with roughly 40 % reserve: 9 / 0.6 ≈ 15 cores. With zone reserve, that has to fit on two of three zones: 15 / 2 × 3 ≈ 23 cores, so 12 nodes with 2 vCPU (4 per zone) – plus room for k6 or real load sources, which here run alongside on s1.

Before that, a look at Drill 2 pays off: gateway and mesh consume 46 % of the CPU. Three levers, each measurable with the tools from this course:

  • Limit the mesh to the paths that need it. The Linkerd proxy costs about as much per pod as kurz itself. Redirects served only from the cache need no mTLS path to Valkey if Valkey itself speaks TLS.
  • Caching at the edge. Redirects are stable for minutes; a Cache-Control on the 302 and a cache in front of the gateway take the load before it reaches Envoy.
  • Set requests to the measured consumption (Drill 3), so autoscalers and the scheduler plan with real numbers – otherwise you buy nodes that then don’t get used after all.

Save your progress? Sign up for free

Checkpoint

# on s1
# Design document present, seven sections, every capacity row with a source
test -f ~/sdk/DESIGN.md && echo vorhanden
grep -c '^## ' ~/sdk/DESIGN.md
grep -A7 '^## 3' ~/sdk/DESIGN.md | grep -c 'Tag 14'
# Cluster complete again after the failure test
kubectl get nodes --no-headers | awk '{print $1, $2}'
kubectl -n kurz get cluster db -o jsonpath='{.status.readyInstances}{"\n"}'

Expected:

vorhanden
7
5
s1 Ready
s2 Ready
s3 Ready
3

Interview note

  • Capacity is a number with a condition: “400–500/s within the SLO, even when a zone is missing” – not “2000/s when everything is running”. Measured with a load staircase and a real zone outage.
  • The platform costs more than the app. Here 4.5 ms of CPU per request, 0.9 ms of it the application. Whoever scales first measures the infrastructure around the service.
  • Requests are planning data. 2.8 cores reserved, 4.5 used – the scheduler plans with fiction.
  • A design document cites its measurements. Every decision with “instead of” and “because”, every number with a source.

Enterprise note

  • In architecture reviews, documents rarely fail on the architecture, but on missing numbers: capacity, failure behavior, costs. The outline from Drill 6 works in most organizations.
  • Capacity planning needs a fixed rhythm: a load staircase after every major change (new proxy, new version, new dependency), with the result going into the design document.
  • Storage limits (stream, database, volumes) belong in the operations calendar with a date – a full volume is the most predictable of all outages.

Contents

More