skip to content

Drivers & Connection Pools

Official drivers share specifications for connection pools, server selection, monitoring and retries. Interviewers ask about pool exhaustion and what an application sees during a failover.

part ofMongoDBread it →
on this pageshow

explore

questions

6

With MongoDB driver defaults, how many connections can 40 application instances open to a three-member replica set, and which settings bound that?

level: middleimportance: must knowfreq 55%

answer

  1. one pool per server
  2. multiply three numbers
  3. 100 is per member
  4. monitors sit outside the pool
  5. a full pool waits, by default forever

basics

~20 s

With one client per instance, the ceiling is instances x maxPoolSize x members: 40 x 100 x 3 = 12,000, or 4,000 per member, because each client keeps one pool per server. Monitoring sockets come on top.

solid answer

~40 s

A `MongoClient` keeps one connection pool per server, so `maxPoolSize`, which the pooling specification defaults to 100, caps each member separately, not the client. With one client in each of 40 instances, that allows 40 x 100 = 4,000 connections per member and 12,000 across three members. That is a ceiling: `minPoolSize` defaults to 0, so a member that serves no operations can hold none from the pool. Under the streaming protocol, monitoring adds two dedicated sockets per client per member, outside the pool. `maxConnecting`, default 2, limits how many connections a pool establishes at once, so requests can queue well below the cap. `waitQueueTimeoutMS` means no limit by default in the specification, PyMongo 4.19 and Node.js driver 7.8, so a full pool makes operations wait instead of failing.

code

plaintext · 1 line
plaintext
mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=rs0&maxPoolSize=50&minPoolSize=5

go deeper

for a junior

Remember that a driver keeps a connection pool for each server, and that maxPoolSize defaults to 100 in the specification. Then multiply: instances times pool size times members gives the most you could see.

for a middle

Separate the settings: maxPoolSize caps a pool, minPoolSize sets its floor, maxConnecting limits concurrent establishment, waitQueueTimeoutMS limits waiting. State that the sum is a ceiling and that monitoring sockets sit outside it.

for a senior

Connect the sum to production: compare the per-member ceiling across all services with net.maxIncomingConnections and with connections.current, and notice that a wait with no limit reads as latency rather than as an error.

for a principal

Treat connections as a budget shared by every service on the cluster. Autoscaling changes one factor of the product without anyone touching configuration, so decide how the budget is allocated and who reviews it.

## One client, one pool per server A **connection pool** is a set of open, authenticated connections that a driver reuses so that each operation need not pay for TCP, TLS and login. The connection monitoring and pooling specification, known as **CMAP**, gives every official driver the same model. A `MongoClient` creates one pool for each data-bearing server it discovers, and a pool belongs to exactly one server. All pools in a client use the same options. So a pool option is a per-server value, multiplied by however many servers and however many clients exist. ## The arithmetic The ceiling for a fleet is **instances x clients per instance x `maxPoolSize` x members**: ```plaintext 40 instances x 100 maxPoolSize = 4,000 connections per member, at most 4,000 x 3 members = 12,000 across the replica set, at most 40 instances x 2 monitoring sockets = 80 more per member, outside the pool ``` This is a limit, not a forecast. Connections are created on demand. If every operation goes to the primary and `minPoolSize` is 0, the secondaries' pools can stay empty while the primary's pool grows towards 4,000. The floor multiplies the same way: `minPoolSize=10` keeps 40 x 10 = 400 connections open on every data-bearing member, 1,200 in all, whether or not that member gets traffic. The specification accepts this deliberately, because warm pools on secondaries offset the rush of new connections when a primary steps down. ## The settings and whose defaults they are | Setting | Limits | CMAP default | PyMongo 4.19 | Node.js driver 7.8 | |---|---|---|---|---| | `maxPoolSize` | connections per pool; 0 means no limit | 100 | 100 | 100 | | `minPoolSize` | connections kept per pool | 0 | 0 | 0 | | `maxConnecting` | connections being established at once, per pool | 2 | 2 | 2 | | `waitQueueTimeoutMS` | time an operation may wait for a connection | 0, no limit | `None`, no limit | 0, no limit | | `maxIdleTimeMS` | idle time before a connection is retired | 0, no limit | `None`, no limit | 0, no limit | `waitQueueTimeoutMS` is deprecated by the specification in favour of `timeoutMS`, but it remains the working setting wherever `timeoutMS` is not set. ## What counts, and what does not - **Counted against `maxPoolSize`:** in-use and available connections, plus those still being established. - **Not counted:** monitoring sockets. With the streaming protocol a client holds one monitoring connection and one round-trip-time connection to each server. In polling mode there is one, without the extra connection. These are dedicated and do not authenticate. - **Every extra client multiplies the sum.** A process that builds several `MongoClient` objects has several sets of pools and monitors. - **Processes count, not machines.** A pool is not fork-safe, so four worker processes on one host are four clients, each created after the fork. ## What happens when a pool is full 1. An operation asks the pool for a connection and joins a first-come, first-served **wait queue**. 2. If an idle connection exists and is still valid, the operation gets it. 3. Otherwise, if the pool is below `maxPoolSize` and fewer than `maxConnecting` connections are being established, the pool opens a new one. 4. Otherwise the operation waits. The wait ends when a connection comes free, when the pool can open another, when the pool is cleared, or when a configured timeout expires. Expiry raises `WaitQueueTimeoutError`, which the Node.js driver names `MongoWaitQueueTimeoutError`. By default no such timeout exists, so a saturated pool appears as latency, not as an error. Note the second gate in step 4: with `maxConnecting` at 2, operations can queue behind a pool holding far fewer than 100 connections. ## The server's side of the sum Each connection costs the server memory and a file descriptor. `net.maxIncomingConnections` sets how many it accepts: the 9.0 source sets a default of 1,000,000, and on Linux the value is capped at `(RLIMIT_NOFILE / 2) * 0.8`, a cap documented since 8.1, 8.0.16 and 7.0.31. Compare your fleet's per-member ceiling with that number, and add every other service that uses the same replica set. `connections.current` in `serverStatus` shows what a member actually holds.

  • What goes wrong if an application creates a new MongoClient for each request?
    Each client brings its own pools and monitors, and has to discover the topology before serving anything. Connections are not reused between requests, so every request pays for TCP, TLS and authentication, and the server sees heavy connection churn. Fleet arithmetic also stops meaning anything. Keep one long-lived client per process and share it.
  • Why might you raise minPoolSize above its default of 0, and what does it cost?
    A warm pool lets a member serve a burst without establishing connections first, which matters most when a secondary is promoted. The cost is a constant baseline: `minPoolSize` applies to every data-bearing member's pool in every client, so 40 instances at 10 hold 1,200 connections even when idle.
  • Why does maxConnecting exist when maxPoolSize already caps the pool?
    `maxPoolSize` limits how many connections exist; `maxConnecting` limits how fast they appear. Establishing one involves TCP, TLS and authentication, so an unbounded burst can hurt a member that is already busy. The manual warns of the tradeoff: raising it connects faster but risks connection storms, while too low a value adds tail latency at checkout.

  • maxPoolSize is a limit on the whole client, shared across all replica set members.
  • With default settings each instance always holds 300 open connections to a three-member set.
  • A full pool returns an error immediately by default, so the application fails fast.
  • Monitoring connections are borrowed from the pool and count towards maxPoolSize.
  • maxConnecting caps the total number of connections a pool may keep open.
  • Raising maxPoolSize is always safe, because idle connections cost the server nothing.

Assumes MongoDB 9.0 for net.maxIncomingConnections, capped on Linux since 8.1, 8.0.16 and 7.0.31; pool defaults are the CMAP specification's, checked in PyMongo 4.19 and Node.js driver 7.8.

open as a page

During a replica set failover, a MongoDB service with 40 application instances runs out of connections and users see timeouts: what happens inside the driver, and how do you diagnose it?

level: seniorimportance: must knowfreq 52%

basics

~20 s

During a MongoDB failover, requests block in server selection until a new primary is known, then every instance hits an often cold pool limited by maxConnecting, and queue waits with no default limit outlast the application's request timeouts.

open as a page

In a MongoDB connection string, what does the mongodb+srv:// scheme add over a plain mongodb:// seed list?

level: juniorimportance: should knowfreq 38%

basics

~20 s

A mongodb+srv:// string names one DNS hostname instead of listing hosts: the driver reads SRV records for the seed list and a TXT record for a few default options, and it turns TLS on by default.

open as a page

Which reads does a MongoDB driver's retryReads retry after a failover, how many times, and what still reaches the application?

level: middleimportance: should knowfreq 41%

basics

~20 s

With retryReads, on by default, a MongoDB driver retries a supported read once after a retryable error, such as a network error or a not-primary reply. getMore, reads in a transaction and generic runCommand stay outside that policy.

open as a page

How does a MongoDB driver pick one server among eligible members, and what do localThresholdMS and serverSelectionTimeoutMS control?

level: middleimportance: should knowfreq 44%

basics

~20 s

A multi-threaded MongoDB driver filters the topology to suitable servers, keeps those within localThresholdMS of the fastest, then picks two at random and uses the one with fewer operations. If none is suitable it waits up to serverSelectionTimeoutMS.

open as a page

How would you set MongoDB driver pool and timeout defaults across many services, and who should own them?

level: principalimportance: should knowfreq 36%

basics

~20 s

Set MongoDB driver defaults centrally from each member's connection budget and each workload's request deadline, publish a template per driver because defaults and timeoutMS support differ, let a platform owner review exceptions, and test the result with failover drills.

open as a page