At 4000 requests per second the service in the cluster returns errors that did not exist without Kubernetes. The logs show timeouts – but the database is not slow, the requests are waiting for a free connection.
Symptom
grep -E 'http_req_failed\.|http_reqs\.' k6-4000.txt | sed 's/ */ /g'
kubectl -n kurz logs -l app=kurz --since=1m --tail=-1 | cut -c21- | sort | uniq -c | sort -rn | head -2
http_req_failed................: 2.21% 1758 out of 79242
http_reqs......................: 79242 2617.341071/s
1701 weiterleiten: context deadline exceeded
53 weiterleiten: timeout: context deadline exceeded
Almost all errors are the same message (weiterleiten = "forwarding"). Throughput stalls at a little over 2600 requests per second.
Diagnosis
Count how many connections each pod holds to the database.
kubectl -n kurz exec db-1 -c postgres -- psql -d kurz -Atc \ "select client_addr, count(*) from pg_stat_activity where usename = 'kurz' group by 1" 10.42.0.30|4 10.42.4.14|4
Causes
four per podpgx default pool size
Without a setting, pgx opens at most as many connections as the machine has cores, at least four. Two pods, so eight connections in total.
KURZ_TIMEOUT 2sThe time limit turns waiting into an error
If a request waits more than two seconds for a free connection, the service gives up. Without the limit it would only have become slow.
W = L / λ ≈ 3.1 msLonger path in the cluster
A round trip to the database takes twice as long in the cluster as without Kubernetes: pod, VXLAN, another node, kube-proxy service rules.
Fix
== pool 8 je Pod http_req_duration..............: avg=90.47ms min=776.23µs med=80.51ms max=512.85ms p(90)=170.13ms p(95)=202.56ms http_req_failed................: 0.00% 0 out of 63243 http_reqs......................: 63243 3145.338356/s
Don't: Simply raise the time limit. The errors disappear, but the requests keep waiting in the queue – the bottleneck stays.
System design on Kubernetes, Day 2: measuring in the cluster – load staircase, connection pool, Little's Law, requests and limits.
Go to day 2 →
FAQ
Why does kubectl logs with --since not count all lines?
Without --tail=-1, kubectl logs -l only shows the last ten lines per pod, even with --since. Always set --tail=-1 for counting.
Do 16 connections help even more?
Only a little – in the course 9 % more, and p95 dropped to 91 ms. The bottleneck had moved on.