Replica set (Replica set):
In layman's words, a replica set is a cluster, master-slave replication, read-write separation, and failover.
The replica set is an upgraded version of MongoDB's original master-slave mode, and it is no longer recommended to use master-slave mode.
The replica set of MongoDB differs from master to subordinate, and the master and slave will stop after the host goes down, while the replica set will take over the master node to become the primary node after the host outage, and there will be no downtime.
MongoDB requires a minimum of two nodes for replication. One is the master node, which handles client requests, and the rest is the slave node, responsible for replicating the data on the master node.
MongoDB each node common collocation method is: A master one from, a master many from.
The master node records all operations on it, oplog the primary node periodically from the node, and then performs these operations on its own copy of the data, ensuring that the data from the node is consistent with the primary node.
The MONGODB replication structure diagram looks like this:
Ps: This picture reads and writes from the master node, can actually set the read operation to walk from the node, but the write operation can only be on the primary node.
Replica set Features:
- N-node clusters
- Any node can be used as the master node
- All write operations are on the primary node
- Auto Fail-Over
- Automatic recovery
Replica set Master node
In a replication set, the primary node is the only node that can receive write requests. MongoDB writes on the master node and logs these operations to the master node's oplog . The slave node copies the Oplog to its native computer and applies the actions to its own dataset.
before the master node is re- regression after failover copy set     not available   successful in   The application from the node is down. When the primary node is re-joined to a replica set from a node, it will " Roll back " and the write operation will be consistent with the rest of the members of the replication set.
Copy set read and write:
From the client application's point of view, the MongoDB instance is running as a single server (that is, "standalone") or the replica set is transparent. However, MongoDB provides additional read and write configurations for the replica set.
The write operation of the replica set references the Writeconcern configuration.
Read operation reference for replica set: Readconcern configuration.
Read operations on a replica set:
The read operation on a replica set from a node member is not guaranteed to reflect the current state of the primary node, since the state of the node falls behind the primary node for a period of time. In many cases, the application does not depend on this type of strict consistency, but the application developer should always consider the application's requirements before setting the replication set read option.
Configure which node to return data from:
We need to choose carefully whether or not to read from the node, unlike the read operation in primary , the data returned when reading from a node may not be The latest data on the master node.
Using Java configuration:
Readpreference settings are involved in the replica set replica set, by default, both read and write are distributed primary node execution, but for less read, we want to read and write separate to share the pressure, So I want to use the secondary node to read, primary only assume the responsibility of writing (actually write can only be distributed to primary node, not modifiable).
MongoDB has 5 modes of readpreference:
Master node, default mode, read operation only in the primary node, if the primary node is not available, error or throw an exception.
The primary node is preferred, in most cases read operations on the primary node, if the primary node is unavailable, such as failover, read operations on the slave node.
From the node, the read operation is only in the slave node, if the slave node is unavailable, an error is raised, or an exception is thrown.
It is preferred to read operations from nodes, in most cases, from nodes, special cases (such as single master node schemas) on the master node.
The nearest Neighbor node, the read operation in the nearest member, may be the primary node or the slave node.
Settings in spring readpreference:
- <!--MongoDB Configuration--
- <MONGO:MONGO id="MONGO" host="${mongo.host}" port="${mongo.port}" write-concern="NORMAL" >
- <MONGO:options
- connections-per-host="${mongo.connectionsperhost}"
- threads-allowed-to-block-for-connection-multiplier="${ Mongo.threadsallowedtoblockforconnectionmultiplier} "
- connect-timeout="${mongo.connecttimeout}"
- max-wait-time="${mongo.maxwaittime}"
- auto-connect-retry="${mongo.autoconnectretry}"
- socket-keep-alive="${mongo.socketkeepalive}"
- socket-timeout="${mongo.sockettimeout}"
- slave-ok="${mongo.slaveok}"
- write-number="1"
- write-timeout="0"
- write-fsync="false"
- />
- </MONGO:MONGO>
- <!--MONGO Factory, through which to get MONGO instances, dbname for MongoDB database name, no words will be automatically created--
- <MONGO:db-factory id="Mongodbfactory" dbname="Uba" mongo-ref="Mongo"/>
- <!--read-write separation level configuration-
- <!--preferred primary node, mostly read operations on the primary node, if the primary node is unavailable, such as failover, read operations on the slave node. -
- <Bean id= "primarypreferredreadpreference" class=" Com.mongodb.TaggableReadPreference.PrimaryPreferredReadPreference "/>
- <!--nearest neighbor, read operations in the nearest member, possibly the primary node or from the node. -
- <Bean id= "nearestreadpreference" class=" Com.mongodb.TaggableReadPreference.NearestReadPreference "/>
- <!--from the node, read the operation only in the slave node, if the slave node is not available, error or throw an exception. The problem is that the data of the secondary node is older than the primary node data. -
- <Bean id= "secondaryreadpreference" class=" Com.mongodb.TaggableReadPreference.SecondaryReadPreference "/>
- <!--read from the secondary node first, and the secondary node reads data from the master node when it is unavailable--
- <Bean id= "secondarypreferredreadpreference" class=" Com.mongodb.TaggableReadPreference.SecondaryPreferredReadPreference "/>
- <!--MongoDB's main operating object, all the additions and deletions to MongoDB are done through it-
- <Bean id= "mongotemplate" class="Org.springframework.data.mongodb.core.MongoTemplate" >
- <constructor-arg name="mongodbfactory" ref="mongodbfactory"/>
- <property name= "readpreference" ref="primarypreferredreadpreference"/>
- </Bean>
You can actually set the readpreferenceof an operation in detail to implement the query API.
MongoDB Replica Set