Bird
Raised Fist0
MongoDBquery~20 mins

Read from secondaries trade-offs in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Secondary Read Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Read Preference Impact
In MongoDB, what is a primary trade-off when configuring an application to read from secondary replicas instead of the primary?
ASecondary nodes cannot serve any read requests.
BWrites become slower because secondaries handle write operations.
CThe primary node becomes unavailable for reads.
DReads may return stale data due to replication lag.
Attempts:
2 left
💡 Hint
Think about how data replication timing affects read freshness.
❓ query_result
intermediate
2:00remaining
Result of Reading from Secondary with Lag
Given a MongoDB replica set where the secondary is 5 seconds behind the primary, what is the expected behavior when a read query is sent with read preference set to 'secondary' immediately after a write on the primary?
MongoDB
db.collection.insertOne({item: 'book', qty: 10});
// Immediately after, a read with readPreference: 'secondary' is performed
AThe read returns the document only after the secondary catches up, causing a delay.
BThe read returns the newly inserted document immediately.
CThe read fails because secondaries do not accept reads.
DThe read returns an error due to replication lag.
Attempts:
2 left
💡 Hint
Consider how replication delay affects data visibility on secondaries.
❓ optimization
advanced
2:00remaining
Optimizing Read Latency with Secondary Reads
Which strategy best reduces read latency when using secondary reads in a geographically distributed MongoDB replica set?
AConfigure read preference to 'nearest' to read from the closest secondary node.
BAlways read from the primary to ensure data freshness.
CDisable replication to avoid lag.
DUse read preference 'primaryPreferred' to read only from the primary.
Attempts:
2 left
💡 Hint
Think about how network distance affects latency.
🔧 Debug
advanced
2:00remaining
Diagnosing Stale Data Reads
An application reads from secondaries but notices stale data frequently. Which MongoDB feature can help reduce stale reads without sacrificing availability?
ADisable secondary reads and read only from primary.
BIncrease write concern to 'w: 0' to speed up writes.
CUse 'readConcern' set to 'majority' to ensure data is acknowledged by most nodes.
DSet read preference to 'primaryPreferred' to always read from primary.
Attempts:
2 left
💡 Hint
Consider how read concern affects data consistency.
🧠 Conceptual
expert
2:00remaining
Trade-offs of Reading from Secondaries in High Write Workloads
In a MongoDB replica set with heavy write traffic, what is a key trade-off when enabling reads from secondaries to scale read throughput?
ASecondary nodes reject all read requests to maintain consistency.
BIncreased risk of reading outdated data due to replication lag, but improved read scalability.
CWrites become slower because secondaries handle write operations.
DPrimary node becomes a bottleneck for all reads and writes.
Attempts:
2 left
💡 Hint
Think about how replication lag affects read freshness and system scalability.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It helps balance the read load and can improve read speed. -> Option A
  4. 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

  1. Step 1: Recall MongoDB read preference options

    MongoDB supports 'primary', 'primaryPreferred', 'secondary', and 'secondaryPreferred' as read preferences.
  2. 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').
  3. Final Answer:

    db.collection.find().readPreference('secondaryPreferred') -> Option D
  4. 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

  1. Step 1: Understand replication lag in secondaries

    Secondaries replicate data asynchronously, so there can be a delay causing stale data.
  2. Step 2: Analyze read preference 'secondary'

    Reading from secondaries means data may not be the freshest due to replication lag.
  3. Final Answer:

    You might get slightly stale data due to replication lag. -> Option B
  4. 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

  1. Step 1: Identify cause of errors

    Using 'secondary' read preference causes errors if no secondary nodes are available.
  2. Step 2: Choose a read preference that allows fallback

    'secondaryPreferred' reads from secondaries if available, else falls back to primary, preventing errors.
  3. Final Answer:

    Change read preference to 'secondaryPreferred' to allow fallback to primary. -> Option C
  4. 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

  1. Step 1: Analyze application needs

    The app wants fast reads, can accept some stale data, and wants to reduce primary load.
  2. Step 2: Evaluate read preference options

    'secondary' risks errors if no secondaries; 'secondaryPreferred' reads from secondaries if available, else primary, balancing load and freshness.
  3. Step 3: Choose best option

    'secondaryPreferred' fits best as it balances load and freshness with fallback.
  4. Final Answer:

    Use 'secondaryPreferred' to read from secondaries when available, else primary, balancing load and freshness. -> Option A
  5. 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
  • Choosing 'primaryPreferred' ignores secondary reads
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard