// Glossary

Connection Pooling

Last updated ·

Connection pooling is the practice of keeping a set of open database connections and lending them to clients as needed, instead of opening a new connection for every request. A pooler such as PgBouncer sits between the application and Postgres and carries many client connections over a smaller number of real database connections.

Why does pooling matter for serverless and edge code?

Postgres connections are expensive. Neon's guide to connection pooling explains that "each Postgres connection creates a new process in the operating system, consuming memory and CPU resources," and that Postgres limits the number of connections based on available memory. Serverless code tends to open a connection per invocation, so a traffic spike becomes a connection spike, and the database starts refusing clients. Neon's pooled endpoint, which runs PgBouncer, accepts up to 10,000 client connections and funnels them into a much smaller set of real ones.

Distance adds a second cost. Cloudflare's Hyperdrive documentation notes that it "maintains a pool of connections to your database" placed to minimize latency, and that its pooler "operates in transaction mode". Cloudflare also notes that setting up a new connection "may require multiple round trips to the database", so pooling also removes a common cause of slow first requests, the database side of a cold start.

What are the pooling modes?

PgBouncer's feature list defines three modes, and the choice changes what application code may do:

  • Session pooling: a server connection is assigned to a client for as long as it stays connected. "This mode supports all PostgreSQL features."
  • Transaction pooling: a server connection is assigned "only during a transaction". This mode "breaks a few session-based features of PostgreSQL."
  • Statement pooling: transaction pooling in which multi-statement transactions are not allowed.

How does EOXLABS use connection pooling?

Workers that query Postgres go through Hyperdrive or Neon's pooled endpoint, both in transaction mode. Work that depends on a session, such as schema migrations, LISTEN/NOTIFY or session-level advisory locks, uses a direct connection instead. Settings that must apply to a query are made with SET LOCAL inside a transaction, so they cannot leak. Both services sit in the EOXLABS stack, on the Hyperdrive and Neon Postgres pages.

Why do two transaction-mode poolers behave differently?

Transaction mode is not one behavior. Neon warns that "PgBouncer doesn't reset a server connection when it returns to the pool in transaction mode", so a plain SET search_path can carry over to another client's queries. Hyperdrive documents the opposite: "When a connection is returned to the pool, the connection is RESET", so a SET does not reach later queries. Code that relies on either behavior is fragile; SET LOCAL works the same under both.

What is connection pooling confused with?

  • An in-process pool. A pool held in a Worker's memory belongs to one isolate. Other isolates cannot share it, which is why pooling for edge code lives in a service in front of the database.
  • A bigger pool is faster. Hyperdrive advises to "start conservatively" and to keep the total connection count within the database's limit across every configuration.
  • Query caching. Hyperdrive can also cache query results. That is a separate feature with its own freshness trade-offs.

More database terms are in the glossary.

→

Related

+

More glossary

Have a system that can’t go dark?

Start a project →