Using Prisma to Address Connection Pooling Issues in Serverless Environments
Tags: javascript, webdev, serverless, database
Published: 2021-12-06 14:57:32

This article discusses what “serverless” environments are, why developers choose to deploy their apps to them, and how the [Prisma Data Platform](https://cloud.prisma.io/) solves the problems that can arise regarding database connection pooling.

Choosing which tools you will use to build your application, and which platform you will be deploying to in production, are early and vital decisions that you will have to make when building a product. 

Using an ORM like [Prisma](https://www.prisma.io/) speeds up the development process, while serverless hosting reduces the ongoing cost and resource overhead of ensuring your application’s availability to your customers.

## What is Prisma?

[Prisma](https://www.prisma.io/) is a next-generation ORM ([Object-Relational Mapping](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping)) library for Node.js and TypeScript.

Prisma allows you to build your app in JavaScript or TypeScript and work with your data as objects rather than SQL or NoSQL queries. These objects are mapped to a database using a schema that generates all the types and conversions required to accurately store your objects in your database platform.

The Prisma toolset includes developer tools for generating database migrations so that your database is always in-sync with your data mode as defined in the [Prisma schema](https://www.prisma.io/docs/concepts/components/prisma-schema). In addition, the cloud-based [Prisma Data Platform](https://cloud.prisma.io/) provides tools like user/role management and a data browser that allow you and your team to collaboratively manage your data online.

**Using Prisma in your projects means spending less time worrying about your database, and more time to focus on building your app.**

In addition to being a great aid to development, the [Prisma Data Platform](https://cloud.prisma.io/) discussed in this article makes Prisma a flexible and efficient way to manage database connections for your application with minimal fuss.

## What is “serverless” and why do developers “go serverless”?

[Serverless hosting](https://en.wikipedia.org/wiki/Serverless_computing) is a means of delivering your app to your user without having to build and maintain a server to deploy it to.

It may sound strange, but it is a cost effective way to deploy applications. Serverless cloud hosting providers like [AWS Lambda](https://aws.amazon.com/lambda/) and [Vercel](https://vercel.com/) take care of all of the infrastructure required to run your application.

Here are some of the top reasons why developers are going serverless.

### Reduced infrastructure requirements

The hosting provider administers all of the infrastructure required to host your app—maintaining it, keeping it up-to-date, and most importantly, keeping it secure. The provider deals with the day-to-day burdens of hosting.

### Streamlined deployment

With serverless hosting you are provided tools to readily upload, run, and monitor your code. When a request comes in, your code is launched in a container and executed. The serverless environment discards the container when it is no longer needed.

### Reduced costs

In addition to the time and resources saved by not having to maintain your own server infrastructure, you also save on the cost of running a full server 24/7. Many serverless hosting providers like AWS and Azure will only bill you for the resources you use.

### Scalability

Serverless hosting offerings like [AWS Lambda](https://aws.amazon.com/lambda/) and [Azure Functions](https://azure.microsoft.com/en-us/services/functions/?cdn=disable) can support sustained high load thanks to the cloud providers’ globally distributed infrastructure. In addition, such systems are well-suited for handling bursts of high load amid periods of lower use due to their automatic scaling functionality.

## The issues with “connection pooling” in serverless environments

Any database, serverless or not, has a limit on the number of open connections that it can handle at one time. For each open connection, the database system like [PostgreSQL](https://www.postgresql.org/) or [MySQL](http://www.mysql.org/) reserves network, memory, and compute resources so that it’s able to handle any incoming requests through each connection.
 
In a serverless environment your code does not run constantly. A user makes a request (whether it’s viewing a webpage or requesting an API resource), and a tiny containerized executing environment (called an instance) running only the code required to serve the request is launched.  The request is served, and the instance is destroyed.

This is efficient, but there is one problem. In traditional hosting environments with a single server that runs your application,  a single instance of the application can service multiple user requests.

In a traditional hosting environment a single instance of the application will be managing a **single** [connection pool](https://en.wikipedia.org/wiki/Connection_pool) to your database. The connection pool ensures that any database queries are within the connection and timeout limits that you have configured. It coordinates the re-use of database connections and prevents the database server from being overloaded.

In a serverless environment, a new instance of your code is launched for every user request. Each instance will be unaware of the others, resulting in **multiple** connection pools. As these separate connection pools are not aware of one another, they cannot coordinate to ensure the database server is not overloaded.

In a serverless environment, there is no shared connection pool. This creates the risk that a small number of user requests exhaust the database connection limit causing the application to stop working.


![Each Serverless function handles one user request which can quickly exceed the database connection limit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cu90f410g7ydf51tym17.png)

## Methods of dealing with with connection pooling issues

There are three key ways in which the modern developer can deal with connection pooling issues in a serverless environment.

### External connection poolers

One method is to use an [external connection pooler](https://www.prisma.io/docs/guides/performance-and-optimization/connection-management#external-connection-poolers). By moving the task of coordinating database connections outside of your serverless app, an external connection pooler solves the issue of the separate execution instances not being able to coordinate with each other.
Not all database servers have an external connection pooler. If this path you choose to mitigate the issue, you should carefully select which tools and platforms will be used in your project early in the development process.

The [Prisma Data Proxy](https://cloud.prisma.io/) is an example of an external connection pooler approach.


![Serverless with the Prisma Data Proxy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rt38aa2ho502ld80uvu0.png)

### Adjusting your code

Another method to overcome connection pooling issues is to ensure that your code is [written in a way](https://www.prisma.io/docs/guides/performance-and-optimization/connection-management#prismaclient-in-serverless-environments) that accounts for the serverless environment in which it will run. Some cloud providers *do* reuse resources between requests, so structuring your code so that connections can be maintained between requests is a way to reduce database load. 

However, this approach is limited and will only help alleviate the problem to some degree. 

### Fine tuning your database server

An important step in deploying to a production environment is to monitor and profile your application. Knowing how your application will be used and how busy it will be allows you to determine what resources to allocate. With this information, you can optimize your database connection to reduce the chance of overloading it. For example, you can reduce the connection timeout for inactive connections so that your database is less likely to be spending resources on a connection that’s not going to be used. Another option is to increase the connection limit on the database, but handling more connections will likely require more compute resources.

In addition to supporting these traditional methods of dealing with connection pooling issues, the Prisma Data Platform introduces a reliable and readily configurable database proxy that solves connection pooling issues—regardless of the database server or database host in use.

## Solving connection pooling issues with Prisma Data Proxy

Prisma has introduced a new service that effectively solves the connection pooling issue when deploying to serverless environments like AWS Lambda and Vercel.

The Prisma Data Proxy acts as an intermediary between your application code and the database, effectively acting as an external connection pooler for any [database supported by Prisma](https://www.prisma.io/docs/reference/database-reference/supported-databases).

### Flexible ORM and proxy with external connection pooling

Using the Prisma Data Proxy solves the connection pooling issue without you having to drastically alter your code or spend extra time tweaking your database connection configuration to account for the serverless environment.

{% youtube GWbzyyziH9A %}


The Prisma Data Proxy also reduces the size of the data bundle that you must upload to your serverless hosting environment. Many of the database functions are outsourced to the highly performant Prisma Engine binary hosted by the Prisma Data Proxy, so they do not need to be included with your deployment bundle.

### Part of the Prisma Data Platform

The Prisma Data Proxy is part of the [Prisma Data Platform](https://www.prisma.io/dataplatform), a cloud based platform that helps manage and collaborate on application data.

The Prisma Data Platform includes a powerful databas tools to assist you and your team during the development process. This includes the Prisma Query Console, which enables you to view and manage your live data through a simple interface. It allows you to gain insight into how your application is used and to quickly fix any arising data issues.

## The benefits of serverless hosting and Prisma 

Serverless hosting in combination with the [Prisma Data Platform](https://cloud.prisma.io/) provides a set of collaborative development and deployment tools for developers who simply want to get on with building their product.
Building and running serverless applications has become a very attractive option for developers due to the reduced resources and costs, and an easier scaling approach.

However, when developing your application you will have to ensure its suitability to be run in a serverless environment. There is nothing inherently wrong with the serverless concept, but there are differences to traditional hosting configurations that must be considered when coding to deploy in such an environment.

The Prisma Data Platform is being actively developed to address the challenges of deploying data-intensive apps to modern serverless platforms while allowing you to use the database and database host you are already comfortable with.

To learn more about databases in the Serverless era and a sneak peak into Prisma's vision for the Prisma Data Platform, check out the talks from the [Prisma Serverless Conference](https://www.prisma.io/serverless). 

### Get started with the Prisma Data Platform

The Prisma Data Platform, including the Prisma Data Proxy, is currently available in Early Access.

Online documentation is available to get you started, so that you are ready to implement the Prisma Data Platform in your project when it is released.

[See the Prisma Data Proxy in action](https://www.youtube.com/watch?v=iyGZ8JFPgoY) or [learn more about how to easily integrate the Prisma Data Platform into your project](https://www.prisma.io/docs/concepts/components/prisma-data-platform#prisma-data-proxy).

And if you are ready to try Prisma Data Platform out, [get started today](https://www.prisma.io/dataplatform)!