DEV Community

Cover image for Postgres was at 30% CPU. My app was still DEAD 💀
Nikhil Thadani
Nikhil Thadani

Posted on AI-assisted

Postgres was at 30% CPU. My app was still DEAD 💀

Your database isn't always the villain. Sometimes your app is just standing in line.

The math nobody does

20 pool connections × 100ms per query = ~200 ops/sec. Ask for 2,000 and the rest queue, then time out. Postgres looks bored because you never gave it work.

Measure the right thing

const t0 = performance.now();
const client = await pool.connect();      // waiting for a connection
const t1 = performance.now();
await client.query(sql);                  // actually running SQL
const t2 = performance.now();
client.release();

console.log({ poolWaitMs: t1 - t0, queryMs: t2 - t1 });
Enter fullscreen mode Exit fullscreen mode
  • poolWaitMs high, queryMs low → pool or app problem
  • poolWaitMs low, queryMs high → real database problem

Also check

  • Event loop: one CPU-heavy sync loop freezes everything
  • File descriptors: EMFILE shows up while the DB is healthy
  • Retries: 8K → 11K → 15K → 20K RPS. Use backoff + jitter + a cap

Where is the request waiting? Start there.

Top comments (0)