Connection Pooling: Why Connection-per-Request Dies
Understand the idea
Why opening a new database connection on every HTTP request will crash your server.
The idea
Opening a new database connection from scratch is surprisingly slow and resource-heavy. Every time your application opens a connection to a database like PostgreSQL or MySQL, it has to complete network handshakes, verify passwords, set up encryption, and the database server has to allocate dedicated memory buffers (often several megabytes per connection).
If an app gets 1,000 visitors per second and opens a brand-new connection for every single visitor, the database spends all its processing power setting up connections instead of actually running queries, and it will quickly crash with 'too many connections' errors.
A fixes this by keeping a fixed group of warm, already-open connections ready in memory. When your app needs to run a query, it borrows an idle connection from the pool, runs the query in a fraction of a millisecond, and immediately hands the connection back so another request can use it.
What changes
| Dimension | New Connection per Request | Connection Pool (e.g. pg-pool, HikariCP) |
|---|---|---|
| Connection Overhead | 50ms - 100ms handshake and authentication per request | Sub-millisecond (connection already open and warm) |
| Database Server Memory | Surges linearly with request traffic; causes OOM | Fixed, predictable memory allocation |
| Concurrency Control | Can crash database when web traffic surges | Smoothly queues excess requests during traffic spikes |
| Process Churn | Constant process spawning and teardown on database | Zero process churn: stable worker threads |
Connection pool · capacity 2
Incoming requests
Waiting queue · 0
Connection 1 · FREE
Ready to acquire →
Connection 2 · FREE
Ready to acquire →
Completed · connections released
Four requests arrive. The pool has exactly two reusable database connections.
01 / 06Connection pool · capacity 2
The command
Simulating connection pool lifecycle
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Run the example to see a connection pool lease connections up to its limit, reject excess requests, and immediately re-lease when a connection is returned.
Guided challenge
Prevent connection starvation
Update the function to return false in all cases, confirming you should NEVER hold a DB connection during external API calls.
Prevent connection starvation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
false false
Hints 0/1
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.