Bird
Raised Fist0
MongoDBquery~20 mins

Replica set architecture mental model in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Replica Set Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Primary role in a MongoDB replica set
In a MongoDB replica set, what is the main responsibility of the primary node?
AIt handles all write operations and replicates data to secondaries.
BIt monitors network traffic but does not store data.
CIt serves read requests exclusively and never accepts writes.
DIt only stores backups and does not handle any client requests.
Attempts:
2 left
💡 Hint
Think about which node accepts changes from users.
❓ query_result
intermediate
2:00remaining
Output of rs.status() command
What key information does the rs.status() command provide in a MongoDB replica set?
AIt displays the network configuration of the replica set members.
BIt shows the current state of each member, including primary or secondary roles.
CIt lists all databases and their sizes on the primary node.
DIt returns the user access permissions for the replica set.
Attempts:
2 left
💡 Hint
Think about what status means for nodes in a set.
📝 Syntax
advanced
2:00remaining
Correct syntax to initiate a replica set
Which of the following commands correctly initiates a MongoDB replica set named 'rs0' with one member?
MongoDB
rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'localhost:27017' }] })
AreplicaSet.initiate({ name: 'rs0', members: ['localhost:27017'] })
Brs.start({ id: 'rs0', nodes: [{ id: 0, address: 'localhost:27017' }] })
Crs.create({ _id: 'rs0', members: [{ _id: 0, host: 'localhost:27017' }] })
Drs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'localhost:27017' }] })
Attempts:
2 left
💡 Hint
Look for the official MongoDB command to start a replica set.
❓ optimization
advanced
2:00remaining
Improving read availability in a replica set
To improve read availability and reduce load on the primary, which configuration is best in a MongoDB replica set?
AEnable read preference to 'secondary' so reads can be served by secondary nodes.
BDisable replication to secondaries to reduce network traffic.
CSet all nodes as primary to allow writes everywhere.
DUse only one node to avoid replication lag.
Attempts:
2 left
💡 Hint
Think about how to distribute read requests safely.
🔧 Debug
expert
3:00remaining
Diagnosing replica set member state
A secondary member in a MongoDB replica set is stuck in the 'RECOVERING' state and not becoming 'SECONDARY'. What is the most likely cause?
AThe secondary has network issues and cannot connect to the client.
BThe primary node is down, so the secondary cannot become primary.
CThe secondary is still syncing data from the primary and has not caught up yet.
DThe replica set name is incorrect on the secondary configuration.
Attempts:
2 left
💡 Hint
Consider what 'RECOVERING' means for a secondary node.

Practice

(1/5)
1. What is the main purpose of a MongoDB replica set?
easy
A. To backup data manually every hour
B. To keep copies of data on multiple servers for safety and availability
C. To store data in different formats on the same server
D. To speed up queries by using multiple CPUs on one server

Solution

  1. Step 1: Understand replica set function

    A replica set keeps copies of data on multiple servers to protect against failure.
  2. Step 2: Compare options

    Only To keep copies of data on multiple servers for safety and availability describes this purpose correctly; others describe unrelated tasks.
  3. Final Answer:

    To keep copies of data on multiple servers for safety and availability -> Option B
  4. Quick Check:

    Replica set = data copies on servers [OK]
Hint: Replica sets = multiple copies for safety [OK]
Common Mistakes:
  • Confusing replica sets with sharding
  • Thinking replica sets speed up CPU usage
  • Believing backups are manual in replica sets
2. Which of the following is the correct way MongoDB decides the primary in a replica set?
easy
A. MongoDB automatically elects a primary among members
B. The server with the most data becomes primary
C. The oldest server is always primary
D. The user manually sets the primary server

Solution

  1. Step 1: Recall primary election process

    MongoDB automatically elects one server as primary to manage writes.
  2. Step 2: Eliminate incorrect options

    Primary is not chosen by data size, age, or manual setting.
  3. Final Answer:

    MongoDB automatically elects a primary among members -> Option A
  4. Quick Check:

    Primary election = automatic [OK]
Hint: Primary is chosen automatically, not manually [OK]
Common Mistakes:
  • Assuming user sets primary manually
  • Thinking data size decides primary
  • Believing oldest server is primary
3. Consider a replica set with one primary and two secondaries. If the primary goes down, what happens next?
medium
A. All secondaries become primary at the same time
B. The replica set stops working until the original primary returns
C. One of the secondaries is automatically elected as the new primary
D. The data is lost because there is no primary

Solution

  1. Step 1: Understand failover in replica sets

    If the primary fails, MongoDB elects a new primary from secondaries automatically.
  2. Step 2: Check other options

    Replica sets do not stop working, do not have multiple primaries, and do not lose data immediately.
  3. Final Answer:

    One of the secondaries is automatically elected as the new primary -> Option C
  4. Quick Check:

    Failover = automatic new primary [OK]
Hint: Primary failure triggers automatic new primary election [OK]
Common Mistakes:
  • Thinking replica set stops on primary failure
  • Believing multiple primaries exist simultaneously
  • Assuming data loss happens immediately
4. You set up a replica set but notice writes are failing. Which of these could be the cause?
medium
A. All members are healthy and synced
B. The replica set has only one member
C. Secondary nodes are accepting writes
D. No primary is elected in the replica set

Solution

  1. Step 1: Identify write requirements in replica sets

    Writes must go to the primary; if no primary exists, writes fail.
  2. Step 2: Analyze options

    Healthy members or single member replica sets usually allow writes; secondaries do not accept writes.
  3. Final Answer:

    No primary is elected in the replica set -> Option D
  4. Quick Check:

    Writes need primary elected [OK]
Hint: Writes fail if no primary exists [OK]
Common Mistakes:
  • Thinking secondaries accept writes
  • Assuming single member replica set blocks writes
  • Ignoring primary election status
5. You want to ensure your MongoDB replica set remains available even if two servers fail. What is the minimum number of members you should have in your replica set?
hard
A. 5 members
B. 4 members
C. 3 members
D. 6 members

Solution

  1. Step 1: Understand majority voting in replica sets

    Replica sets use majority votes to elect primary; to tolerate 2 failures, majority must be > half of total members.
  2. Step 2: Calculate minimum members

    With 5 members, majority is 3; losing 2 still leaves 3 members to form majority and keep primary.
  3. Final Answer:

    5 members -> Option A
  4. Quick Check:

    To survive 2 failures, need 5 members [OK]
Hint: Majority = more than half; 5 members survive 2 failures [OK]
Common Mistakes:
  • Choosing 3 or 4 members which can't tolerate 2 failures
  • Not considering majority voting
  • Assuming even number members are better
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