Connection Pooling

Connection pooling

A is a storage of database connections that can be reused for future requests to the database. When a new connection is requested, it is retrieved from the pool if one is available. Once the connection is no longer needed, it is returned to the pool for reuse.

is important as it allows you to reuse existing connections instead of creating new ones, which can be an expensive operation.

The efficient management of database connections allows the database to process more queries without exhausting the available database connections, making your application more scalable.

Accelerate provides built-in connection pooling by default. By simply using Accelerate, you get the benefits of connection pooling without having to configure anything. However, you can also configure the connection pool to suit your needs.

For more information about connection pooling in Prisma, see the documentation here.

Default connection pool size

By default, Accelerate calculates a default connection pool size using the formula num_physical_cpus * 2 + 1.

For example, a machine with 2 physical CPUs will have a default connection pool size of 2 * 2 + 1 or 5.

This means that Accelerate will create a maximum of 5 connections to the database. If more than 5 connections are requested, Accelerate will queue the requests until a connection is available.

Configuring the connection pool size

The connection pool size can be configured to a value other than the default via the database connection string.

Update database connection string in Accelerate

To adjust the connection pool size, you can add the connection_limit parameter to the database connection string. The value of connection_limit is the maximum number of connections that Accelerate will open against your database.

For example, here is how you can set a connection pool size of 10:

postgresql://user:password@localhost:5432/db?connection_limit=10

Configuring the connection pool timeout

The connection pool timeout is the duration, measured in seconds, during which the query engine must process a specific query; failing to do so within this timeframe results in an exception being thrown, and the system proceeds to the next query in the queue.

Similar to the connection pool size, you may also configure the connection pool timeout via the database connection string. To adjust this value, you may add the pool_timeout parameter to the database connection string.

For example:

postgresql://user:password@localhost:5432/db?connection_limit=10&pool_timeout=20

The default value for pool_timeout is 10 seconds.