· Engineering notes

Connection pool exhaustion under load: a k6 check you can run tonight

Our sample report lists a High finding: connection pool max 20 exhausted near 600 VUs and p95 past SLO. Reproduce the pattern on staging with a short k6 script and pool metrics - then decide raise pool, add a pooler, or cut concurrency.

What the sample finding says

Severity High · Category Load · Finding: connection pool (max 20) exhausted at about 600 VUs; p95 exceeds SLO. Recommendation on the sample: raise pool to 50, add pgbouncer, re-test at 800 VUs. Those numbers are from the anonymized sample app, not a client system. Copy the method, not the integers, unless your metrics match.

What you need before the script

  • Staging (or waived prod) with the same pool max as you care about
  • One authenticated read or write that holds a DB connection for a realistic time
  • A way to watch pool wait / saturation (app metric, PgBouncer stats, or DB pg_stat_activity)
  • An SLO for that path (see SLO before load)

Artifact: minimal k6 script

import http from 'k6/http';
import { check, sleep } from 'k6';

// EXAMPLE only - point at staging. Tune VUs to your pool size.
export const options = {
  scenarios: {
    pool_pressure: {
      executor: 'ramping-vus',
      startVUs: 0,
      stages: [
        { duration: '2m', target: 100 },
        { duration: '3m', target: 300 },
        { duration: '3m', target: 600 },
        { duration: '2m', target: 0 },
      ],
    },
  },
  thresholds: {
    http_req_failed: ['rate<0.01'],
    http_req_duration: ['p(95)<400'], // match YOUR SLO ms
  },
};

const BASE = __ENV.BASE_URL; // e.g. https://staging.example.com
const TOKEN = __ENV.TOKEN;

export default function () {
  const res = http.get(`${BASE}/v1/accounts/me`, {
    headers: { Authorization: `Bearer ${TOKEN}` },
  });
  check(res, { '2xx': (r) => r.status >= 200 && r.status < 300 });
  // Hold the VU briefly so connections stay checked out under concurrency
  sleep(0.5);
}

Run: k6 run -e BASE_URL=... -e TOKEN=... pool.js. Prefer arrival-rate when you model real traffic; VU ramp is enough to expose pool wait. Background: realistic k6 models and scoping hub.

How to read the result

SignalLikely causeNext action
p95 climbs while CPU is idlePool wait / lock, not computeWatch pool wait metric; raise pool or add pooler
Errors with "too many connections" / timeout acquiringHard pool ceilingCap app concurrency; raise ceiling with evidence
DB CPU peggedQuery cost, not pool size aloneFix queries before growing the pool
Green until N VUs, cliff afterSaturation pointRecord N; that is your capacity statement for this path

More on percentiles and saturation: reading performance results. Lab helper for rough sizing: capacity calculator.

When this check lies

If staging has a huge pool and tiny data, you will not see the cliff. If every request is mocked past the DB, the script only load-tests the edge. Match pool max and query shape to production, or the finding is theatre.

Load work that ends with a capacity statement and scripts in your repo: load & stress testing.

Want the three pool or queue risks on your stack named against this method? Book a free 20-min test audit.