Applications and operations

Budget connection pools

Limit connections across all application replicas and preserve room for workers, migrations, and emergency access.

8 minute lesson

~~~

Create one bounded pool in each application process:

import pg from 'pg'

const { Pool } = pg

export const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  max: 5,
  connectionTimeoutMillis: 5000,
  idleTimeoutMillis: 30000,
  statement_timeout: 10000,
  application_name: 'notes-web',
})

Five connections is an example budget. Eight replicas with a pool of five can still request forty connections.

Leave room for workers, migrations, and emergency access. Call await pool.end() when a script or worker shuts down.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →