Skip to content
Dashboard

The real serverless compute to database connection problem, solved

CTO, Vercel

Copy link to headingYou can stop reading now if

Copy link to headingThe ground truth

Copy link to headingThe real problem

A pool client idle time should fire while the function is suspended, but it never does because the function is suspended. This “leaks” the pool client.A pool client idle time should fire while the function is suspended, but it never does because the function is suspended. This “leaks” the pool client.
A pool client idle time should fire while the function is suspended, but it never does because the function is suspended. This “leaks” the pool client.

Copy link to headingWhy suspension leaks connections

Copy link to headingSo, is this a real problem?

Copy link to headingIs there a solution?

import { Pool } from "pg";
import { attachDatabasePool } from "@vercel/functions"
const pool = new Pool({
connectionString: process.env.POSTGRES_URL,
});
attachDatabasePool(pool);

Copy link to headingCost implications

Copy link to headingBest practices

Copy link to headingConfigure relatively low connection idle timeouts

Copy link to headingDefine your connection pools globally

Copy link to headingDon’t set the maximum pool size to 1

Copy link to headingUtilize rolling releases

Copy link to headingSummary

Ready to deploy?