How do MongoDB's five readPreference modes differ in which members they route reads to?
answer
- five names, two of them end in 'preferred'
- one mode does not care about role at all
- latency window, not the single closest member
- only one mode refuses tag sets
basics
~10 sMongoDB offers primary (default, primary only), primaryPreferred (primary, else secondaries), secondary (secondaries only), secondaryPreferred (secondaries, else primary), and nearest (whichever eligible member has lowest latency, primary or secondary).
solid answer
~40 sThere are five modes. `primary` is the default and reads only from the primary, erroring if there is none — during an election, for example. `secondary` reads only from secondaries and errors if none are eligible. The two "preferred" modes are fallbacks: `primaryPreferred` uses the primary but falls back to secondaries when there is no primary, and `secondaryPreferred` uses secondaries but falls back to the primary when none qualify. `nearest` ignores the role entirely and picks among all eligible members by lowest observed network latency, within a small latency window. Every mode except `primary` can return data older than the primary's latest, because secondaries apply the oplog asynchronously. All modes except `primary` also accept tag sets, to pin reads to members carrying a given tag, and `maxStalenessSeconds`, to exclude members lagging beyond a bound.
code
javascript · 4 linesdb.metrics.find({ day: "2026-08-19" }).readPref(
"secondaryPreferred",
[ { region: "eu" }, {} ] // prefer an EU member, else any
)go deeper
Know the five mode names and that only primary guarantees you are talking to the leader; everything else may hand back older data.
Explain the selection pipeline — mode, then staleness bound, then tag sets, then the latency window — and why the two 'preferred' modes exist as fallbacks.
Show judgment about which workloads may leave the primary, why a global connection-string setting is a blunt instrument, and how mode choice changes behaviour during an election rather than only in steady state.
Own the routing policy across services: which read classes are allowed off the primary, how tags encode topology intent, and whether stale-read incidents should be solved by mode changes or by capacity and sharding decisions.
## What read preference selects Read preference tells the driver **which replica set member a read may be sent to**. It is purely about member selection; how fresh or how durable the returned data must be is controlled separately by read concern. In a sharded cluster the same preference is honoured by the router when it fans out to each shard's replica set. ## The five modes **`primary`** — the default. Reads go to the primary only. If there is no primary (mid-election, or a set that cannot form a majority), the read **errors** rather than falling back. This mode gives the freshest data and, in the absence of a rollback, lets you read your own writes without extra machinery. **`primaryPreferred`** — the primary when one exists, otherwise an eligible secondary. Useful when you want fresh reads normally but would rather serve slightly stale data than fail during a failover window. **`secondary`** — eligible secondaries only, never the primary; errors if none qualifies. Used to keep a workload strictly off the primary, for example a reporting or export job. **`secondaryPreferred`** — eligible secondaries when available, the primary as a fallback. This is the common choice for read-heavy analytical traffic that must not fail if secondaries are unavailable. **`nearest`** — role-blind. The driver considers **all** eligible members, primary included, and chooses on measured network latency. This is the mode for latency-sensitive geo-distributed reads where you care about proximity, not about which member is the leader. ## How the driver actually picks Drivers continuously monitor each member with heartbeats and keep a round-trip-time estimate per member. Selection proceeds in stages: filter by mode (which roles are allowed), then by `maxStalenessSeconds` if given, then by tag sets if given, and finally choose among the survivors within a **latency window** — the fastest eligible member's round-trip time plus a configurable threshold (`localThresholdMS`, 15 ms by default), with a random pick inside that window to spread load. This is why `nearest` does not mean "strictly the single closest member" but "one of the members that are about as close as the closest". ## Tag sets Members can carry arbitrary key/value tags in the replica set configuration — `{ region: "eu", use: "reporting" }`, for example. A read preference can then supply an ordered list of tag sets; the driver tries the first set, and only if no member matches does it move to the next. This is how you pin analytics traffic to a dedicated member, or keep reads inside a region. Tag sets cannot be combined with the `primary` mode, since there is only ever one primary and no choice to make. ## `maxStalenessSeconds` Because secondaries apply the oplog asynchronously, a badly lagging member is still "eligible" under a plain `secondaryPreferred`. `maxStalenessSeconds` sets an upper bound: members estimated to be further behind than that value are excluded from selection. The value has a floor — it must be at least 90 seconds — because the estimate is derived from heartbeats and periodic no-op writes, and a tighter bound could not be computed reliably. Like tag sets, it cannot be used with the `primary` mode. Note what it is not: a freshness guarantee, only a coarse exclusion of badly lagging members. ## What secondary reads cost you Three things routinely surprise teams. First, **staleness**: any non-primary read may return an older view, so the write you just made may be invisible. Fixing that properly needs causally consistent sessions or a primary read, not a read preference tweak. Second, **secondaries do the same write work**: every secondary applies the full oplog. Offloading reads relieves the primary's read load but adds nothing to write capacity, so "add a secondary to scale" only helps read-bound workloads. Third, **failure behaviour differs by mode**: `primary` fails closed during an election, while `secondaryPreferred` keeps serving — which is a feature for a dashboard and a hazard for a balance check. ## Where to set it Read preference can be set on the connection string (`readPreference=secondaryPreferred&maxStalenessSeconds=120`), on a client, database or collection handle, or on the individual operation, with the most specific level winning. Setting it globally on the connection string is convenient but blunt: it silently applies to reads that genuinely need the primary, which is why per-operation overrides on the sensitive paths are the safer pattern.
- Why does maxStalenessSeconds have a minimum value rather than accepting any number?The driver estimates a member's staleness from heartbeat responses and the periodic no-op writes the primary makes when idle, so the estimate is only meaningful over a window of tens of seconds. MongoDB requires at least 90 seconds because a tighter bound could not be computed reliably and would exclude healthy members at random.
- What happens to a read with readPreference primary during an election?It fails. There is no primary to select, so the driver waits within its server-selection timeout and then raises an error rather than falling back to a secondary. If you would rather serve slightly stale data than fail through a failover window, `primaryPreferred` is the mode that expresses that.
- Does routing reads to secondaries increase the cluster's write capacity?No. Every secondary applies the entire oplog, so the write workload is replicated in full on each member. Secondary reads move read load off the primary, which helps a read-bound workload, but a write-bound cluster needs sharding, not more secondaries.
saying these in an interview costs you the question
- Thinks nearest means the geographically closest member only
- Believes secondary reads add write capacity to the cluster
- Assumes maxStalenessSeconds guarantees data freshness
- Sets secondaryPreferred globally, including on read-your-write paths
- Expects primary mode to fall back to a secondary during an election