prisma: Too many idle connections and too little active connections in high traffic
Bug description
Hello, I’m using Google Cloud Run to connect to Cloud SQL Postgres instance with Prisma through Auth Proxy.
Using connection_limit=2 since serverless.
Still too many connections are created and are in the idle state.
Even when 10 cloud run instances are active and 8 are idle, GCP Cloud SQL shows only 2 connections as active and 21 connections as idle.
Tested with 100 concurrent connections to the cloud run endpoint for 60 seconds.
Is using external connection pooling the only solution here?
PS: This is a NestJS app and the Prisma Client is used as a NestJS service.
Cloud SQL connections chart
Cloud Run instances chart
How to reproduce
Using a serverless service like Cloud Run and Cloud SQL with Prisma
Expected behavior
Prisma should’t be creating 20 idle connections for 2 active connections
Prisma information
// Add your schema.prisma
@Injectable()
export class PrismaService implements OnModuleDestroy, OnModuleInit {
constructor(private config: ConfigService) {
logger.debug("Initializing Prisma on startup")
}
db : PrismaClient = new PrismaClient({
datasources: {
db: {
url: this.config.get('DATABASE_URL'),
},
}
})
async onModuleInit() {
this.initializeDb()
}
async initializeDb(){
try {
logger.debug("initializing prisma db")
this.db.$connect().catch(async (e) => {
logger.debug(`Error while connecting to db: ${process.env.DATABASE_URL}`)
logger.debug("Trying again")
await new Promise((resolve) => setTimeout(resolve, 10000));
this.initializeDb()
})
} catch (error) {
logger.error(`Error while connecting to db: ${process.env.DATABASE_URL}`)
logger.error("Trying again")
await new Promise((resolve) => setTimeout(resolve, 10000));
this.initializeDb()
}
}
async onModuleDestroy() {
await this.db.$disconnect();
}
async enableShutdownHooks(app: INestApplication) {
//@ts-ignore
this.db.$on("beforeExit", async () => {
await app.close();
});
}
}
Environment & setup
- OS: Debian
- Database: PostgreSQL
- Node.js version: 20
Prisma Version
5.4.0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (5 by maintainers)
@cryptrr Please share your solution
Hi @cryptrr
I am experiencing the same issue in the same environment. Can you share how you adjusted the scaling or if you solved the problem? Thanks
Hello @cryptrr I have the same issue with the same stack (Cloud Run + Cloud SQL + Prisma). I’m searching a solution aroung prisma pool_size and I see your posts. Could you share you cloud run scaling solution ?
The issue was with cloud run scaling. Closing now.
What do you mean here? Do you have 10 instances total? Or 18 total? What does “are idle” mean? Did they not handle any traffic at all?
So there are a total of 23 connections open, and 2 are active and 21 are shown as idle, correct?
Generally, when a Prisma Client instance handles at least 1 query it will open one connection. If it has to handle multiple queries at the same time, it will open as many connections as needed until the connection limit is hit. It will then keep those connections open (and not just close them again). When the instance does not run any queries, all of these connections will be idle and waiting. When there are queries, it will use these existing connections.
So if you have 18 “servers” with a Prisma Client instance each, it would be normal for at least 18 connections to be open, or even 36 when those instances all handled traffic that made it necessary to open connections up to your connection limit.