Connection Pooling
A connection pool 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.
Connection pooling 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 via the database connection string in your Platform project (not the Accelerate connection string in your Prisma schema) via the connection_limit
parameter. For example, set a connection pool size of 10
like this: postgresql://USER:PASSWORD@HOST:PORT/DATABASE?connection_limit=10
.
If nothing is configured, a default value will be used.
To change the size of the connection pool:
- Open the Console.
- Select the project in which you're using Accelerate. You might need to select a different workspace if you can't see your project.
- Select the environment where you want to configure Accelerate's connection pool size.
- Update the connection string by appending the
connection_limit
argument.
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 time frame 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.