Node. js mongodb ReplSet, node. jsmongodb

Source: Internet
Author: User

Node. js mongodb ReplSet, node. jsmongodb

With the rise of web, applications with high concurrency and large data size have increasingly obvious performance requirements for Rapid database response. traditional relational databases are somewhat weak in this regard. The emergence of memory DB makes up for the shortcomings of traditional relational databases. Currently, the most popular memory databases in the market include redis, memcach, and mongodb. The first two are stored in key-value format, while mongodb is based on some features of relational database tables and supports indexing. Therefore, mongodb is a good choice when there are requirements for large data volumes and data correlation.

Replica Set is a Replica cluster solution of mongodb, which is superior to the traditional master-slave database mode. In the traditional master-slave mode, the master node is responsible for reading and writing data, and the slaver node is responsible for synchronizing data from the master node. Once the master node is down, slaver will be abolished. This mode has defects in disaster recovery, mongodb's Replica Set cluster mechanism solves this defect.

Replica Set:

It can be divided into: primary (master node, which provides addition, deletion, query, modification, and modification services), slaver (slave node, which only provides read), and arbiter (Arbitration node, which does not store data and is only responsible for arbitration ).

Process: the client reads and writes data from the primary node, and slaver synchronizes data from the primary node. When the primary is down,ArbiterA healthy Server Load balancer node will be selected from multiple Server Load balancer nodes within 10 seconds to replace primary, which reduces the disaster..ArbiterThe Node itself does not store data, but only monitors the running status of primary and slaver in the cluster (ifArbiterWhen the cluster goes down, the whole cluster will be deprecated. The only drawback is that). Slaver only provides the READ function and cannot write. we can connect to the slaver node for project query, which greatly reduces the load on the primary master node.

Below isReplica SetFlowchart:


Replica SetWe understand the principle. You may ask, during programming,For primary, slaverSo many databases, we must write data to the primary node. If the primary node goes down, how should the program detect it? How can we find the new primary node?

Don't worry. mongodb has solved your questions. Mongodb provides driver support for various languages. You only need to callReplica Set interface, and then use it according to the instructions. The following uses node. js


Var Db = require ('mongodb '). Db,

Server = require ('mongodb '). Server,
ReplSet = require ('mongodb '). ReplSet;

// Cluster Server address
Var serverAddr = {
9001: '192. 168.1.100 ', // Node 1
9002: '192. 168.1.100 ', // Node 2
9003: '192. 168.1.100 '// Node 3
}

// Set of cluster Sever objects
Var servers = [];
For (var I in serverAddr ){
Servers. push (new Server (serverAddr [I], parseInt (I )));

}


Var replStat = new ReplSet (servers ,{});

Var db = new Db ('blog ', replStat );

// Mongodb operation
Db. open (function (err, db ){
Var collection = db. collection ('user ');
// Query a document
Collection. findOne ({
Name: 'Jerry'
}, Function (err, results ){
Lele.info ('query: ', results );
});
// Insert a document
Collection. insert ({
Name: 'OK ',
Age: 28
}, Function (err, results ){
Lele.info ('insert: '+ results );
});

});


Several nodes 9001, 9002, and 9003 are configured above. We do not need to pay attention to the master node, slave node, or blanking node. The driver will automatically determine a healthy master node for the node, we only need to concentrate on writing the databaseOperationLogic.

However, there is a problem here. When the Replica Set switches nodes, there will be a break period. We know that the node is asynchronous/O. During this break period, if the node is performing a large number of operations, the weak stack memory overflows and reports the following error: RangeError: Maximum call stack size exceeded. This error is a system-level error that causes the app to collapse, even if the capture exception or wait for the db switch to complete, the program will still be suspended. Currently, no solution has been found. We are studying the mongo-driven api and trying to solve it by an event that reflects the monitoring status of the switching process. If this event is triggered, the db operation is stopped, the problem can be solved after the switchover is completed.


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.