Question presented to candidate: "After moving our Next.js app to Prisma, Postgres started rejecting connections with 'too many connections', first on developer laptops and later in production with six containers. What is going on, and how do you set it up correctly?"
What a strong answer should cover:
- Every PrismaClient owns a connection pool; creating clients repeatedly multiplies pools.
- In
next dev, edits re-evaluate server modules, so a module-levelnew PrismaClient()creates a new pool per edit; theglobalThissingleton pattern fixes it. - In production, total connections = instances (or serverless invocations) times pool size; size the pool so the sum stays under the database limit.
- Prisma 7 requires a driver adapter (for Postgres,
@prisma/adapter-pg), so pool settings such asmaxandidleTimeoutMilliscome from thepgdriver. - For serverless or many instances, put a pooler (PgBouncer, a managed pooler, Prisma Accelerate) in front of the database, and keep data access in server-only modules.
Clarifying questions expected:
- "How many instances run, and is it serverless?" β the connection math depends on it.
- "What is the database connection limit, and is a pooler already in place?"
Code / implementation expected: Yes β a lib/db.ts singleton with an explicit pool size, plus the connection budget arithmetic.