In recent years, with the increasing popularity of big data, the importance of non-relational databases has been recognized by more and more people, and more developers have gradually joined the NoSQL camp. We know that NoSQL means Not Only SQL. In this case, the features supported by many relational databases are also applicable to non-relational data, such as replica sets.
MongoDB supports data replication. It has the same advantages as other data replication sets. It deploys data on multiple different servers, to prevent data loss due to a single machine failure, data redundancy is used to improve data reliability and security. The replication technology can also be used to build distributed databases to improve system access performance and security.
The replica set mode of MongoDB is master-slave replication. Among all database service machines, only one machine plays the Primary role, and the other machines are Secondaries. The machine that assumes the Primary role receives all Write operation requests from the client and completes the operation to ensure data consistency. The machine that assumes the Primary role can also allocate Read operations from the client to other machines (Secondaries) to relieve the pressure on the master database server.
So how does MongoDB replicate data on various machines?
It turns out that the Primary database of MongoDB records these operations in the oplog log file every time it updates the data. The Secondaries database reads and executes logs locally through asynchronous operations to generate data consistent with Primary.
When the Primary database is unavailable, the replica set selects a machine from Secondaries as the Primary machine. By default, the client reads data from the Primary machine, but the client can also specify the copy from which the data is read.
We can also configure an arbitration server. Arbitration does not manage replica sets, nor can it become Primary. It only determines which replica sets have the right to become Primary.
The above is the main content of the MongoDB replica set. If you have any questions, please leave a comment.