Under growing load, latency rises, throughput stalls and k6 drops planned requests. There are no errors. The bottleneck is not the database, it is the four connections to it.
Symptom
== 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
Everything ran fine at 500 to 2000 requests per second. At 4000/s the service manages 2635/s, p95 is a quarter of a second, and all 500 k6 virtual users are busy.
Diagnosis
In the middle of the run, look at the service's CPU and the open database connections.
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"
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
Causes
exactly four connectionspgx default pool size
Without a setting, the pgx connection pool opens at most as many connections as the machine has cores, at least four. Every request holds one of them for a round trip to the database.
λ = L / WLittle's Law predicts the limit
Four connections divided by about 1.5 ms per round trip gives roughly 2700 requests per second. The measurement was 2635/s – the formula finds the bottleneck without you searching for it.
database load 0.38The database is not the problem
uptime on the database server shows hardly any load. More CPU for PostgreSQL would not have helped.
Fix
Don't: Set the pool size to 500. Every PostgreSQL connection is a separate process with its own memory – two instances with 500 each would have overwhelmed the database.
System design hands-on, Day 2: measure instead of guessing – a load staircase with k6, finding the bottleneck, Little's Law.
Go to day 2 →
FAQ
What does dropped_iterations mean in k6?
k6 could not start planned iterations because all virtual users are still waiting for responses. It is a sign of saturation, not of errors.
Why are there no errors?
An overloaded system is often not broken, only slow. The requests queue up waiting for a connection.