We do not recommend deploying Blitz to Vercel. Vercel is a platform optimized for frontend deployments, not fullstack. While you can deploy to Vercel, most people run into too many issues and give up. Direct SQL database connections are one of the biggest issues. You have to pay for a very expensive database to support any amount of scale.
There are three main issues when it comes to using SQL databases in a serverless environment:
Because Vercel doesn't provide databases, you will use a separate platform for your database. And therefore people accidentally deploy their app to a different region than their database.
Solution: Deploy your app and database to the same region. By default Vercel apps are deployed to Washington D.C., so your database should be as close as possible to that.
Ideally you will set up your database client and database connection outside your API handler (this is the default setup in Blitz apps). This optimizes performance because subsequent requests to the same lambda already have an active database connection.
However, this can result in a large number of idle connections, larger than what your database can support. For reference, the lowest database tier on Digital Ocean has a limit of 22 connections.
Solution: Use a connection pool in front of your database. PgBouncer is the most popular connection pool for PostgreSQL. Both Digital Ocean and Heroku have a feature to simply add PgBouncer.
PgBouncer for PostgreSQL is advertised as allowing you to have 5,000-10,000 active database connections, but it's critical to understand that those are idle connections. PgBouncer does not increase your core database capacity. If your database has a max of 22 connections, then you can only have 22 simultaneous transactions, regardless of whether you have PgBouncer in front of it or not.
Solution: Increase your database size to meet the traffic needs for your app. But be aware this can become very expensive.
Note: Slow database transactions will greatly decrease your throughput. If it seems you are running out of connections before you should, based on your app traffic, then turn on logging for your database queries and look for long transactions.
This assumes your Blitz app is already set up for Postgres. If it's not, first follow these instructions
This also assumes you already have a Vercel account.
&pgbouncer=true
to the end of your connection string for
Prisma to work correctly.NODE_ENV=production blitz build && blitz prisma migrate deploy
so
that the production DB will be migrated on each deployvercel env add DATABASE_URL
SESSION_SECRET_KEY
environment variable (It needs to be 32
characters long)openssl rand -hex 16
in your terminal.git push
if using the
Git Integration or
vercel
if using Vercel CLI