MongoDB Replica Set

Source: Internet
Author: User
Tags failover

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:

    • Primary

Master node, default mode, read operation only in the primary node, if the primary node is not available, error or throw an exception.

    • Primarypreferred

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.

    • Secondary

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.

    • Secondarypreferred

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.

    • Nearest

The nearest Neighbor node, the read operation in the nearest member, may be the primary node or the slave node.

Settings in spring readpreference:

  1. <!--MongoDB Configuration--
  2. <MONGO:MONGO id="MONGO" host="${mongo.host}" port="${mongo.port}" write-concern="NORMAL" >
  3. <MONGO:options
  4. connections-per-host="${mongo.connectionsperhost}"
  5. threads-allowed-to-block-for-connection-multiplier="${ Mongo.threadsallowedtoblockforconnectionmultiplier} "
  6. connect-timeout="${mongo.connecttimeout}"
  7. max-wait-time="${mongo.maxwaittime}"
  8. auto-connect-retry="${mongo.autoconnectretry}"
  9. socket-keep-alive="${mongo.socketkeepalive}"
  10. socket-timeout="${mongo.sockettimeout}"
  11. slave-ok="${mongo.slaveok}"
  12. write-number="1"
  13. write-timeout="0"
  14. write-fsync="false"
  15. />
  16. </MONGO:MONGO>
  17. <!--MONGO Factory, through which to get MONGO instances, dbname for MongoDB database name, no words will be automatically created--
  18. <MONGO:db-factory id="Mongodbfactory" dbname="Uba" mongo-ref="Mongo"/>
  19. <!--read-write separation level configuration-
  20. <!--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. -
  21. <Bean id= "primarypreferredreadpreference" class=" Com.mongodb.TaggableReadPreference.PrimaryPreferredReadPreference "/>
  22. <!--nearest neighbor, read operations in the nearest member, possibly the primary node or from the node. -
  23. <Bean id= "nearestreadpreference" class=" Com.mongodb.TaggableReadPreference.NearestReadPreference "/>
  24. <!--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. -
  25. <Bean id= "secondaryreadpreference" class=" Com.mongodb.TaggableReadPreference.SecondaryReadPreference "/>
  26. <!--read from the secondary node first, and the secondary node reads data from the master node when it is unavailable--
  27. <Bean id= "secondarypreferredreadpreference" class=" Com.mongodb.TaggableReadPreference.SecondaryPreferredReadPreference "/>
  28. <!--MongoDB's main operating object, all the additions and deletions to MongoDB are done through it-
  29. <Bean id= "mongotemplate" class="Org.springframework.data.mongodb.core.MongoTemplate" >
  30. <constructor-arg name="mongodbfactory" ref="mongodbfactory"/>
  31. <property name= "readpreference" ref="primarypreferredreadpreference"/>
  32. </Bean>

You can actually set the readpreferenceof an operation in detail to implement the query API.

MongoDB Replica Set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.