Wie Sie ein Cluster für eine Produktionsumgebung erstellen
Wie Sie ein Replica Set anlegen
Wie MongoDB für die Datenpersistenz sorgt und wie ein Replica Set den Betrieb nach einem Ausfall automatisch wieder aufnimmt
What Goes Wrong with Language Definitions and How to Improve the Situation
Back to Basics German 3: Einführung in Replica Sets
1.
2. Back to Basics 2017 : Webinar 3
Einführung in Replica Sets
Benjamin Lorenz
Senior Solutions Architect
MongoDB Frankfurt
3. 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. 4
Ablauf heute
• Dauerhaftigkeit (durability) von Daten
• Der MongoDB-Ansatz: Das Replica Set
• Replica Set-Lebenszyklus
• Programmierung mit einem Replica Set
41. 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. 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. 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. 44
What Does This Mean? - Updates
collection.update( { "_id" : 1 },
{ "$inc" : { "counter" : 1 }})
45. 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
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)
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)
Calls i
Calls i
State machine, full set of states defined in spec.
Calls i
Calls i
Calls i
Needs a primary to complete a write.
Needs a primary to complete a write.
Needs a primary to complete a write.
Needs a primary to complete a write.
Each thread wakes every 10 seconds. Runs ismaster, sleeps.
We use ismaster to check latency.
Keep topology description up to date.
Each thread wakes every 10 seconds. Runs ismaster, sleeps.
We use ismaster to check latency.
Keep topology description up to date.
Each thread wakes every 10 seconds. Runs ismaster, sleeps.
We use ismaster to check latency.
Keep topology description up to date.
Primary is marked as unknown
Wakes up all monitor threads to check for a primary.
Primary is marked as unknown
Wakes up all monitor threads to check for a primary every half second.
Primary is marked as unknown
Wakes up all monitor threads to check for a primary every half second.
Each thread wakes every 10 seconds. Runs ismaster, sleeps.
We use ismaster to check latency.
Keep topology description up to date.
Try once. This will accomdate elections. Other errore should be propagated.
Try once. This will accomdate elections. Other errore should be propagated.
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.