skip to content

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

level: principalimportance: should knowfreq 36%

answer

  1. a shared connection budget
  2. defaults are not a policy
  3. deadlines come from the request
  4. one option, two drivers, two behaviours
  5. prove it with a drill

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.

solid answer

~40 s

I would treat driver settings as a shared contract: a platform or database team owns the template, and services override it by exception. Start from capacity, summing instances x `maxPoolSize` per member across all services against each member's limit and what it can serve. Then derive timeouts from request deadlines, because the defaults leave checkout unbounded and `serverSelectionTimeoutMS` at 30 seconds, which outlasts a typical web request. Where a driver honours it, `timeoutMS` sets one deadline across selection, checkout, execution and retries and replaces `socketTimeoutMS`, `waitQueueTimeoutMS` and `maxTimeMS`. But the Node.js driver 7.8 made it a no-op, so a mixed fleet needs per-driver templates. Defaults also differ: `connectTimeoutMS` is 20 seconds in PyMongo and 30 in Node. Keep separate profiles for web and batch work, and prove them with failover drills.

code

plaintext · 5 lines
plaintext
# Web profile, PyMongo services (timeoutMS honoured)
mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=rs0&maxPoolSize=30&minPoolSize=5&timeoutMS=3000&serverSelectionTimeoutMS=1000

# Web profile, Node.js driver 7.8 services (timeoutMS has no effect)
mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=rs0&maxPoolSize=30&minPoolSize=5&waitQueueTimeoutMS=500&serverSelectionTimeoutMS=1000&socketTimeoutMS=1500

go deeper

for a junior

Recall that pool and timeout settings have defaults that come from the driver specifications, and that those defaults are not tuned for your application. Know that some of them differ from one driver to another.

for a middle

Calculate a fleet's connection ceiling and map timeouts onto a request: selection, checkout, socket. Explain what timeoutMS replaces and which older options remain in force under it.

for a senior

Show that you verify rather than assume: read driver source or changelogs for defaults, notice that Node's timeoutMS is a no-op in 7.8, and confirm behaviour with a planned step-down under load.

for a principal

Argue ownership and enforcement. Connections are a shared budget and deadlines belong to services, so decide who holds each, how exceptions get approved, and how many profiles are worth maintaining against the cost of unusual workloads fitting badly.

## What is actually being decided No single set of numbers is right. The decision is who owns a resource that every service consumes and nobody sees whole: the connections on each replica set member. Pool options live in application configuration, so each team sets them locally, while the cost lands on a shared server. A lead has to settle how that budget is divided, what a request may spend waiting, and who can change either. ## Why driver defaults are not a policy Specification defaults are written to be safe for an unknown application, not to fit your request deadline. | Setting | Specification | PyMongo 4.19 | Node.js driver 7.8 | |---|---|---|---| | `maxPoolSize` | 100 | 100 | 100 | | `maxConnecting` | 2 | 2 | 2 | | `waitQueueTimeoutMS` | 0, no limit | `None`, no limit | 0, no limit | | `serverSelectionTimeoutMS` | 30,000 ms | 30 s | 30,000 ms | | `connectTimeoutMS` | 10,000 ms, unless a driver already differs | 20 s | 30,000 ms | | `socketTimeoutMS` | no timeout | `None`, no timeout | 0, no timeout | | `timeoutMS` | unset | unset, honoured | no effect | Two lessons follow. An unset checkout and socket timeout means a request can wait far longer than any user will, and identical connection strings do not behave identically across drivers. The manual itself warns that `timeoutMS` is not supported by all official drivers. ## `timeoutMS` against the older options The client-side operations timeout specification defines **`timeoutMS`** as one limit on a whole operation, set at client, database, collection or operation level. - When set, it replaces `socketTimeoutMS`, `waitQueueTimeoutMS`, `wTimeoutMS` and `maxTimeMS`, which the specification deprecates; a driver ignores them. - Selection and checkout share `min(serverSelectionTimeoutMS, remaining timeoutMS)`. The time left is passed to the server as `maxTimeMS`, less the minimum round-trip time. - Retries continue until the deadline, instead of stopping at one. - `serverSelectionTimeoutMS` and `connectTimeoutMS` are deliberately not deprecated, because finding a server and opening a socket need a limit even when an operation does not. - `0` means infinite, and once set at a level the value cannot be changed back to unset. The catch is at the driver level. PyMongo 4.19 honours it. The **Node.js driver 7.8.0 made `timeoutMS` a no-op and deprecated it**, so there the older options are what actually run. A standard written from the specification alone would leave those services with no effective deadline. ## Deriving the numbers 1. **Count connections.** Per member, add up instances x `maxPoolSize` for every service, using peak autoscaled instance counts. Add two monitoring sockets per client per member. Compare with `net.maxIncomingConnections` and with what the member can serve in practice. 2. **Size pools from concurrency,** which is roughly the operation rate multiplied by operation latency per instance, plus headroom. A pool far above that only raises the worst case. 3. **Set the floor.** `minPoolSize` warms secondaries for failover, and it costs instances x floor x members at all times. 4. **Split the deadline.** From a 3-second request budget, decide how much selection and checkout may take, for example 1 second and half a second, and leave the rest for execution. 5. **Test it.** Run a planned step-down under load and check that errors, latency and connection counts match what the numbers predicted. ## Who owns what - **A platform or database team** owns the template per driver, the per-cluster connection budget and the upgrade review. - **Service teams** own their request deadlines and propose exceptions with a reason and a number. - **Enforcement** lives in a shared configuration library or a central connection string, not in a wiki page. Otherwise the defaults return with each new service. - **Monitoring** alerts on checkout failures and on `connections.current` against budget, and each driver upgrade gets its changelog read, because a change such as Node's can remove a limit silently. ## The tradeoffs to argue | Choice | Gain | Cost | |---|---|---| | Short `serverSelectionTimeoutMS` | fails fast, frees threads | errors during a normal election | | Long `serverSelectionTimeoutMS` | rides out an election | requests pile up behind it | | High `minPoolSize` | warm failover | constant connection load | | One central template | consistency, simple review | ill fit for unusual workloads | | Separate web and batch profiles | each gets a sensible deadline | more to maintain and explain | Web requests and overnight jobs rarely want the same numbers, so two profiles is a common compromise. Past that, each extra profile is a cost to justify.

  • How does autoscaling change the connection budget?
    It changes one factor of the product without any configuration change. If a service scales from 40 to 120 instances at `maxPoolSize=100`, its per-member ceiling goes from 4,000 to 12,000. So budget against the autoscaler's maximum, not the usual count, and lower the pool size as you raise the instance limit.
  • Would you set serverSelectionTimeoutMS below the time a typical election takes?
    For a user-facing path, often yes, if a fast error can be handled by a fallback or a clear message, since that user is gone after a few seconds anyway. For batch work, no, because waiting is cheap and a failure is expensive. That is the main reason to have two profiles.
  • How would you find out whether the standard is actually in use?
    Compare what services really run with the template: review their configuration and driver versions, and have each set `appName`, which appears in `currentOp` output, so that load can be attributed to a service. Then run a failover drill: the services that misbehave show where the standard is not being followed.

  • timeoutMS behaves the same in every official driver, so one connection string fits the whole fleet.
  • All drivers share the specification's defaults, so a per-driver template is unnecessary.
  • Pool size is each team's private concern, because pools live inside the application.
  • With timeoutMS set you can drop serverSelectionTimeoutMS and connectTimeoutMS too, as both are deprecated.
  • A large default maxPoolSize for every service is a cheap and safe choice.

Assumes PyMongo 4.19 and the Node.js driver 7.8 against MongoDB 9.0: timeoutMS is honoured in the first and a no-op since 7.8.0 in the second, and their connectTimeoutMS defaults differ, so re-read both on a driver upgrade.