0
0
MongoDBquery~10 mins

Read preference for replica sets in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Read preference for replica sets
Client issues read request
Check read preference setting
Primary
Send read to chosen replica
Receive data from replica
Return data to client
The client decides which replica to read from based on the read preference, then sends the read request to that replica and returns the data.
Execution Sample
MongoDB
db.collection.find().readPref('secondary')
This query reads data from a secondary replica in the replica set.
Execution Table
StepRead PreferenceReplica ChosenActionResult
1secondarySecondary replicaSend read requestRead data from secondary
2primaryPrimary replicaSend read requestRead data from primary
3primaryPreferredPrimary replica if availableSend read requestRead data from primary
4primaryPreferredSecondary replica if primary downSend read requestRead data from secondary
5secondaryPreferredSecondary replica if availableSend read requestRead data from secondary
6secondaryPreferredPrimary replica if no secondarySend read requestRead data from primary
7nearestReplica with lowest latencySend read requestRead data from nearest replica
8---Stop: Read completed and data returned
💡 Read completes after sending request to chosen replica and receiving data
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
readPreferencenot setsecondaryprimaryprimaryPreferredsecondaryPreferrednearest
replicaChosennonesecondaryprimaryprimarysecondarynearest
readResultnonedata from secondarydata from primarydata from primary or secondarydata from secondary or primarydata from nearest replica
Key Moments - 3 Insights
Why does 'primaryPreferred' sometimes read from secondary?
Because if the primary is down or unreachable, 'primaryPreferred' falls back to reading from a secondary, as shown in execution_table rows 3 and 4.
What happens if no secondary is available but read preference is 'secondaryPreferred'?
The read goes to the primary replica instead, as shown in execution_table row 6.
How does 'nearest' read preference decide which replica to read from?
'Nearest' chooses the replica with the lowest network latency, regardless of primary or secondary status, as shown in execution_table row 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which replica does 'secondaryPreferred' read from if a secondary is available?
APrimary replica
BSecondary replica
CNearest replica
DNo replica
💡 Hint
Check execution_table row 5 under 'Replica Chosen'
At which step does the read preference fall back to secondary if primary is down?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look at execution_table rows 3 and 4 for 'primaryPreferred' behavior
If the read preference is 'nearest', which replica is chosen?
AReplica with lowest latency
BSecondary replica only
CPrimary replica only
DRandom replica
💡 Hint
See execution_table row 7 for 'nearest' read preference
Concept Snapshot
Read preference controls which replica in a MongoDB replica set handles read queries.
Options include primary, secondary, primaryPreferred, secondaryPreferred, and nearest.
The client sends reads to the chosen replica based on preference and availability.
Fallbacks occur if preferred replicas are unavailable.
This helps balance load and improve read scalability.
Full Transcript
This visual execution trace shows how MongoDB clients decide which replica to read from in a replica set based on the read preference setting. The client first checks the read preference, then selects the appropriate replica: primary, secondary, or nearest. For example, 'secondaryPreferred' reads from a secondary if available, otherwise falls back to primary. The execution table steps through each preference and the replica chosen, showing the read request sent and data returned. Variable tracking shows how the readPreference and replicaChosen change at each step. Key moments clarify common confusions like fallback behavior. The quiz tests understanding of which replica is chosen under different preferences. The snapshot summarizes the concept simply for quick review.