Learning record of mongodb Master Course (day 17)

Time:2020-2-17

Written in front

Recently, there are new projects online, which is too busy. The progress of learning is a little slow, but I will stick to it.

Seventeenth days

Today I want to learn the chapter “20 transaction development: read one of the operational transactions”. It mainly explains how to read data.

Readrreference parameter

There are 5 values, as shown in the figure below. The default value is primary, that is, read data from the primary node.
Learning record of mongodb Master Course (day 17)

Example of application scenario

Take E-Commerce orders as an example, dry goods.

  • After placing an order, the user will be transferred to the order details page – primary / primarypreferred. At this time, the node may not be copied to the new order;
  • Users query the orders they have placed — secondary / secondarypreferred. Querying Historical orders usually does not require much timeliness;
  • Generate reports – secondary. Reports have low timeliness requirements, but large resource requirements. They can be processed separately at slave nodes to avoid impact on online users;
  • Distribute the pictures uploaded by users all over the world, so that users everywhere can read the nearest. The application in each region selects the nearest node to read data.

Control multiple nodes

This part can control which group to read from by labeling nodes.
Learning record of mongodb Master Course (day 17)

Readpreference configuration

Connection string parameters through mongodb:

  • mongodb://host1:27107,host2:27107,host3:27017/?replicaSet=rs&readPreference=secondary

Through the mongodb driver API:

  • MongoCollection.withReadPreference(ReadPreference readPref)

Mongo Shell:

  • db.collection.find({}).readPref( “secondary” )

Readpreference experiment: read from node

• write {X: 1} to the master node, and observe that the data is visible to each node

• perform dB. Fsynclock() on two slave nodes to lock writes (synchronization)

• master writes {X: 2}

• db.test.find({a: 123})
• db.test.find({a: 123}).readPref(“secondary”)

• unlock db.fsyncunlock() from node

• db.test.find({a: 123}).readPref(“secondary”)

Matters needing attention

  • You should also be aware of high availability when specifying readpreference. For example, if readpreference is specified as primary, there will be no node readable during failover when primary does not exist. If the business allows, you should select primary preferred;
  • The same problem will be encountered when using tag. If only one node has a specific tag, no node will be readable when this node fails. This is sometimes the expected result, sometimes not. For example:

    • If the node used by the report fails, even if no report is generated, it is not usually desirable to transfer the report load to other nodes. At this time, it is a reasonable choice that only one node has a report tag;
    • If the online node fails, you usually want to have an alternative node, so you should keep multiple nodes with the same tag;
  • Tag sometimes needs to be considered together with priority and voting right. For example, the node that makes the report usually does not want it to be the master node, so the priority should be 0.

Last

That’s all for today, continue tomorrow.

Recommended Today

Python basics Chinese series tutorial · translation completed

Original: Python basics Python tutorial Protocol: CC by-nc-sa 4.0 Welcome anyone to participate and improve: a person can go very fast, but a group of people can go further. Online reading Apache CN learning resources catalog introduce Seven reasons to learn Python Why Python is great Learn Python introduction Executing Python scripts variable character string […]