Agent Wranglers

SCL-23Required

Fit the connection budget to the autoscaling maximum

The maximum number of copies of each process type times its pool size, summed over all types, stays below the database's connection limit. Check it whenever a maximum or a pool size changes.

How to do it

PostgreSQL's max_connections is typically 100 by default, and 3 of those are kept for superusers. Autoscaling to 20 copies with a pool of 10 asks for 200. New copies then fail to connect just at the peak they were started for. Pool database connections sets the rule for one process. When the sum is too high, put a pooler in front, as the PgBouncer pick explains. Count web, worker and scheduled processes, and leave 10% free for migrations and people.

Examples

JavaScript example

import { test } from 'node:test';
import assert from 'node:assert/strict';
import settings from '../deploy/settings.json' with { type: 'json' };

const SHARE_FOR_APPS = 0.9;

test('pools fit the database connection limit', () => {
  const wanted = settings.processes.reduce((sum, p) => sum + p.maxCopies * p.poolSize, 0);
  const usable = settings.database.maxConnections - settings.database.reservedConnections;
  assert.ok(wanted <= usable * SHARE_FOR_APPS, `${wanted} connections wanted, ${usable} usable`);
});

Python example

import json

SHARE_FOR_APPS = 0.9

def test_pools_fit_the_database_connection_limit():
    settings = json.load(open("deploy/settings.json"))
    wanted = sum(p["maxCopies"] * p["poolSize"] for p in settings["processes"])
    db = settings["database"]
    usable = db["maxConnections"] - db["reservedConnections"]
    assert wanted <= usable * SHARE_FOR_APPS, f"{wanted} connections wanted, {usable} usable"

How to check

A test in CI reads the deploy settings and sums the maximum copies times the pool size for every process type. The sum is at most 90% of max_connections minus the reserved connections.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/scaling/growing-the-database/connection-budget">Agent Wranglers scaling playbook</a>. In code you write, add a comment next to it with the same link.