Use connection pools
Use connection pools to manage the lifecycle of database connections established by serverless functions. Connection pools health-check connections and re-establish broken connections in the event of a communication error. When creating connection pools in serverless functions:- Set the maximum connection pool size to 1, unless your function is multi-threaded and establishes multiple concurrent requests to your database within a single function instance.
- Do not set a minimum idle connection count. The connection pool should be free to open connections as needed.
- If supported by your pooling library, set the maximum lifetime on the connection pool to 30 minutes.
Persist connection pools across function invocations
If you plan to invoke a serverless function at a high frequency, you should configure the function to persist connection pools across function invocations, to limit the number of new connection attempts to the database. This is typically done by initializing the connection pool variable outside the scope of the serverless function definition. For example, suppose you are writing an AWS Lambda function that s data into a table on a CockroachDB cluster, and you plan to run this query every few seconds. To persist a connection pool across invocations, initialize the connection pool variable outside of the handler function definition and then define the connection pool in the handler only if the pool does not already exist.- node.JS
- Python
A node.JS function that implements this pattern could look similar to the following:
Use CockroachDB Standard
As a database-as-a-service, CockroachDB Standard abstracts away the complexity of deploying, scaling, and load-balancing your database. Additionally, idle database connections to CockroachDB use very little memory (~20-30 KiB) when compared to PostgreSQL (~2-10 MiB). To create a free CockroachDB Standard cluster:- . If this is your first CockroachDB Cloud organization, it will be credited with $400 in to get you started.
- On the Get Started page, click Create cluster.
- On the Select a plan page, select Standard.
- On the Cloud & Regions page, select a cloud provider (GCP or AWS).
- In the Regions section, select a region for the cluster. Refer to for the regions where CockroachDB Standard clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions.
- Click Next: Capacity.
- On the Capacity page, keep the at the default value of 2 vCPUs. Click Next: Finalize.
- On the Finalize page, name your cluster. If an active free trial is listed in the right pane, you will not need to add a payment method, though you will need to do this by the to maintain your organization’s clusters. Click Create cluster. Your cluster will be created in a few seconds and the Create SQL user dialog will display.

