Glitch sleep/wake cycle vs. Node.js database connection pool: preventing intermittent exhaustion
0 reputation · 08 May 2024, 10:20 UTC
0 reputation · 08 May 2024, 10:20 UTC
I'm running a Node.js project on Glitch that connects to an external database through a connection pool. Under low traffic, the project sleeps after about 5 minutes without HTTP requests, and I suspect this sleep/wake cycle is behind intermittent pool exhaustion: after several wake cycles, the pool appears to hit its limit even though actual query volume is low.
My understanding is that sleep terminates open sockets, but I'm unsure whether pooled connections are always cleaned up, especially TLS-wrapped ones, and whether stale connections can remain counted on the database server side. Glitch doesn't seem to expose a dedicated before-sleep hook, so the options appear to be listening for process events like SIGTERM to drain the pool, or tuning settings such as idleTimeoutMillis below the sleep interval — but it's unclear how reliable either approach is across drivers.
How should a pool be configured or shut down so connections are fully released before Glitch puts the project to sleep? Is there a reliable signal or event to hook into, and what trade-offs does aggressive idle-timeout tuning introduce for wake-up latency?
32275 reputation · 08 May 2024, 18:57 UTC
When a Node.js project on Glitch enters its default idle sleep, the underlying process is terminated after roughly five minutes of inactivity. This abrupt kill can leave database connection pool state in an uncertain position, especially for pooled connections that wrap TLS sessions. Below is a breakdown of the likely explanation, confirmed facts, and the minimal steps needed to prevent intermittent pool exhaustion.
Glitch’s sleep cycle sends a SIGKILL or equivalent signal to the Node.js process. Most database drivers will close the underlying socket on process termination, but they do not always return the connection to the database server’s idle pool in an orderly fashion. If the driver relies on reference counting and the pool object is garbage‑collected without an explicit drain, the server may still count the session as active. Repeated wake‑sleep cycles can therefore accumulate stale or “zombie” connections, making the pool appear exhausted even when query volume is low.
idleTimeoutMillis in the connection pool configuration to a value lower than Glitch’s sleep interval (e.g., 300000 ms = 5 min, or lower such as 60000 ms). This prompts the driver to proactively release idle connections before the process sleeps.process.on('SIGTERM') handler (Glitch can send SIGTERM during graceful shutdown) that calls pool.end() or driver‑specific cleanup to ensure all sockets are returned or closed before the process exits.beforeExit hook, attach the same drain logic there as a fallback.SELECT 1 for PostgreSQL, SELECT 1 for MySQL, or a simple db.adminCommand("ping") for MongoDB) to verify the pool is healthy before resuming full traffic.Lowering idleTimeoutMillis reduces server‑side stale connections but increases wake‑up latency: the pool must recreate connections on the first request, which can add roughly 10–100 ms per new connection depending on the database and network latency. If Glitch triggers sleep via internal scheduling rather than an explicit SIGTERM, a SIGTERM handler may not fire in time, potentially causing request loss on the first post‑wake request.
To determine whether the above adjustments are necessary for your setup, please specify: the Node.js database driver in use (e.g., pg, mysql2, mongoose) and whether the Glitch project runs on the free tier with a hardcoded 5‑minute sleep window.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.