Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

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

Connection-per-Request vs Connection Pooling
DimensionNew Connection per RequestConnection Pool (e.g. pg-pool, HikariCP)
Connection Overhead50ms - 100ms handshake and authentication per requestSub-millisecond (connection already open and warm)
Database Server MemorySurges linearly with request traffic; causes OOMFixed, predictable memory allocation
Concurrency ControlCan crash database when web traffic surgesSmoothly queues excess requests during traffic spikes
Process ChurnConstant process spawning and teardown on databaseZero process churn: stable worker threads

Connection pool · capacity 2

Incoming requests

Request A
Request B
Request C
Request D

Waiting queue · 0

No requests waiting

Connection 1 · FREE

Ready to acquire →

Connection 2 · FREE

Ready to acquire →

↓ Database · 0 / 2 connections in use

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.

    01Why is establishing a new database connection for every HTTP request harmful?
    02What does a connection pool do when all connections are currently leased?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson