0
0
MongoDBquery~10 mins

Replica set architecture mental model in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Replica set architecture mental model
Client sends request
Primary node receives
Primary applies write
Primary replicates to secondaries
Secondaries apply write
Secondaries acknowledge
Primary confirms write success
Client receives confirmation
If primary fails
Election starts
New primary elected
Clients redirect to new primary
The client sends requests to the primary node, which applies writes and replicates them to secondary nodes. If the primary fails, an election chooses a new primary to keep the system running.
Execution Sample
MongoDB
Client -> Primary: Write data
Primary -> Secondaries: Replicate data
Secondaries -> Primary: Acknowledge
Primary -> Client: Confirm write
If Primary fails -> Election -> New Primary
Shows the flow of a write operation and failover in a MongoDB replica set.
Execution Table
StepActionNode InvolvedState ChangeResult
1Client sends write requestClientRequest sent to PrimaryPrimary receives write
2Primary applies writePrimaryData written to PrimaryWrite successful on Primary
3Primary replicates writePrimary -> SecondariesWrite sent to SecondariesSecondaries receive write
4Secondaries apply writeSecondariesData written to SecondariesSecondaries updated
5Secondaries acknowledgeSecondaries -> PrimaryAcknowledgement sentPrimary receives acknowledgements
6Primary confirms writePrimary -> ClientConfirmation sentClient receives success
7Primary failsPrimaryPrimary goes downNo response from Primary
8Election startsSecondariesVote for new PrimaryOne Secondary elected Primary
9New Primary electedNew PrimaryBecomes PrimaryClients redirect writes here
💡 Execution stops after new primary is elected and clients redirect their requests.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 7After Step 9
Primary statusUpUpUpUpDownUp
Secondaries statusUpUpUpUpUpUp
Write data stateNot writtenWritten on PrimaryWritten on SecondariesConfirmed to ClientConfirmedConfirmed
Client write statusNot sentSentWaitingSuccessWaiting for new PrimaryRedirected
Key Moments - 3 Insights
Why does the client send writes only to the primary node?
Because the primary node is the only one that accepts writes and replicates them to secondaries, as shown in execution_table rows 1 and 2.
What happens when the primary node fails during operation?
An election starts among secondaries to choose a new primary, as shown in execution_table rows 7 to 9, ensuring continuous availability.
How do secondaries confirm they have applied the write?
They send acknowledgements back to the primary, which then confirms success to the client, as shown in execution_table rows 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the client receive confirmation that the write succeeded?
AStep 5
BStep 3
CStep 6
DStep 9
💡 Hint
Check the 'Result' column for when the client receives success confirmation.
According to variable_tracker, what is the status of the primary after step 7?
ADown
BUp
CUnknown
DRecovering
💡 Hint
Look at the 'Primary status' row after step 7.
If the primary fails, what is the next action in the replica set flow?
AClient retries write to primary
BSecondaries start election
CSecondaries shut down
DClient disconnects
💡 Hint
Refer to execution_table steps 7 and 8 for failover process.
Concept Snapshot
Replica set has one primary and multiple secondaries.
Client sends writes only to primary.
Primary applies writes and replicates to secondaries.
Secondaries acknowledge writes.
If primary fails, secondaries hold election.
New primary elected to maintain availability.
Full Transcript
In a MongoDB replica set, the client sends write requests to the primary node. The primary applies the write and then replicates it to the secondary nodes. The secondaries apply the write and send acknowledgements back to the primary. Once the primary receives acknowledgements, it confirms success to the client. If the primary node fails, the secondaries start an election to choose a new primary. After a new primary is elected, clients redirect their writes to the new primary, ensuring continuous operation.