All Articles

Connecting To Databases In Serverless Functions

Jack Herrington
Jack Herrington
August 14th, 2023

Question: With next js serverless architecture, what do you usually do to properly manage calls to db's?

Answer: From a security perspective use connection strings, or credentials, stored in environment variables that you then reference in your serverless functions. Other than that, the specific mechanisms of connecting to the database remain the same.

From a deployment architecture if you are hosting the database instance yourself then you should deploy that database with the same cloud provider and in the same region, preferably within the same private virtual cloud. This would allow you to heavily restrict database access to just the serverless functions acting as the entrypoint.

But what I'm guessing is really at the crux of this concern is the cold start issues with serverless functions. When you first access a node based serverless (or lambda) function it can take a while, often several seconds, for that serverless function to start up. If the serverless function is connecting to a database you might guess that the issue is with the latency of starting the database conncetion, that's certainly possible, but more likely it's just the slowness of the serverless architecture parsing through the massive deployd set of node modules.

There are ways to mitigate the cold start problem. But in reality it's primarily an issue with small deployments. Once a serverless function is up and running it's usually quick to respond (depending on what it's doing). The only issue comes when the function spins down again because nobody is using it. So if you have a service that is rarely used it can feel slow because it spins up, services a couple of requests, then spins down again. With lots of traffic this really isn't an issue because there are always functions that are warm and available for requests.

If you don't have a lot of traffic on your service you might want to try a different architecture, like using Docker and deploying to a container service like Amazon's ECS or EKS. Docker instances spin up a lot faster and they scale nicely under laod.

If you are using NextJS then you can use API routes (with the Pages Router) or Route Handlers (with the App Router) to connect to the database. In a Docker setup this functions would deploy with the SSR server. If you deploy to Vercel then those routes (as well as any SSR routes) will be independently deployed as managed serverless functions. In addition Vercel has done a decent job mitigating the cold start on these functions so the overall deployed applications, even with low traffic, feel snappy.

Share this article with your friends

Subscribe for Free Tips, Tutorials, and Special Discounts

We're in this together!

We respect your privacy. Unsubscribe at any time.

Jack Herrington

Written by Jack Herrington

Jack Herrington is a Full Stack Principal Engineer who orchestrated the rollout of React/NextJS at Walmart Labs and Nike. He is also the "Blue Collar Coder" on YouTube where he posts weekly videos on advanced use of React and NextJS as well as other frontend technologies trends. His YouTube channel hosts an entire free courses on React and TypeScript. He has written seven books including most recently No-BS TypeScript which is a companion book to the YouTube course.