MongoDB London 2013 - Basic Replication
-
Upload
marc-schwering -
Category
Technology
-
view
209 -
download
2
Transcript of MongoDB London 2013 - Basic Replication
![Page 1: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/1.jpg)
Solutions Architect, 10gen
Marc Schwering
#MongoDBDays - @m4rcsch
Replication and Replica Sets
![Page 2: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/2.jpg)
Notes to the presenter Themes for this presentation:
• Balance between cost and redundancy.
• Cover the many scenarios which replication would solve and why.
• Secret sauce of avoiding downtime & data loss
• If time is short, skip 'Behind the curtain' section
• If time is very short, skip Operational Considerations also
![Page 3: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/3.jpg)
Agenda
• Replica Sets Lifecycle
• Developing with Replica Sets
• Operational Considerations
![Page 4: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/4.jpg)
Why Replication?
• How many have faced node failures?
• How many have been woken up from sleep to do a fail-over(s)?
• How many have experienced issues due to network latency?
• Different uses for data – Normal processing – Simple analytics
![Page 5: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/5.jpg)
ReplicaSet Lifecycle
![Page 6: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/6.jpg)
Node 1 Node 2
Node 3
Replica Set – Creation
![Page 7: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/7.jpg)
Node 1Secondary
Node 2Secondary
Node 3Primary
Replication
Heartbeat
ReplicationReplica Set – Initialize
![Page 8: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/8.jpg)
Node 1Secondary
Node 2Secondary
Node 3
Heartbeat
Primary Election
Replica Set – Failure
![Page 9: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/9.jpg)
Node 1Secondary
Node 2Primary
Node 3
Replication
Heartbeat
Replica Set – Failover
![Page 10: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/10.jpg)
Node 1Secondary
Node 2Primary
Replication
Heartbeat
Node 3Recovery
Replication
Replica Set – Recovery
![Page 11: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/11.jpg)
Node 1Secondary
Node 2Primary
Replication
Heartbeat
Node 3Secondary
Replication
Replica Set – Recovered
![Page 12: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/12.jpg)
ReplicaSet Roles & Configuration
![Page 13: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/13.jpg)
Node 1Secondary
Node 2Arbiter
Node 3Primary
Heartbeat
ReplicationReplica Set Roles
![Page 14: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/14.jpg)
> conf = { _id : "mySet", members : [ {_id : 0, host : "A”, priority : 3}, {_id : 1, host : "B", priority : 2}, {_id : 2, host : "C”}, {_id : 3, host : "D", hidden : true}, {_id : 4, host : "E", hidden : true, slaveDelay : 3600} ]
} > rs.initiate(conf)
Configuration Options
![Page 15: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/15.jpg)
> conf = { _id : "mySet”, members : [ {_id : 0, host : "A”, priority : 3}, {_id : 1, host : "B", priority : 2}, {_id : 2, host : "C”}, {_id : 3, host : "D", hidden : true}, {_id : 4, host : "E", hidden : true, slaveDelay : 3600} ]
} > rs.initiate(conf)
Configuration Options
Primary DC
Primary DC
![Page 16: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/16.jpg)
> conf = { _id : "mySet”, members : [ {_id : 0, host : "A”, priority : 3}, {_id : 1, host : "B", priority : 2}, {_id : 2, host : "C”}, {_id : 3, host : "D", hidden : true}, {_id : 4, host : "E", hidden : true, slaveDelay : 3600} ]
} > rs.initiate(conf)
Configuration Options
Secondary DC Default Priority = 1
![Page 17: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/17.jpg)
> conf = { _id : "mySet”, members : [ {_id : 0, host : "A”, priority : 3}, {_id : 1, host : "B", priority : 2}, {_id : 2, host : "C”}, {_id : 3, host : "D", hidden : true}, {_id : 4, host : "E", hidden : true, slaveDelay : 3600} ]
} > rs.initiate(conf)
Configuration Options
Analytics node
![Page 18: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/18.jpg)
> conf = { _id : "mySet”, members : [ {_id : 0, host : "A”, priority : 3}, {_id : 1, host : "B", priority : 2}, {_id : 2, host : "C”}, {_id : 3, host : "D", hidden : true}, {_id : 4, host : "E", hidden : true, slaveDelay : 3600} ]
} > rs.initiate(conf)
Configuration Options
Backup node
![Page 19: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/19.jpg)
Developing with Replica Sets
![Page 20: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/20.jpg)
Secondary Secondary
Primary
Client ApplicationDriver
Write
Read
Strong Consistency
![Page 21: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/21.jpg)
Secondary Secondary
Primary
Client ApplicationDriver
Write
Read Read
Delayed Consistency
![Page 22: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/22.jpg)
Write Concern
• Network acknowledgement
• Wait for error
• Wait for journal sync
• Wait for replication
![Page 23: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/23.jpg)
write
Driver
Primary
apply inmemory
Unacknowledged
![Page 24: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/24.jpg)
Driver
Primary
apply inmemory
getLastError
MongoDB Acknowledged (wait for error)
![Page 25: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/25.jpg)
Driver
Primary
write tojournal
apply inmemory
getLastError
write
j:trueWait for Journal Sync
![Page 26: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/26.jpg)
Driver
Primary
Secondary
getLastError
write
w:2
replicate
apply inmemory
Wait for Replication
![Page 27: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/27.jpg)
Tagging
• Control where data is written to, and read from
• Each member can have one or more tags – tags: {dc: "ny"} – tags: {dc: "ny",
subnet: "192.168", rack: "row3rk7"}
• Replica set defines rules for write concerns
• Rules can change without changing app code
![Page 28: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/28.jpg)
{ _id : "mySet", members : [ {_id : 0, host : "A", tags : {"dc": "ny"}}, {_id : 1, host : "B", tags : {"dc": "ny"}}, {_id : 2, host : "C", tags : {"dc": "sf"}}, {_id : 3, host : "D", tags : {"dc": "sf"}}, {_id : 4, host : "E", tags : {"dc": "cloud"}}], settings : { getLastErrorModes : { allDCs : {"dc" : 3}, someDCs : {"dc" : 2}} } } > db.blogs.insert({...}) > db.runCommand({getLastError : 1, w : "someDCs"})
Tagging Example
![Page 29: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/29.jpg)
Driver
Primary (SF)
Secondary (NY)
getLastError
write
W:allDCs
Secondary (Cloud)
replicate
replicate
apply inmemory
Wait for Replication (Tagging)
![Page 30: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/30.jpg)
Read Preference Modes
• 5 modes – primary (only) - Default – primaryPreferred – secondary – secondaryPreferred – Nearest
When more than one node is possible, closest node is used for reads (all modes but primary)
![Page 31: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/31.jpg)
Tagged Read Preference
• Custom read preferences
• Control where you read from by (node) tags – E.g. { "disk": "ssd", "use": "reporting" }
• Use in conjunction with standard read preferences – Except primary
![Page 32: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/32.jpg)
Operational Considerations
![Page 33: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/33.jpg)
Maintenance and Upgrade
• No downtime
• Rolling upgrade/maintenance – Start with Secondary – Primary last
![Page 34: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/34.jpg)
Replica Set – 1 Data Center
• Single datacenter
• Single switch & power
• Points of failure: – Power – Network – Data center – Two node failure
• Automatic recovery of single node crash
Datacenter 2
Datacenter
Member 1
Member 2
Member 3
![Page 35: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/35.jpg)
Replica Set – 2 Data Centers
• Multi data center
• DR node for safety
• Can’t do multi data center durable write safely since only 1 node in distant DC
Member 3
Datacenter 2
Member 1
Member 2
Datacenter 1
![Page 36: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/36.jpg)
Replica Set – 3 Data Centers
• Three data centers
• Can survive full data center loss
• Can do w= { dc : 2 } to guarantee write in 2 data centers (with tags)
Datacenter 1Member 1
Member 2
Datacenter 2Member 3
Member 4
Datacenter 3Member 5
![Page 37: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/37.jpg)
Behind the Curtain
![Page 38: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/38.jpg)
Implementation details
• Heartbeat every 2 seconds – Times out in 10 seconds
• Local DB (not replicated) – system.replset – oplog.rs • Capped collection • Idempotent version of operation stored
![Page 39: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/39.jpg)
> db.replsettest.insert({_id:1,value:1})
{ "ts" : Timestamp(1350539727000, 1), "h" : NumberLong("6375186941486301201"), "op" : "i", "ns" : "test.replsettest", "o" : { "_id" : 1, "value" : 1 } }
> db.replsettest.update({_id:1},{$inc:{value:10}})
{ "ts" : Timestamp(1350539786000, 1), "h" : NumberLong("5484673652472424968"), "op" : "u", "ns" : "test.replsettest", "o2" : { "_id" : 1 }, "o" : { "$set" : { "value" : 11 } } }
Op(erations) Log is idempotent
![Page 40: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/40.jpg)
> db.replsettest.update({},{$set:{name : ”foo”}, false, true})
{ "ts" : Timestamp(1350540395000, 1), "h" : NumberLong("-4727576249368135876"), "op" : "u", "ns" : "test.replsettest", "o2" : { "_id" : 2 }, "o" : { "$set" : { "name" : "foo" } } }
{ "ts" : Timestamp(1350540395000, 2), "h" : NumberLong("-7292949613259260138"), "op" : "u", "ns" : "test.replsettest", "o2" : { "_id" : 3 }, "o" : { "$set" : { "name" : "foo" } } }
{ "ts" : Timestamp(1350540395000, 3), "h" : NumberLong("-1888768148831990635"), "op" : "u", "ns" : "test.replsettest", "o2" : { "_id" : 1 }, "o" : { "$set" : { "name" : "foo" } } }
Single operation can have many entries
![Page 41: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/41.jpg)
Recent improvements
• Read preference support with sharding – Drivers too
• Improved replication over WAN/high-latency networks
• rs.syncFrom command
• buildIndexes setting
• replIndexPrefetch setting
![Page 42: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/42.jpg)
Just Use It
• Use replica sets
• Easy to setup – Try on a single machine
• Check doc page for RS tutorials – http://docs.mongodb.org/manual/replication/#tutorials
![Page 43: MongoDB London 2013 - Basic Replication](https://reader033.fdocuments.us/reader033/viewer/2022052908/5594708a1a28ab990e8b46da/html5/thumbnails/43.jpg)
Solutions Architect, 10gen
Marc Schwering
#MongoDBDays - @m4rcsch
Thank You