2. Replication
Process of keeping IDENTICAL COPY
of your data on different servers.
Replication offers
Minimum Downtime
in case of hardware failure, software
failure or maintenance
4. Replica Set
MongoDB’s Terminology for Replication
A replica set is a group of servers with one primary,
the server taking client requests, and multiple
secondaries, servers that keep copies of the primary’s
data.
5. Give A Go To
MongoDB’s Replica Set
In
60 Seconds!
#ToBeTriedOnlyAtHome
6. Step 1: Setup Replica Set
$: mongo --nodb
$: myReplica = new ReplSetTest({nodes: 3});
$: myReplica.startSet();
$: myReplica.initiate();
8. Step 2: Connect To Primary
$: mongo --port 31000
testReplSet:PRIMARY> use surryhills_db
testReplSet:PRIMARY> db.offices.insert({name: ‘MongoDB’});
testReplSet:PRIMARY> show dbs
testReplSet:PRIMARY> show collections
testReplSet:PRIMARY> db.offices.find().pretty()
9. Pro Tip :]
Auto Format Query Response
Add
DBQuery.prototype._prettyShell = true;
to your ~/.mongorc.js
10. Step 3: Connect To A Secondary
$: mongo --port 31001
testReplSet:SECONDARY> db.slaveOk()
testReplSet:SECONDARY> show dbs
testReplSet:SECONDARY> use surryhills_db
testReplSet:SECONDARY> show collections
testReplSet:SECONDARY> db.offices.find()
15. Auto Connect To Current Primary
$: mongo --host myReplica/my-host:31000,my-host:31001,my-host:31002
16. Replica Set
Good To Knows
1. Always start your instance with `replSet`
2. You can’t initiate a replica set with more than
one member with existing data on it.
3. If one of set members has data, you must run
rs.initiate() against that member
4. `system.replset` collection on `local` database
of all members contains set configuration.
5. Replica sets can have up to 50 members, 7 voting
6. By default, MongoDB reads from primary
17. Replica Set Reconfiguration
Add new member
$: mongo --port 31000 # current primary
$: rs.add(‘my-host:31003’) # accepts object as well.
19. Replica Set Reconfiguration
Update existing member
$: mongo --port 31000 # current primary
$: config = rs.config()
$: config.members[2].priority = 0;
$: rs.reconfig(config);
20. How Replication Works
MongoDB keeps a log of every write operations that
primary does in database
Logs are saved in “oplog.rs” collection of “local” db
Secondaries apply changes by reading from oplog.rs and
applieing to their data
Secondaries maintain their own “oplog.rs” as well.
21. “oplog.rs” Facts
“oplog.rs” is a capped collection which means it can
save limited amount of operation logs.
Bulk operations fill “oplog” quickly. A bulk removal of
1K documents adds 1K documents to “oplog”
It’s important to have a “oplog.rs” to keep logs for
atleast last 24 hrs.
22. “Stale” Secondary :(
Secondary misses a copy of last operation it applied in
syncing source.
Secondary queries other members to find another
source to continue syncing.
Secondary missed some operations permanently.
Admin must resyncs from scratch.
23. Heartbeat
Members PING each other every 2 seconds.
Members should
reply within 10 seconds
otherwise they’re counted
as unreachable.
24. Change in replica set configuration
Current primary steps down
Replica Set Election
When It Happens?
Current primary is unreachable
26. You need a majority of voting
members to elect a primary.
A primary can only stay
primary so long as it can reach
a majority of voting members.
Majority
27. Replica Set with two members.
Three Common
Replica Set
Design Mistakes :(
Even number of members in two data center.
A set with more than one arbiter
28. Keep a majority of members in preferred
data center and rest in the other one.
Two Common
Replica Set
Design Patterns :)
Keep even number of members in two data
center and a tie breaker in third one.
29. Meet Arbiter
Budget Friendly Member Of Replica Set
Does not hold data
It’s a lightweight process
Can vote on elections
30. Start An Arbiter Process
$: mongod --port 31005 --replSet myReplica --
nojournal --smallfiles
Arbiter does not save data, no need for journaling
Don’t waste disk space and use --smallfiles
31. Add An Arbiter To Replica Set
$: rs.addArb(‘arbiter-host-name:port’);
32. Arbiter
Good To Know
You can’t change an arbiter to non-arbiter member
If possible, use normal members instead of arbiters
At most you need one arbiter in your set
Do not add an arbiter to a set with odd members
36. Rollback Process
1. Former primary reverts write operations
2. Saves affected documents into /rollback directory
3. Admin browses documents using mongorestore
4. Admin applies changes to the current primary
37. Durability
Write Concerns And Replica Set
1. Unacknowledged
2. Acknowledged
3. Journaled
4. Replica Acknowledged
5. Custom Writer Concern
43. Replica Set &
Mixed Storage Engines
You can have both
WiredTiger & MMAPv1
● Due to migration
● Due to different use cases
44. Keen To Go Further?
Register for M102: MONGODB FOR DBAS course!
Starts tomorrow!
It’s awesome!
https://university.mongodb.com/courses/M102/about
45. Keen To Go Further?
docs.mongodb.org/manual/replication
MongoDB
The Definite Guide
46. Stay In Touch
● Follow me on Twitter
@mehdivk
● Add me to your Linkedin network https://au.
linkedin.com/in/valikhani
● Subscribe to my blog
blog.mehdivk.net
● Send me an email
valikhani@sirangweb.com