I'm building a Node.js backend and trying to understand the role of database connection pooling more deeply.
Since Node.js is single-threaded and uses an event loop with non-blocking I/O via libuv, I figured that await would allow the app to handle thousands of concurrent requests without blocking. I understand that when await is hit (e.g., await db.query(...)), the query is offloaded to the underlying TCP/network layer, and Node can move on to handle other events or requests.
If the DB query is offloaded and non-blocking, and Node resumes the function only when the result comes back, why do I still need a pool of DB connections?
Wouldn’t a single connection be enough since the app is async and the queries are not blocking the event loop?
Here’s what I’m using as an example:
app.get('/price', async (req, res) => {
const result = await db.query('SELECT price FROM crypto WHERE id = $1', [req.params.id]);
});
I'm using PostgreSQL with the pg library. I'm debating whether to use a single Client or a Pool for handling queries.