Reusing connections and speeding up the database are different tasks
What a pool does
A database connection must be established, configured and later closed. A pool keeps several ready connections and lends them to requests as they become free. It removes repeated connection setup costs, but neither speeds up an individual SQL query nor increases database CPU.
Time and concurrency
A 20 ms query occupies a connection for about one fiftieth of a second. A continuously busy slot can handle around 50 such queries/s. Processing 500 queries/s requires at least 10 concurrent slots if there are no other limits. Without a pool, connection setup adds 200 ms per query.
Why an oversized pool hurts
Too much concurrent work increases contention for CPU, memory and locks. In this model, contention rises above 16 connections, and CPU limits throughput regardless. That threshold is illustrative: production pool sizes come from measurements, not copying the lesson's number.
How to run the experiment
Add a pool and vary connection count while keeping query time fixed. Compare small, sufficient and excessive sizes. Then change query duration: required concurrency changes too. Check throughput and latency alongside cost.
Task
500 requests/s. New connection setup takes 200 ms; a query takes 20 ms. Add a pool and choose a connection count without overloading the database through contention.
A query takes queryMs; without a pool, connection setup adds 200 ms. Database CPU caps throughput at 700 requests/s. Above 16 connections, teaching-model contention rises by 12% per extra connection. The contention formula is a stand assumption, not production advice.
- Add a connection pool.
- With a query time of 20 ms, choose the connection count for 500 requests/s.
- Compare a sufficient size with 40 connections and run the check.