Top Banner
Hackathoner, MongoDB J. Randall Hunt #mongodbdays chicago Introduction to Replication and Replica Sets
43

Replication MongoDB Days 2013

Jan 15, 2015

Download

Technology

Randall Hunt

Slides used in MongoDB Days 2013 presentations in North America
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Replication MongoDB Days 2013

Hackathoner, MongoDB

J. Randall Hunt

#mongodbdays chicago

Introduction to Replication and Replica Sets

Page 2: Replication MongoDB Days 2013

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: Replication MongoDB Days 2013

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 4: Replication MongoDB Days 2013

Agenda

•  Replica Sets Lifecycle

•  Developing with Replica Sets

•  Operational Considerations

Page 5: Replication MongoDB Days 2013

Replica Set Lifestyle

Page 6: Replication MongoDB Days 2013

Replica Set – Creation

Node 1 Node 2

Node 3

Page 7: Replication MongoDB Days 2013

Replica Set – Initialize

Node 1Secondary

Node 2Secondary

Node 3Primary

Replication

Heartbeat

Replication

Page 8: Replication MongoDB Days 2013

Replica Set – Failure

Node 1Secondary

Node 2Secondary

Node 3

Heartbeat

Primary Election

Page 9: Replication MongoDB Days 2013

Replica Set – Failover

Node 1Secondary

Node 2Primary

Node 3

Replication

Heartbeat

Page 10: Replication MongoDB Days 2013

Replica Set – Recovery

Node 1Secondary

Node 2Primary

Replication

Heartbeat

Node 3Recovery

Replication

Page 11: Replication MongoDB Days 2013

Replica Set – Recovered

Node 1Secondary

Node 2Primary

Replication

Heartbeat

Node 3Secondary

Replication

Page 12: Replication MongoDB Days 2013

Replica Set Roles & Configuration

Page 13: Replication MongoDB Days 2013

Replica Set Roles

Node 1Secondary

Node 2Arbiter

Node 3Primary

Heartbeat

Replication

Page 14: Replication MongoDB Days 2013

> conf = {

_id: ”empire",

members: [

{ _id: 0, host: 'vader.deathstar.mil'},

{ _id: 1, host: 'tarkin.deathstar.mil'},

{ _id: 2, host: 'palpatine.deathstar.mil'}

]

}

> rs.initiate(conf)

Configuration Options

Page 15: Replication MongoDB Days 2013

> conf = {

_id: ”empire",

members: [

{ _id: 0, host: 'vader.deathstar.mil', priority: 3},

{ _id: 1, host: 'tarkin.deathstar.mil', priority: 2},

{ _id: 2, host: 'palpatine.deathstar.mil'}

]

}

> rs.initiate(conf)

Configuration Options

Primary DC

Page 16: Replication MongoDB Days 2013

> conf = {

_id: ”empire",

members: [

{ _id: 0, host: 'vader.deathstar.mil', priority: 3},

{ _id: 1, host: 'tarkin.deathstar.mil', priority: 2},

{ _id: 2, host: 'palpatine.deathstar.mil'},

{ _id: 3, host: 'marajade.empire.mil', hidden: true}

]

}

> rs.initiate(conf)

Configuration Options

Analytics node

Page 17: Replication MongoDB Days 2013

> conf = { _id: "myset", members: [ { _id: 0, host: 'vader.deathstar.mil', priority: 3}, { _id: 1, host: 'tarkin.deathstar.mil', priority: 2}, { _id: 2, host: 'palpatine.deathstar.mil'}, { _id: 3, host: 'marajade.empire.mil', hidden: true}, { _id: 4, host: 'thrawn.empire.mil', hidden: true, slaveDelay: 3600} ] }} > rs.initiate(conf)

Configuration Options

Backup node

Page 18: Replication MongoDB Days 2013

Developing with Replica Sets

Page 19: Replication MongoDB Days 2013

Strong Consistency

Secondary Secondary

Primary

Client ApplicationDriver

Write

Read

Page 20: Replication MongoDB Days 2013

Delayed Consistency

Secondary Secondary

Primary

Client ApplicationDriver

Write

Read Read

Page 21: Replication MongoDB Days 2013

Write Concern

•  Network acknowledgement

•  Wait for error

•  Wait for journal sync

•  Wait for replication

Page 22: Replication MongoDB Days 2013

Unacknowledged

write

Driver

Primary

apply inmemory

Page 23: Replication MongoDB Days 2013

MongoDB Acknowledged (wait for error) getLastError

Driver

Primary

apply inmemory

Page 24: Replication MongoDB Days 2013

Wait for Journal Sync

Driver

Primary

write tojournal

apply inmemory

getLastError

write

j:true

Page 25: Replication MongoDB Days 2013

Wait for Replication

Driver

Primary

Secondary

getLastError

write

w:2

replicate

apply inmemory

Page 26: Replication MongoDB Days 2013

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 27: Replication MongoDB Days 2013

{ _id : "mySet", members : [ {_id : 0, host : "frodo", tags : {"dc": "ny"}}, {_id : 1, host : "sam", tags : {"dc": "ny"}}, {_id : 2, host : "merry", tags : {"dc": "sf"}}, {_id : 3, host : "pippin", tags : {"dc": "sf"}}, {_id : 4, host : "gandalf", tags : {"dc": "cloud"}}], settings : { getLastErrorModes : { allDCs : {"dc" : 3}, someDCs : {"dc" : 2}} } } > db.blogs.insert({...}) > db.runCommand({getLastError : 1, w : "someDCs"})

Tagging Example

Page 28: Replication MongoDB Days 2013

Wait for Replication (Tagging)

Driver

Primary (SF)

Secondary (NY)

getLastError

write

W:allDCs

Secondary (Cloud)

replicate

replicate

apply inmemory

Page 29: Replication MongoDB Days 2013

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 30: Replication MongoDB Days 2013

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 31: Replication MongoDB Days 2013

Operational Considerations

Page 32: Replication MongoDB Days 2013

Maintenance and Upgrade

•  No downtime

•  Rolling upgrade/maintenance –  Start with Secondary –  Primary last

Page 33: Replication MongoDB Days 2013

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 34: Replication MongoDB Days 2013

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 remote DC

Member 3

Datacenter 2

Member 1

Member 2

Datacenter 1

Page 35: Replication MongoDB Days 2013

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 36: Replication MongoDB Days 2013

Behind the Curtain

Page 37: Replication MongoDB Days 2013

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 38: Replication MongoDB Days 2013

> 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 39: Replication MongoDB Days 2013

> 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 40: Replication MongoDB Days 2013

Recent improvements

•  Read preference support with sharding –  Drivers too

•  Improved replication over WAN/high-latency networks

•  rs.syncFrom command

•  buildIndexes setting

•  replIndexPrefetch setting

Page 41: Replication MongoDB Days 2013

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 42: Replication MongoDB Days 2013

Questions?

Page 43: Replication MongoDB Days 2013

Thank You

Hackathoner, MongoDB @jrhunt, [email protected]

J. Randall Hunt

#mongodbdays seattle