Guaranteeing Writes to Multiple Database Nodes

The term node refers to a single instance of the MongoDB daemon process. Typically there is a single MongoDB node per machine, but for testing or development cases you can run multiple nodes on one machine.

Replica Set is the MongoDB term for the database’s enhanced master-slave replication configuration. This is similar to the traditional master-slave replication you find in RDBMS such as MySQL and PostgreSQL in that a single node handles writes at a given time. In MongoDB master selection is determined by an election protocol and during failover a slave is automatically promoted to master without requiring operator intervention. Furthermore, the PyMongo driver is Replica Set-aware and performs automatic reconnect on failure to the new master. MongoDB Replica Sets, therefore, represent a master-slave replication configuration with excellent failure handling out of the box. For anyone who has had to manually recover from a MySQL master failure in a production environment, this feature is a welcome relief.

By default, MongoDB will return success for your write operation once it has been written to a single node in a Replica Set.

However, for added safety in case of failure, you may wish your write to be committed to two or more replicas before returning success. This can help ensure that in case of catastrophic failure, at least one of the nodes in the Replica Set will have your write.

PyMongo makes it easy to specify how many nodes you ...

Get MongoDB and Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.