back to basics webinar 3: introduction to replica sets

49

Upload: mongodb

Post on 09-Feb-2017

685 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Page 1: Back to Basics Webinar 3: Introduction to Replica Sets
Page 2: Back to Basics Webinar 3: Introduction to Replica Sets

Back to Basics 2017 : Webinar 3Introduction to Replica Sets

Joe DrumgooleDirector of Developer Advocacy, EMEA

MongoDB@jdrumgoole

V1.0

Page 3: Back to Basics Webinar 3: Introduction to Replica Sets

3

Summary of Part 1 and 2

• Why NoSQL exists• The types of NoSQL database• The key features of MongoDB• How to install MongoDB• How to do the basic CRUD operations• How to create indexes• How to use the explain() plan• MongoDB Compass and MongoDB Atlas

Page 4: Back to Basics Webinar 3: Introduction to Replica Sets

4

Agenda

• Data durability• The MongoDB Approach – The replica set• Replica Set Life-cycle• How to program when using a replica set

Page 5: Back to Basics Webinar 3: Introduction to Replica Sets

5

Next Webinar : Introduction to Sharding

• How to build a highly scalable performant cluster• How to remove write bottlenecks• How to select a shard key

Thursday, 9-Feb-2016, 11:00 am GMT.

Page 6: Back to Basics Webinar 3: Introduction to Replica Sets

6

High Availability and Data Durability – Replica Sets

SecondarySecondary

Primary

Page 7: Back to Basics Webinar 3: Introduction to Replica Sets

7

Replica Set Creation

SecondarySecondary

Primary

Heartbeat

Page 8: Back to Basics Webinar 3: Introduction to Replica Sets

8

Replica Set Node Failure

SecondarySecondary

Primary

No Heartbeat

Page 9: Back to Basics Webinar 3: Introduction to Replica Sets

9

Replica Set Recovery

SecondarySecondary

HeartbeatAnd Election

Page 10: Back to Basics Webinar 3: Introduction to Replica Sets

10

New Replica Set – 2 Nodes

SecondaryPrimary

HeartbeatAnd New Primary

Page 11: Back to Basics Webinar 3: Introduction to Replica Sets

11

Replica Set Repair

SecondaryPrimary

Secondary

Rejoin and resync

Page 12: Back to Basics Webinar 3: Introduction to Replica Sets

12

Replica Set Stable

SecondaryPrimary

Secondary

Heartbeat

Page 13: Back to Basics Webinar 3: Introduction to Replica Sets

Developing with Replica Sets

Page 14: Back to Basics Webinar 3: Introduction to Replica Sets

14

Driver Responsibilities

https://github.com/mongodb/mongo-python-driver

Driver

Authentication& Security Python<->BSON Error handling &

Recovery

WireProtocol

Topology Management Connection Pool

Page 15: Back to Basics Webinar 3: Introduction to Replica Sets

15

Strong Consistency

SecondarySecondary

Primary

Client Application

Client Driver

Writ

e Read

Page 16: Back to Basics Webinar 3: Introduction to Replica Sets

16

Eventual Consistency

SecondarySecondary

Primary

Client Application

Client Driver

Writ

e

ReadRea

d

Page 17: Back to Basics Webinar 3: Introduction to Replica Sets

17

Write Concerns

• Network acknowledgement• Wait for error • Wait for journal sync• Wait for replication

Page 18: Back to Basics Webinar 3: Introduction to Replica Sets

18

Write Concern : 0 (Unacknowledged)

Page 19: Back to Basics Webinar 3: Introduction to Replica Sets

19

Write Concern : 1 (Acknowledged)

Page 20: Back to Basics Webinar 3: Introduction to Replica Sets

20

Write Concern : majority

Page 21: Back to Basics Webinar 3: Introduction to Replica Sets

21

Driver Responsibilities

https://github.com/mongodb/mongo-python-driver

Driver

Authentication& Security Python<->BSON Error handling &

Recovery

WireProtocol

Topology Management Connection Pool

Page 22: Back to Basics Webinar 3: Introduction to Replica Sets

22

Start MongoClient

c = MongoClient( "host1, host2", replicaSet="replset" )

Page 23: Back to Basics Webinar 3: Introduction to Replica Sets

23

Client Side View

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MongoClient( "host1, host2", replicaSet="replset" )

Page 24: Back to Basics Webinar 3: Introduction to Replica Sets

24

Client Side View

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2

{ ismaster : False, secondary: True, hosts : [ host1, host2, host3 ] }

Page 25: Back to Basics Webinar 3: Introduction to Replica Sets

25

What Does ismaster show?

>>> pprint.pprint( db.command( "ismaster" )){u'hosts': [u'JD10Gen-old.local:27017', u'JD10Gen-old.local:27018', u'JD10Gen-old.local:27019'], u'ismaster' : False, u'secondary': True, u'setName' : u'replset',…}>>>

Page 26: Back to Basics Webinar 3: Introduction to Replica Sets

26

Topology

Current Topology ismaster New

Topology

Page 27: Back to Basics Webinar 3: Introduction to Replica Sets

27

Client Side View

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

Page 28: Back to Basics Webinar 3: Introduction to Replica Sets

28

Client Side View

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

Page 29: Back to Basics Webinar 3: Introduction to Replica Sets

29

Client Side View

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Page 30: Back to Basics Webinar 3: Introduction to Replica Sets

30

Next Is Insert

c = MongoClient( "host1, host2", replicaSet="replset" )client.db.col.insert_one( { "a" : "b" } )

Page 31: Back to Basics Webinar 3: Introduction to Replica Sets

31

Insert Will Block

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Insert

Page 32: Back to Basics Webinar 3: Introduction to Replica Sets

32

ismaster response from Host 1

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Insert

ismaster

Page 33: Back to Basics Webinar 3: Introduction to Replica Sets

33

Now Write Can Proceed

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Insert

Insert

Page 34: Back to Basics Webinar 3: Introduction to Replica Sets

34

Later Host 3 Responds

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Page 35: Back to Basics Webinar 3: Introduction to Replica Sets

35

Steady State

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Page 36: Back to Basics Webinar 3: Introduction to Replica Sets

36

Life Intervenes

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Page 37: Back to Basics Webinar 3: Introduction to Replica Sets

37

Monitor may not detect

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Insert

ConnectionFailure

Page 38: Back to Basics Webinar 3: Introduction to Replica Sets

38

So Retry

Secondaryhost2

Secondaryhost3

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Insert

Page 39: Back to Basics Webinar 3: Introduction to Replica Sets

39

Check for Primary

Secondaryhost2

Secondaryhost3

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Insert

Page 40: Back to Basics Webinar 3: Introduction to Replica Sets

40

Host 2 Is Primary

Primaryhost2

Secondaryhost3

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Insert

Page 41: Back to Basics Webinar 3: Introduction to Replica Sets

41

Steady State

Secondaryhost2

Secondaryhost3

Primaryhost1

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Page 42: Back to Basics Webinar 3: Introduction to Replica Sets

42

What Does This Mean? - Connect

import pymongo

client = pymongo.MongoClient()

try: client.admin.command( "ismaster" )except pymongo.errors.ConnectionFailure, e : print( "Cannot connect: %s" % e )

Page 43: Back to Basics Webinar 3: Introduction to Replica Sets

43

What Does This Mean? - Queries

import pymongo

def find_with_recovery( collection, query ) : try:

return collection.find_one( query )

except pymongo.errors.ConnectionFailure, e :

logging.info( "Connection failure : %s" e ) return collection.find_one( query )

Page 44: Back to Basics Webinar 3: Introduction to Replica Sets

44

What Does This Mean? - Inserts

def insert_with_recovery( collection, doc ) : doc[ "_id" ] = ObjectId() try: collection.insert_one( doc ) except pymongo.errors.ConnectionFailure, e: logging.info( "Connection error: %s" % e ) collection.insert_one( doc ) except DuplicateKeyError: pass

Page 45: Back to Basics Webinar 3: Introduction to Replica Sets

45

What Does This Mean? - Updates

collection.update( { "_id" : 1 }, { "$inc" : { "counter" : 1 }})

Page 46: Back to Basics Webinar 3: Introduction to Replica Sets

46

Configuration

connectTimeoutMS : 30sserverTimeoutMS : 30s

Page 47: Back to Basics Webinar 3: Introduction to Replica Sets

47

connectTimeoutMS

Secondaryhost2

Secondaryhost3

MongoClient

MonitorThread 1

MonitorThread 2 ✔

MonitorThread 3

YourCode

Insert

connectTimeoutMS

serverTimeoutMS

Page 48: Back to Basics Webinar 3: Introduction to Replica Sets

48

More Reading

• The spec author Jess Jiryu Davis has a collection of links and his better version of this talkhttps://emptysqua.re/blog/server-discovery-and-monitoring-in-mongodb-drivers/

• The full server discovery and monitoring spec is on GitHubhttps://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst

Page 49: Back to Basics Webinar 3: Introduction to Replica Sets

Q&A