Edge + @neondatabase/serverless Pool: create, use, close per request
Hoisting a Pool to module scope on Vercel Edge or Cloudflare Workers leaks WebSockets across requests. The docs require the full lifecycle inside one handler.
Edge Pool lifecycle: per request, not per module
The trap
On a normal Node server you create one Pool at startup and share it. On Edge runtimes (Vercel Edge, Cloudflare Workers), WebSocket connections cannot outlive a single request. A module-scope Pool looks fine on the first request and then degrades: stale sockets, hangs, growing memory.
The rule (from the driver docs)
Pool or Client objects must be connected, used, and closed within a single request handler. Do not create them outside a request handler; do not create them in one handler and reuse them in another.
Checklist
- Factory function per handler,
pool.end()in a finally block. - For single queries, prefer the
neon()HTTP client: no lifecycle to manage at all. - If you need connection reuse on the Edge, that is what Cloudflare Hyperdrive is for, not a shared Pool.
- Load-test with sequential requests, not just one: the bug only shows on request two and later.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.