Perf connection pooling

From Intense-Visions/harness-engineering · View interactive version

Perf connection pooling is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Master database connection pooling — pool sizing formulas, connection lifecycle overhead, PgBouncer transaction-mode pooling, serverless connection management, pool monitoring and diagnostics, and c

How to use this skill

Skill file

Connection Pooling

Master database connection pooling — pool sizing formulas, connection lifecycle overhead, PgBouncer transaction-mode pooling, serverless connection management, pool monitoring and diagnostics, and configuration for PostgreSQL, MySQL, and managed database services.

When to Use

Instructions

  1. Understand connection overhead. Each PostgreSQL connection consumes ~5-10MB of RAM on the server and requires a TCP handshake plus TLS negotiation:

```

Connection establishment cost:

Per-connection memory (PostgreSQL):

```

  1. Size the pool correctly. The optimal pool size depends on workload type, not application concurrency:

```

Formula (Brian Brazeal / PostgreSQL wiki):

pool_size = (core_count * 2) + effective_spindle_count

For SSD-backed servers:

pool_size = core_count * 2 + 1

Example: 4-core database server with SSD

pool_size = (4 * 2) + 1 = 9 connections

For I/O-bound workloads (many external API calls during transactions):

pool_size = core_count * 4

```

The counterintuitive rule: smaller pools perform better. A pool of 10 connections on a 4-core server outperforms a pool of 200 because fewer connections mean less context switching, less lock contention, and less memory pressure.

  1. Configure application-level pooling. Set min, max, idle timeout, and connection lifetime:

```typescript

// Node.js with pg (node-postgres)

import { Pool } from 'pg';

const pool = new Pool({

host: process.env.DB_HOST,

max: 10, // maximum connections in pool

min: 2, // minimum idle connections

idleTimeoutMillis: 30000, // close idle connections after 30s

connectionTimeoutMillis: 5000, // fail if connection not acquired in 5s

maxLifetimeMillis: 1800000, // recycle connections every 30 min

});

// Always return connections to the pool

async function query(sql: string, params?: any[]) {

const client = await pool.connect();

try {

return await client.query(sql, params);

} finally {

client.release(); // CRITICAL: always release back to pool

}

}

```

```typescript

// Prisma connection pool configuration

// In DATABASE_URL:

// postgresql://user:pass@host:5432/db?connection_limit=10&pool_timeout=5

// Or in schema.prisma for more control:

const prisma = new PrismaClient({

datasources: {

db: {

url: process.env.DATABASE_URL + '?connection_limit=10&pool_timeout=5',

},

},

});

```

  1. Deploy PgBouncer for external pooling. PgBouncer sits between the application and PostgreSQL, multiplexing many client connections onto fewer database connections:

```ini

; pgbouncer.ini

[databases]

myapp = host=pg-primary port=5432 dbname=myapp

[pgbouncer]

listen_addr = 0.0.0.0

listen_port = 6432

auth_type = scram-sha-256

auth_file = /etc/pgbouncer/userlist.txt

; Pool mode: transaction (recommended), session, or statement

pool_mode = transaction

; Pool sizing

default_pool_size = 20 ; connections per user/database pair

max_client_conn = 1000 ; total client connections accepted

max_db_connections = 50 ; max connections to actual PostgreSQL

reserve_pool_size = 5 ; extra connections for burst handling

; Timeouts

server_idle_timeout = 60 ; close idle server connections after 60s

client_idle_timeout = 300 ; close idle client connections after 5min

query_timeout = 30 ; kill queries running longer than 30s

```

```

Architecture:

[App Server 1] ──┐ ┌── [PostgreSQL]

[App Server 2] ──┤── [PgBouncer] ──────┤ max_connections = 100

[App Server 3] ──┘ 1000 clients └── 50 actual connections

→ 50 DB conns

```

  1. Handle serverless connection challenges. Serverless functions create new connections on every cold start. Use external pooling:

```typescript

// Neon serverless driver — uses WebSocket, handles pooling externally

import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL);

// Supabase with connection pooling (uses PgBouncer internally)

// Use port 6543 for pooled connections instead of 5432

// postgresql://user:pass@host:6543/db?pgbouncer=true

// Prisma with Accelerate (managed connection pooling)

// DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=..."

// AWS RDS Proxy — managed connection pooling for Lambda

// Configure in AWS console, use proxy endpoint instead of RDS endpoint

```

  1. Monitor pool health. Track pool metrics to diagnose issues before they cause outages:

```typescript

// node-postgres pool events

pool.on('connect', () => {

metrics.increment('db.pool.connect');

});

pool.on('acquire', () => {

metrics.increment('db.pool.acquire');

});

pool.on('remove', () => {

metrics.increment('db.pool.remove');

});

pool.on('error', (err) => {

metrics.increment('db.pool.error');

console.error('Pool error:', err.message);

});

// Periodic health check

setInterval(() => {

metrics.gauge('db.pool.total', pool.totalCount);

metrics.gauge('db.pool.idle', pool.idleCount);

metrics.gauge('db.pool.waiting', pool.waitingCount);

if (pool.waitingCount > 0) {

console.warn(${pool.waitingCount} queries waiting for connections);

}

}, 5000);

```

  1. Handle connection failures gracefully. Connections break due to network issues, database restarts, or failovers:

```typescript

// Retry with exponential backoff for transient connection failures

async function resilientQuery(sql: string, params?: any[], retries = 3) {

for (let attempt = 0; attempt < retries; attempt++) {

try {

return await pool.query(sql, params);

} catch (err: any) {

const isTransient = [

'57P01', // admin_shutdown

'57P03', // cannot_connect_now

'08006', // connection_failure

'08001', // sqlclient_unable_to_establish_sqlconnection

].includes(err.code);

if (!isTransient || attempt === retries - 1) throw err;

await new Promise((r) => setTimeout(r, 100 * Math.pow(2, attempt)));

}

}

}

```

Details

Transaction Mode vs Session Mode (PgBouncer)

In transaction mode, PgBouncer assigns a server connection for the duration of a transaction, then returns it to the pool. This achieves the highest multiplexing ratio (1000 clients on 20 connections). Limitation: session-level features (SET, LISTEN/NOTIFY, prepared statements, temporary tables) do not work across transactions. In session mode, a server connection is held for the entire client session. Lower multiplexing but full feature compatibility. Most applications should use transaction mode.

Worked Example: Discord Serverless Migration

Discord migrated their message history API from dedicated servers to serverless functions, which caused connection storms — 10,000 concurrent Lambda invocations each opening a database connection. They deployed PgBouncer on a dedicated instance, configured with pool_mode = transaction, default_pool_size = 25, and max_client_conn = 15000. The PgBouncer instance multiplexes 15,000 Lambda connections onto 25 actual PostgreSQL connections. Result: PostgreSQL CPU dropped from 90% to 35%, and p99 latency decreased from 200ms to 45ms because the database was no longer thrashing with 10,000 concurrent connections.

Worked Example: Vercel Edge Functions + Neon

Vercel's edge functions run in globally distributed isolates that cannot maintain persistent TCP connections. Neon's serverless driver uses HTTP/WebSocket transport instead of raw TCP, with connection pooling handled by Neon's proxy layer. Each edge function invocation sends a query over HTTP — no connection setup, no pool management. The proxy maintains a warm pool of PostgreSQL connections. Result: cold-start database queries complete in 5-10ms (HTTP overhead) versus 50-100ms (TCP connection establishment), and the PostgreSQL server sees a stable 50 connections regardless of edge function concurrency.

Anti-Patterns

Setting pool size equal to max_connections. If pool_size matches max_connections, one application instance monopolizes all database connections, leaving zero for other services, migration scripts, or admin access. Always reserve 20-30% of max_connections for non-application use.

Not releasing connections back to the pool. A missing client.release() (or failed finally block) causes a connection leak. The pool gradually exhausts. Always release in a finally block or use pool.query() which handles acquisition and release automatically.

Opening a new pool per request. Creating a new Pool() in a request handler establishes a fresh pool for each request instead of reusing connections. Create the pool once at application startup and reuse it.

Oversizing the pool. A pool of 100 connections on a 4-core database causes context-switching overhead and lock contention that degrades throughput. Start with (cores * 2) + 1 and increase only with load testing evidence.

Source

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.