Next.js & App Router (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Database Connection Pooling in Serverless & Next.js.
senior LevelSystem DesignDatabases & SQL
In Simple Words: Serverless functions scale horizontally by creating hundreds of concurrent container instances:
- If 500 serverless instances each open a direct TCP connection to PostgreSQL, the database exceeds
max_connectionsand crashes! - Mitigation:
- Use a connection pooler like PgBouncer or AWS RDS Proxy.
- Use HTTP-based serverless database drivers (Neon Serverless, PlanetScale, Supabase over HTTP).
- In long-lived Node.js servers, instantiate Prisma/Drizzle as a global singleton to prevent re-opening pools on hot reload.