SlideShare a Scribd company logo
Back to Basics German 3: Einführung in Replica Sets
Back to Basics 2017 : Webinar 3
Einführung in Replica Sets
Benjamin Lorenz
Senior Solutions Architect
MongoDB Frankfurt
3
Zusammenfassung von Teil 1 und Teil 2
• Warum NoSQL
• Unterschiedliche Typen von NoSQL-Datenbanken
• MongoDB: Detaillierte Übersicht
• Datenbankkonzepte
• Installieren von MongoDB
• Erstellen einer einfachen Blogging-Anwendung
• Hinzufügen eines Index
• Abfrageoptimierung mit explain()
4
Ablauf heute
• Dauerhaftigkeit (durability) von Daten
• Der MongoDB-Ansatz: Das Replica Set
• Replica Set-Lebenszyklus
• Programmierung mit einem Replica Set
5
HighAvailability and Data Durability – Replica Sets
SecondarySecondary
Primary
6
Replica Set Creation
SecondarySecondary
Primary
Heartbeat
7
Replica Set Node Failure
SecondarySecondary
Primary
No Heartbeat
8
Replica Set Recovery
SecondarySecondary
Heartbeat
And Election
9
New Replica Set – 2 Nodes
SecondaryPrimary
Heartbeat
And New Primary
10
Replica Set Repair
SecondaryPrimary
Secondary
Rejoin and resync
11
Replica Set Stable
SecondaryPrimary
Secondary
Heartbeat
Developing with Replica Sets
13
Driver Responsibilities
https://github.com/mongodb/mongo-python-driver
Driver
Authentication
& Security
Python<->BSON
Error handling &
Recovery
Wire
Protocol
Topology
Management
Connection Pool
14
Strong Consistency
SecondarySecondary
Primary
Client Application
Client Driver
Write
Read
15
Eventual Consistency
SecondarySecondary
Primary
Client Application
Client Driver
Write
16
Write Concerns
• Network acknowledgement
• Wait for error
• Wait for journal sync
• Wait for replication
17
Write Concern : 0 (Unacknowledged)
18
Write Concern : 1 (Acknowledged)
19
Write Concern : majority
20
Driver Responsibilities
https://github.com/mongodb/mongo-python-driver
Driver
Authentication
& Security
Python<->BSON
Error handling &
Recovery
Wire
Protocol
Topology
Management
Connection Pool
21
Start MongoClient
c = MongoClient( "host1, host2",
replicaSet="replset" )
22
Client Side View
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
MongoClient( "host1, host2",
replicaSet="replset" )
23
Client Side View
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
{ ismaster : False,
secondary: True,
hosts : [ host1, host2, host3 ] }
24
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',
…}
>>>
25
Topology
Current
Topology
ismaster
New
Topology
26
Client Side View
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
27
Client Side View
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
28
Client Side View
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
29
Next Is Insert
c = MongoClient( "host1, host2",
replicaSet="replset" )
client.db.col.insert_one( { "a" : "b" } )
30
Insert Will Block
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
Insert
31
ismaster response from Host 1
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
Insert
ismaster
32
Now Write Can Proceed
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
Insert Insert
33
Later Host 3 Responds
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
34
Steady State
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
35
Life Intervenes
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
✖
36
Monitor may not detect
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
✖
Insert
ConnectionFailure
37
So Retry
Secondary
host2
Secondary
host3
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
✖
Insert
38
Check for Primary
Secondary
host2
Secondary
host3
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
✖
Insert
39
Host 2 Is Primary
Primary
host2
Secondary
host3
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
✖
Insert
40
Steady State
Secondary
host2
Secondary
host3
Primary
host1
Mongo
Client
Monitor
Thread 1
Monitor
Thread 2
Monitor
Thread 3
Your
Code
41
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 )
42
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 )
43
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 )
try:
collection.insert_one( doc )
except DuplicateKeyError:
pass
44
What Does This Mean? - Updates
collection.update( { "_id" : 1 },
{ "$inc" : { "counter" : 1 }})
45
More Reading
• The spec author Jess Jiryu Davis has a collection of links and his better
version of this talk
https://emptysqua.re/blog/server-discovery-and-monitoring-in-mongodb-
drivers/
• The full server discovery and monitoring spec is on GitHub
https://github.com/mongodb/specifications/blob/master/source/server-
discovery-and-monitoring/server-discovery-and-monitoring.rst
Q&A

More Related Content

PPTX
Back to Basics German 3: Einführung ins Sharding
PPT
Использование MongoDB как основной метабазы в UGC-сервисах
PPT
Introduction to MongoDB (Webinar Jan 2011)
PPTX
Using MongoDB For BigData in 20 Minutes
PDF
Restinio (actual aug 2018)
PPTX
Mango Database - Web Development
PPTX
Back to Basics 2017 - Introduction to NoSQL
PDF
進階使用Nodejs 淺談no sql(mongodb)
Back to Basics German 3: Einführung ins Sharding
Использование MongoDB как основной метабазы в UGC-сервисах
Introduction to MongoDB (Webinar Jan 2011)
Using MongoDB For BigData in 20 Minutes
Restinio (actual aug 2018)
Mango Database - Web Development
Back to Basics 2017 - Introduction to NoSQL
進階使用Nodejs 淺談no sql(mongodb)

What's hot (18)

PDF
MongoDB Mojo: Building a Basic Perl App
KEY
MongoDB Hadoop DC
PDF
An introduction to MongoDB
PPTX
Mongodb tutorial by Rajendra Arora
PPTX
Introduction to MongoDB and CRUD operations
PPTX
MongoDB Introduction - Document Oriented Nosql Database
PPT
Architecture & Functionality for Learning Spaces Website
PPT
4 exercises for part 1
PPTX
Introduction to mongo db
PDF
Create a RESTful API with NodeJS, Express and MongoDB
PDF
Fluentd: Unified Logging Layer at CWT2014
DOCX
การใช้งาน Scroll bar component
PDF
Easy access to open stack object storage
KEY
MongoDB Strange Loop 2009
PPTX
MongoDB by Emroz sardar.
PPTX
Mongo db1
PPTX
Minio Red Herring
KEY
MongoDB EuroPython 2009
MongoDB Mojo: Building a Basic Perl App
MongoDB Hadoop DC
An introduction to MongoDB
Mongodb tutorial by Rajendra Arora
Introduction to MongoDB and CRUD operations
MongoDB Introduction - Document Oriented Nosql Database
Architecture & Functionality for Learning Spaces Website
4 exercises for part 1
Introduction to mongo db
Create a RESTful API with NodeJS, Express and MongoDB
Fluentd: Unified Logging Layer at CWT2014
การใช้งาน Scroll bar component
Easy access to open stack object storage
MongoDB Strange Loop 2009
MongoDB by Emroz sardar.
Mongo db1
Minio Red Herring
MongoDB EuroPython 2009
Ad

Similar to Back to Basics German 3: Einführung in Replica Sets (20)

PPTX
Back to Basics Webinar 3: Introduction to Replica Sets
PPTX
Webinar Back to Basics 3 - Introduzione ai Replica Set
PPTX
EuroPython 2016 : A Deep Dive into the Pymongo Driver
PPTX
Back to Basics Spanish Webinar 3 - Introducción a los replica sets
PPTX
Replication and replica sets
PDF
Evolution of MongoDB Replicaset and Its Best Practices
PDF
Evolution Of MongoDB Replicaset
PDF
Mongodb replication
PDF
Mdb dn 2016_09_34_features
DOCX
MongoDB Replication and Sharding
PDF
MongoDB Database Replication
PPTX
Get expertise with mongo db
PPTX
Practical Replication June-2011
PPT
High Availabiltity & Replica Sets with mongoDB
PDF
Replication MongoDB Days 2013
KEY
MongoDB Administration ~ Kevin Hanson
PPTX
Python mongo db-training-europython-2011
PPTX
Mongodb connection string
PPTX
Server discovery and monitoring with MongoDB
PDF
Exploring the replication in MongoDB
Back to Basics Webinar 3: Introduction to Replica Sets
Webinar Back to Basics 3 - Introduzione ai Replica Set
EuroPython 2016 : A Deep Dive into the Pymongo Driver
Back to Basics Spanish Webinar 3 - Introducción a los replica sets
Replication and replica sets
Evolution of MongoDB Replicaset and Its Best Practices
Evolution Of MongoDB Replicaset
Mongodb replication
Mdb dn 2016_09_34_features
MongoDB Replication and Sharding
MongoDB Database Replication
Get expertise with mongo db
Practical Replication June-2011
High Availabiltity & Replica Sets with mongoDB
Replication MongoDB Days 2013
MongoDB Administration ~ Kevin Hanson
Python mongo db-training-europython-2011
Mongodb connection string
Server discovery and monitoring with MongoDB
Exploring the replication in MongoDB
Ad

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...

Recently uploaded (20)

PDF
Salesforce Agentforce AI Implementation.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
AutoCAD Professional Crack 2025 With License Key
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Cost to Outsource Software Development in 2025
PDF
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
PDF
Nekopoi APK 2025 free lastest update
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
Salesforce Agentforce AI Implementation.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
AutoCAD Professional Crack 2025 With License Key
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Design an Analysis of Algorithms I-SECS-1021-03
Reimagine Home Health with the Power of Agentic AI​
wealthsignaloriginal-com-DS-text-... (1).pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Cost to Outsource Software Development in 2025
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
Nekopoi APK 2025 free lastest update
Designing Intelligence for the Shop Floor.pdf
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Monitoring Stack: Grafana, Loki & Promtail
Digital Systems & Binary Numbers (comprehensive )
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Computer Software and OS of computer science of grade 11.pptx

Back to Basics German 3: Einführung in Replica Sets

Editor's Notes

  • #3: Who I am, how long have I been at MongoDB.
  • #14: Present a native language interface - converts python types to BSON objects Convert the JSON query language into commands for the database Convert JSON data into BSON data and vice-versa Handles interfacing to different MongoDB topologies Helps recover from server side outages/network errors Manages the client side connection pool The pymongo driver code is on Github (Apache License)
  • #21: Present a native language interface - converts python types to BSON objects Convert the JSON query language into commands for the database Convert JSON data into BSON data and vice-versa Handles interfacing to different MongoDB topologies Helps recover from server side outages/network errors Manages the client side connection pool The pymongo driver code is on Github (Apache License)
  • #23: Calls i
  • #24: Calls i
  • #26: State machine, full set of states defined in spec.
  • #27: Calls i
  • #28: Calls i
  • #29: Calls i
  • #31: Needs a primary to complete a write.
  • #32: Needs a primary to complete a write.
  • #33: Needs a primary to complete a write.
  • #34: Needs a primary to complete a write.
  • #35: Each thread wakes every 10 seconds. Runs ismaster, sleeps. We use ismaster to check latency. Keep topology description up to date.
  • #36: Each thread wakes every 10 seconds. Runs ismaster, sleeps. We use ismaster to check latency. Keep topology description up to date.
  • #37: Each thread wakes every 10 seconds. Runs ismaster, sleeps. We use ismaster to check latency. Keep topology description up to date.
  • #38: Primary is marked as unknown Wakes up all monitor threads to check for a primary.
  • #39: Primary is marked as unknown Wakes up all monitor threads to check for a primary every half second.
  • #40: Primary is marked as unknown Wakes up all monitor threads to check for a primary every half second.
  • #41: Each thread wakes every 10 seconds. Runs ismaster, sleeps. We use ismaster to check latency. Keep topology description up to date.
  • #43: Try once. This will accomdate elections. Other errore should be propagated.
  • #44: Try once. This will accomdate elections. Other errore should be propagated.
  • #45: Can you afford to over or under count. Operations need to be idempotent. Turn an update into a write of a document, cf EventSourcing. Then aggregate on the server.