Opening Scenario
Your API response times are fine at 10 req/s. You load test at 500 req/s, and latency spikes to 4 seconds. Your database CPU is at 20%. The bottleneck isn't compute — the profiler shows your app spending most of its time in "connect to database." Each request is opening a new database connection, using it, and closing it. At 500 req/s, that's 500 connection handshakes per second.
Question Progression
1. Why is establishing a connection expensive?
Question: A TCP connection to your database is "just" a network round trip. Why would that be a bottleneck? Isn't the database query itself the slow part?
Reasoning: A database connection isn't just a TCP handshake. Postgres, MySQL, and most databases run a full authentication exchange on each new connection — TLS negotiation, password challenge-response, session initialization, and allocation of server-side resources (memory buffers, process or thread creation). For Postgres…
In the full tutorial
- Closing: When to Reach for This