Next.js Apps Can Exhaust PostgreSQL Connections Through Module-Scoped Pools
A production Next.js app on a small managed PostgreSQL cluster began failing all queries at peak load with connection-slot errors, even though the pool size seemed within limits. The cause was that Next.js compiles server code into several separate entry kinds, so a module-level database singleton was instantiated once per compiled copy, each opening its own pool. The author reproduces the issue on Next.js 16 against a PostgreSQL instance with a 12-connection limit. The suggested fix is to store the client on globalThis rather than in module scope, so all compiled copies share one pool. A boot-time check is also recommended to keep the connection budget from drifting again.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.



Discussion (0)
Log in to join the discussion and vote.
Log in