Does anyone here have tips for handling DB connect...
# orm-help
b
Does anyone here have tips for handling DB connections using Next.js and the serverless functions? I’m running into issues where there are too many connections open.
c
I'm curious about this too!
d
This is a very common problem and there are several solutions. If you’re using PostgreSQL, I’d check out PgBouncer. There’s also this video which explains how to set up PgBouncer with Vercel and Digital Ocean

https://www.youtube.com/watch?v=tsOHw2T1HvI

Also check out this talk:

https://www.youtube.com/watch?v=SmKJnITMQZw

which explains the challenges of connection pooling
b
Amazing thanks! Do you know if this is possible with AWS RDS by any chance?
r
You can use PgBouncer in front of RDS as a connection pooler.
👍 1
b
(edit) I guess my next question would be if there is some documentation regarding connecting to a DB hosted on EC2? I couldn’t directly find one.
@Ryan or @Daniel Norman if you could point me to a link or example, that would be awesome! Sorry for all the questions but I really want to make Prisma work 🙂
d
I guess my next question would be if there is some documentation regarding connecting to a DB hosted on EC2? I couldn’t directly find one. (edited)
In essence that would work the same way as any other database. You have to configure the correct DATABASE_URL.
In general though, I wouldn’t recommend deploying a database to EC2. You’re much better off using a hosted service like AWS RDS, DigitalOcean, or Supabase. Both Supabase and DigitalOcean offer a managed PgBouncer which should work pretty well for you
💯 1
b
Got it, yeah I have it hosted on AWS RDS right now but unfortunately I would have to host the pgBouncer on EC2 as there is no direct access to RDS’s shell to install pgBouncer in front of it.
👍 1
d
you can use serverless proxy with rds
d
There’s no benefit to using RDS Proxy with Prisma (you don’t get any of the connection pooling benefits it gives). See the docs for more details https://www.prisma.io/docs/guides/deployment/deployment-guides/caveats-when-deploying-to-aws-platforms#aws-rds-proxy
d
ah, didnt know this, thanks
👍 1
b
just to follow up, this is probably not within the scope of support but if you could add documentation for how to do it with RDS that would be amazing, I decided to switch to supabase to have the connection pooler but would have like to stay with RDS and might consider having to switch to a different stack later on…
c
Maybe Amazon Aurora has something that would help with connection pooling? https://aws.amazon.com/rds/aurora. It's basically a MySQL/Postgres-compatible cloud-native database engine with lots of extra features vs vanilla RDS, and not much more expensive.
d
@Ba Thien Tran Unfortunately it’s currently not possible to use RDS Proxy with Prisma due to the lack of support for prepared statements. The good news is that we’re working to improve this. Feel free to sign up for Early Access to Prisma’s upcoming Data Proxy for serverless backends: https://prisma.slack.com/archives/C01739JGFCM/p1625062714026400
d
We had some benefit from excluding pinned queries when using RDS proxy but I'm not sure how large the benefits are or if there even are any from doing this. I would personally avoid using aws in the current state as I don't think they are suitable for prisma in a serverless environment at this point
I would be interested in hearing from anyone who has used aurora serverless 2 but the pricing is higher due to using dedicated instances
@Chris Tsongas aurora serverless 1 has issues with scaling time due to it being unable to rapidly provision instances to scale to spikes in demand
traditional aurora has nothing to deal properly with serverless connection pooling a la pgbouncer
d
I would personally avoid using aws in the current state as I don’t think they are suitable for prisma in a serverless environment at this point
If you’re looking for a ready to use solution, this is true, however, it shouldn’t be too complicated to run PgBouncer on AWS Fargate which solves the problem. In the long term, Prisma will provide a solution for this that should work natively on AWS and would not require you to run any additional infrastructure.
d
ah, I’m speaking from using it for Lambda in prod ~8 months ago for a serverless solution, we have shifted to using ECS with fargate for using prisma in prod now
💡 1
when I say aws, I mean AWS with an out-of-the-box solution
💯 1
there needs to be some configuring
👍 1
e
@Daniel Norman @Dominic Hadfield Hello! wondering if you have some references or articles that would help setting up PgBouncer on AWS Fargate 🙏