a map of backend systems
◀ Back to the map

MongoDB line

Read preferences

Where you read from is a throughput decision, a consistency decision, and a latency decision simultaneously. The five modes give you the dial.


MongoDB deep-dive · Part 5 of 11. Previous: Replica sets & elections. Next: Aggregation pipeline.

A MongoDB replica set has one primary and one or more secondaries. Writes must go to the primary. Reads can go anywhere — that’s where the choice begins. Your read preference tells the driver which node(s) are acceptable for a given read operation.

The choice is not purely about performance. It’s also about consistency: a secondary may lag the primary by anywhere from milliseconds to seconds (or more). Reading from a secondary can return data that the primary has already superseded. Whether that matters depends entirely on what the read is for.

The five modes

primary (default)

All reads go to the primary. The only mode that guarantees you’ll see the latest committed writes. No stale reads possible, but no scale-out for reads either.

from pymongo import MongoClient, ReadPreference

client = MongoClient(
    "mongodb://host1,host2,host3/?replicaSet=rs0",
    readPreference="primary"  # default
)

Use for: any read where staleness is unacceptable — e.g., reading a record immediately after writing it, reading data that drives a downstream write.

primaryPreferred

Reads go to the primary when it’s available; fall back to a secondary if the primary is unreachable. This is the “best effort primary” mode.

Use for: read-heavy workloads where you want primary consistency normally but don’t want reads to fail during a failover window.

secondary

All reads go to a secondary (chosen according to driver tag sets or latency window). Never reads from the primary.

Use for: analytics, reporting, batch jobs — workloads that can tolerate stale data and that you want to offload from the primary.

secondaryPreferred

Reads go to a secondary when any secondary is available; falls back to the primary when no secondaries are reachable.

Use for: the most common scale-out pattern for read-heavy applications. You accept mild staleness in exchange for distributing read load.

nearest

Reads go to the member with the lowest network latency, regardless of whether it’s a primary or secondary. The driver measures round-trip times in its server monitoring loop and routes to the fastest node.

Use for: globally distributed deployments where latency is the primary concern and you’re willing to accept reads from any member.

maxStalenessSeconds

When using secondary, secondaryPreferred, or nearest, you can set a bound on how stale the secondary’s data may be:

from pymongo.read_preferences import Secondary

col = client.mydb.events.with_options(
    read_preference=Secondary(max_staleness=120)  # seconds
)

The driver will exclude any secondary whose estimated lag from the primary exceeds maxStalenessSeconds. If all secondaries exceed the bound, the operation falls back to the primary (for secondaryPreferred/nearest) or raises an error (for secondary).

Tag sets

Tag sets let you route reads to specific secondaries based on custom metadata. You configure tags in rs.conf():

// In the mongo shell, label one secondary as "analytics"
cfg = rs.conf()
cfg.members[1].tags = { "purpose": "analytics" }
rs.reconfig(cfg)

Then route specific operations to it:

from pymongo.read_preferences import Secondary

analytics_col = client.mydb.events.with_options(
    read_preference=Secondary(tag_sets=[{"purpose": "analytics"}])
)

# This read goes only to the "analytics"-tagged secondary
cursor = analytics_col.find({"ts": {"$gte": t_start}})

Tag sets are powerful for multi-region topologies: tag secondaries by region and route application reads to the local region’s secondary, with a fallback to the primary if the local secondary is unavailable.

Choosing the right mode: a decision matrix

ScenarioRecommended modeReason
Read after write (same user session)primaryNo stale reads possible
High-read-throughput CRUD appsecondaryPreferredDistribute load; mild staleness OK
Analytics / reportingsecondaryOffload primary completely
Multi-region, latency-sensitivenearest with tag setsRoute by geography; staleness tolerable
Failover resilience for readsprimaryPreferredPrimary when healthy, secondary on failover

Combining read preference with sessions (RYOW)

MongoDB provides read-your-own-writes (RYOW) consistency automatically when using a causal consistency session with primary or primaryPreferred read preference:

with client.start_session() as session:
    session.start_transaction()
    # writes here...
    # reads in the same session on primary will see those writes

For non-transaction use cases, the simplest guarantee of read-your-own-writes is primary read preference, full stop. If you need to read from a secondary after a write in the same workflow, you need either a causal session or an explicit wait for replication — there’s no shortcut.

secondaryPreferred buys read throughput. nearest buys latency. Neither buys consistency. When you write and then read in the same user flow, use primary or primaryPreferred — anything else risks showing the user a stale world.

Recall · check yourself

Before you go

Answer from memory first, then reveal. These cards also live in the review queue — come back in a few days.

  1. R01 Why does nearest break read-your-own-writes?

    The latency-closest node may be a lagging secondary: the user writes to the primary, immediately reads from that secondary, and their save appears to vanish. Route post-write reads to primary.

  2. R02 Why can't maxStalenessSeconds go below 90?

    Staleness is estimated from 10-second heartbeats — too imprecise to enforce less. Need tighter freshness? Use primary, not a small maxStaleness.

  3. R03 What three decisions does a read preference make at once?

    Throughput (which nodes share read load), consistency (how stale data can be), and latency (how far the read travels). The five modes are one dial over all three.

  4. R04 When is secondaryPreferred a sound choice?

    Offloading staleness-tolerant read traffic — analytics, dashboards, feeds — while falling back to the primary when no secondary is available.