"MongoDB" readpreference Read preferences

Source: Internet
Author: User
Tags failover

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:

<!-- 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}" &Nbsp;       write-number= "1"           write-timeout= "0"          write-fsync= "false"      /></mongo:mongo><!-- mongo Factory, through it 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 and 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, may be the primary node or the slave node.   --><bean id= "Nearestreadpreference"  class= " Com.mongodb.TaggableReadPreference.NearestReadPreference " /><!--  slave node, read operation only from node,  if 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 is preferred, and the secondary node reads the data from the master node when it is not available   --><bean id= " Secondarypreferredreadpreference " class=" Com.mongodb.TaggableReadPreference.SecondaryPreferredReadPreference " /><!--the main objects of  mongodb, All of the additions and deletions to MongoDB are performed through it  --><bean id= "Mongotemplate"  class= " Org.springframework.data.mongodb.core.MongoTemplate ">    <constructor-arg name= "Mongodbfactory"  ref= "Mongodbfactory"  />    <property name= " Readpreference " ref=" Primarypreferredreadpreference " /></bean>


This article is from the architect's path blog, so be sure to keep this source http://lizhuquan0769.blog.51cto.com/2591147/1763287

"MongoDB" readpreference Read preferences

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.