What does reading from secondaries in MongoDB mean?
It means querying data from secondary replica set members instead of the primary, which can reduce load on the primary and improve read scalability.
Click to reveal answer
beginner
What is a main trade-off when reading from secondaries?
You might get stale data because secondaries replicate changes asynchronously and can lag behind the primary.
Click to reveal answer
intermediate
How does reading from secondaries affect consistency?
It can reduce consistency guarantees because the data on secondaries may not reflect the most recent writes made to the primary.
Click to reveal answer
intermediate
Why might an application choose to read from secondaries despite potential stale data?
To improve read throughput and reduce latency by distributing read load, especially for read-heavy workloads.
Click to reveal answer
advanced
What is a common strategy to minimize stale reads when reading from secondaries?
Use read preferences like 'secondaryPreferred' with a maxStalenessSeconds setting to limit how stale the data can be.
Click to reveal answer
What is a potential downside of reading from MongoDB secondaries?
AData might be outdated due to replication lag
BPrimary server load increases
CWrites become slower
DSecondaries cannot serve reads
✗ Incorrect
Secondaries replicate asynchronously, so data can be slightly behind the primary.
Which read preference allows reading from secondaries but falls back to primary if no secondaries are available?
Aprimary
Bsecondary
CsecondaryPreferred
Dnearest
✗ Incorrect
'secondaryPreferred' reads from secondaries if possible, otherwise from primary.
How can you limit how stale data can be when reading from secondaries?
ADisable journaling
BUse write concern majority
CIncrease primary election timeout
DSet maxStalenessSeconds in read preference
✗ Incorrect
maxStalenessSeconds restricts how old the data on secondaries can be for reads.
Why might an application prefer reading from the primary over secondaries?
ATo increase read scalability
BTo get the most up-to-date data
CTo reduce primary load
DTo avoid replication lag
✗ Incorrect
Primary always has the latest data, so reading from it ensures strong consistency.
Which scenario benefits most from reading from secondaries?
ARead-heavy workloads with tolerance for slightly stale data
BSingle server deployments
CApplications requiring strict consistency
DWrite-heavy workloads
✗ Incorrect
Reading from secondaries improves read throughput but may return slightly stale data.
Explain the trade-offs of reading from secondaries in MongoDB replica sets.
Think about what happens when data is copied asynchronously.
You got /4 concepts.
Describe strategies to reduce stale reads when using secondary reads in MongoDB.
Consider configuration options that control how fresh the data must be.
You got /4 concepts.
Practice
(1/5)
1. What is a main advantage of reading data from secondary nodes in a MongoDB replica set?
easy
A. It helps balance the read load and can improve read speed.
B. It guarantees the freshest data with zero delay.
C. It prevents any replication lag from occurring.
D. It disables writes to the primary node.
Solution
Step 1: Understand the role of secondaries in MongoDB
Secondary nodes replicate data from the primary and can serve read requests to reduce load on the primary.
Step 2: Identify the benefit of reading from secondaries
Reading from secondaries balances read load and can improve read speed, but data might be slightly stale.
Final Answer:
It helps balance the read load and can improve read speed. -> Option A
Quick Check:
Reading from secondaries = load balancing and faster reads [OK]
Hint: Secondaries help with read speed but may lag [OK]
Common Mistakes:
Thinking secondaries always have the freshest data
Assuming secondaries prevent replication lag
Believing reading from secondaries disables writes
2. Which of the following is the correct way to set a read preference to read from secondary nodes in MongoDB using the shell?
easy
A. db.collection.find().readPreference('secondary')
B. db.collection.find().setReadPref('primary')
C. db.collection.find().readPreference('primaryPreferred')
D. db.collection.find().readPreference('secondaryPreferred')
Solution
Step 1: Recall MongoDB read preference options
MongoDB supports 'primary', 'primaryPreferred', 'secondary', and 'secondaryPreferred' as read preferences.
Step 2: Identify correct syntax for reading from secondaries
To read from secondaries but allow fallback to primary, use 'secondaryPreferred'. The method is .readPreference('secondaryPreferred').
Final Answer:
db.collection.find().readPreference('secondaryPreferred') -> Option D
Quick Check:
Use .readPreference('secondaryPreferred') to read from secondaries [OK]
Hint: Use 'secondaryPreferred' to read from secondaries safely [OK]
Common Mistakes:
Using 'secondary' which fails if no secondary is available
Confusing 'primaryPreferred' with reading from secondaries
Using incorrect method names like setReadPref
3. Consider a MongoDB replica set with some replication delay. If you run a query with read preference set to secondary, what is the most likely outcome?
medium
A. You get the freshest data always from the primary.
B. You might get slightly stale data due to replication lag.
C. The query will fail if the primary is down.
D. The query will always return an error if secondaries exist.
Solution
Step 1: Understand replication lag in secondaries
Secondaries replicate data asynchronously, so there can be a delay causing stale data.
Step 2: Analyze read preference 'secondary'
Reading from secondaries means data may not be the freshest due to replication lag.
Final Answer:
You might get slightly stale data due to replication lag. -> Option B
Quick Check:
Reading from secondaries = possible stale data [OK]
Hint: Secondary reads can be stale due to lag [OK]
Common Mistakes:
Assuming secondary reads always return fresh data
Thinking queries fail if primary is down when reading from secondary
Believing reading from secondaries causes errors if secondaries exist
4. You set your MongoDB client to read from secondaries using readPreference: 'secondary', but your application sometimes gets errors when no secondaries are available. How can you fix this?
medium
A. Remove read preference to always read from primary.
B. Set read preference to 'primary' to avoid errors.
C. Change read preference to 'secondaryPreferred' to allow fallback to primary.
D. Restart the MongoDB server to fix the errors.
Solution
Step 1: Identify cause of errors
Using 'secondary' read preference causes errors if no secondary nodes are available.
Step 2: Choose a read preference that allows fallback
'secondaryPreferred' reads from secondaries if available, else falls back to primary, preventing errors.
Final Answer:
Change read preference to 'secondaryPreferred' to allow fallback to primary. -> Option C
Quick Check:
Use 'secondaryPreferred' to avoid errors when no secondaries [OK]
Hint: Use 'secondaryPreferred' to avoid errors if no secondaries [OK]
Common Mistakes:
Switching to 'primary' loses read load balancing benefits
Removing read preference may not fix fallback issues
Restarting server does not solve read preference errors
5. Your application requires fast reads but can tolerate slightly stale data. You want to minimize load on the primary node. Which read preference should you choose and why?
hard
A. Use 'secondaryPreferred' to read from secondaries when available, else primary, balancing load and freshness.
B. Use 'secondary' to read only from secondaries, balancing load but risking stale data.
C. Use 'primaryPreferred' to always read from primary regardless of load.
D. Use 'primary' to ensure freshest data and reduce load on secondaries.
Solution
Step 1: Analyze application needs
The app wants fast reads, can accept some stale data, and wants to reduce primary load.
Step 2: Evaluate read preference options
'secondary' risks errors if no secondaries; 'secondaryPreferred' reads from secondaries if available, else primary, balancing load and freshness.
Step 3: Choose best option
'secondaryPreferred' fits best as it balances load and freshness with fallback.
Final Answer:
Use 'secondaryPreferred' to read from secondaries when available, else primary, balancing load and freshness. -> Option A
Quick Check:
'secondaryPreferred' balances load and freshness [OK]
Hint: Choose 'secondaryPreferred' for load balance and fallback [OK]
Common Mistakes:
Choosing 'primary' ignores load balancing
Choosing 'secondary' risks errors if no secondaries