Skip to article content
FrontendXLesson 16 / 24

Connection pooling and lifecycle

Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.

advanced · about 50 minutes · 2 exercises

  • Explain
  • Debug

What you will be able to do

#
  • Explain why opening a connection per request is expensive.
  • Size a pool against database limits and worker count.
  • Keep every statement of a transaction on one connection.

Overview

#

A database connection is a stateful, expensive resource. A pool keeps a small set of open connections and hands them to requests, so you pay the handshake cost rarely.

Pool sizing

A pool that is too small queues requests; one that is too large overwhelms the database. A common starting point is a small pool per process, sized so that (pool size) times (number of processes) stays under the database connection limit.

snippet.js
const pool = new Pool({ max: 10, idleTimeoutMillis: 30000 });

Transactions need one connection

A transaction spans multiple statements, so all of them must run on the same connection. Borrow a client from the pool, run the transaction, then release it in a finally block.

snippet.js
const client = await pool.connect();
try {
  await client.query('BEGIN');
  await client.query('UPDATE accounts SET balance = balance - 100 WHERE id = $1', [1]);
  await client.query('UPDATE accounts SET balance = balance + 100 WHERE id = $1', [2]);
  await client.query('COMMIT');
} catch (err) {
  await client.query('ROLLBACK');
  throw err;
} finally {
  client.release();
}

Common pitfalls

  • Running transaction statements on the pool instead of one borrowed client, so they land on different connections.
  • Forgetting client.release(), which leaks connections until the pool is exhausted.
  • Setting a huge pool per serverless instance and exceeding the database limit under scale.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    This transaction occasionally updates the wrong connection. Explain why and fix it.

    starter.javascript
    await pool.query('BEGIN');
    await pool.query('UPDATE accounts SET balance = balance - 100 WHERE id = $1', [1]);
    await pool.query('UPDATE accounts SET balance = balance + 100 WHERE id = $1', [2]);
    await pool.query('COMMIT');

    Reveal the reference answer
    reference.javascript
    const client = await pool.connect();
    try {
      await client.query('BEGIN');
      await client.query('UPDATE accounts SET balance = balance - 100 WHERE id = $1', [1]);
      await client.query('UPDATE accounts SET balance = balance + 100 WHERE id = $1', [2]);
      await client.query('COMMIT');
    } catch (err) {
      await client.query('ROLLBACK');
      throw err;
    } finally {
      client.release();
    }

    Authored expected output: All transaction statements run on one borrowed connection and the client is released.

    pool.query may use a different connection each call, so the BEGIN and the updates can land on different sessions. Borrow one client for the whole transaction.

  2. Exercise 2Explain

    Why can a large pool per serverless instance exhaust the database?

    starter.text
    max = 20 per instance, 100 instances

    Reveal the reference answer
    reference.text
    Each instance opens its own pool, so 100 instances times 20 connections is 2000 connections, far beyond a typical database limit. Serverless instances scale horizontally, so the total connection count multiplies.

    Authored expected output: Explains that per-instance pools multiply with instance count and exceed the database limit.

    Pool sizing must account for the number of concurrent processes, not just one process.

Common misconceptions

#
  • A bigger pool is always faster: beyond the database's capacity, extra connections add contention.
  • pool.query and a borrowed client are interchangeable inside a transaction: only the borrowed client keeps one connection.

Follow-up practice

#
  • backend-databases-mongo-documents

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.