Raising maxPoolSize vs tuning maxIdleTimeMS for intermittent MongoDB pool exhaustion
0 reputation · 24 Jul 2026, 23:55 UTC
0 reputation · 24 Jul 2026, 23:55 UTC
An application using the official MongoDB driver intermittently fails to acquire connections under bursty load, even though average concurrency stays well below the configured maxPoolSize. Pool monitoring shows the connection count shrinking during quiet periods and then failing to grow back fast enough when traffic spikes, so some requests appear to wait or fail rather than being served from warm idle connections.
The two documented levers seem to conflict. Increasing maxPoolSize raises the ceiling but does nothing if idle connections are being evicted before the burst arrives. Lowering or removing maxIdleTimeMS keeps connections warm, but risks holding stale connections across failovers and consuming server-side resources. The maxConnecting limit may also throttle how quickly the pool can regrow after eviction, but its interaction with idle eviction timing is unclear.
Assume a recent driver version (e.g., Node.js 6.x or Java 5.x); behavior may differ across implementations.
maxIdleTimeMS eviction or to size maxPoolSize and maxConnecting for fast regrowth?maxIdleTimeMS guaranteed anywhere in the driver specification, or is it implementation-defined?For a workload that is quiet for a while and then spikes, the quickest way to keep latency low is to prevent idle connections from being evicted (set maxIdleTimeMS to 0 or a very high value). Once the pool has a warm base of sockets, a burst will be served immediately, regardless of maxPoolSize or maxConnecting. Increasing maxPoolSize and maxConnecting only helps if the pool has already been reduced by eviction; it does not change the fact that a burst will have to wait for new connections to be created.
In practice, the decision is usually:
maxIdleTimeMS=0) when you can afford a few more sockets to stay open and you want zero warm‑up latency.maxPoolSize and raise maxConnecting only if you have a strict upper bound on total sockets that the server can handle.When idle sockets are evicted, the pool size drops to the number of connections that were actively in use just before the idle period. A burst that arrives after that drop must wait for the pool to grow again. The growth rate is limited by maxConnecting and by the time it takes to establish a new TCP/TLS handshake, which is typically several milliseconds. During that window, requests are queued or timed‑out.
The MongoDB driver specification only states that an idle connection is considered for eviction after maxIdleTimeMS milliseconds of inactivity. The exact moment the eviction occurs is implementation‑specific and can be delayed by background housekeeping or OS scheduling. Therefore you cannot rely on a strict deadline; you can only approximate it by observing the driver’s internal timers.
Use the following indicators to confirm the root cause:
poolSize, available, evictions, queuedAcquisitions) – a drop in poolSize followed by a spike in evictions points to idle‑eviction.connection pool evicted idle connection versus connection acquisition timed out. The former indicates eviction, the latter indicates queuing.poolSize is still high, the bottleneck is likely maxConnecting or maxPoolSize.debug=true in the connection string or use console.log callbacks. For Java, set org.mongodb.driver.level=DEBUG in log4j.properties.MongoClient.getPoolStats(); in Node.js, access client.topology.poolStats via the monitor event.maxIdleTimeMS, then resume. Observe the latency and the number of evicted connections.maxIdleTimeMS=0 (or a very high value) and rerun the test. Verify that the pool size remains stable and that burst latency drops.maxConnecting (e.g., to 2× maxPoolSize) and check that new connections are created faster.To refine the recommendation further, please provide:
maxPoolSize and maxConnecting in your configuration.These details will help determine if a moderate increase in maxPoolSize could be sufficient or if eviction control is the only viable solution.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.