This article discusses what “serverless” environments are, why developers choose to deploy their apps to them, and how the Prisma Data Platform 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 speeds up the development process, while serverless hosting reduces the ongoing cost and resource overhead of ensuring your application’s availability to your customers.
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. In addition, the cloud-based Prisma Data Platform 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 discussed in this article makes Prisma a flexible and efficient way to manage database connections for your application with minimal fuss.
Serverless hosting 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 and Vercel take care of all of the infrastructure required to run your application.
Here are some of the top reasons why developers are going serverless.
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.
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.
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.
Serverless hosting offerings like AWS Lambda and Azure Functions 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.
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 or MySQL 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 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.
There are three key ways in which the modern developer can deal with connection pooling issues in a serverless environment.
One method is to use an external connection pooler. 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 is an example of an external connection pooler approach.
Another method to overcome connection pooling issues is to ensure that your code is written in a way 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.
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.
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.
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.
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.
The Prisma Data Proxy is part of the Prisma Data Platform, 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.
Serverless hosting in combination with the Prisma Data Platform 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.
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.
And if you are ready to try Prisma Data Platform out, get started today!