With the rise of web2.0, the application of high concurrency large data volume is becoming more and more obvious, and the traditional relational database is weak in this aspect. A spear has its own shield, and the presence of a memory db compensates for the shortcomings of the traditional relational db. The current popular memory db mainly has Redis, Memcach, MongoDB. The preceding two are stored in key-value form, and MongoDB is based on some of the characteristics of relational database tables and supports indexing. MongoDB is a good choice for scenarios where there is a requirement for large data volumes and data correlation.
Replica set is a copy cluster scheme for MongoDB, which is superior to the traditional master-slave method of database. Traditional master-slave mode, Master is responsible for reading and writing, Slaver is responsible for synchronizing data from Master, once the master down, slaver is obsolete, this way in disaster preparedness, and the replica set of MongoDB cluster mechanism to solve this flaw.
Replica Set:
Mainly divided into: primary (master node, provide additions and deletions of the service), Slaver (Spare node, only provide read), arbiter (quorum node, not store data, only responsible for arbitration).
Process: Client reads and writes data from primary node, slaver synchronizes data from primary,arbiter when primary down A healthy slaver replacement primary will be selected from many slaver nodes within 10 seconds to mitigate the disaster . Arbiter The node itself does not store data, but only monitors the operation of primary and slaver in the cluster (if the arbiter goes down, the entire cluster is wasted, the only disadvantage ). Slaver only provides read function, cannot write, our project query request can go to connect slaver node, thus greatly reduce the load of primary main node.
The following is a flowchart of the Replica Set :
Replica Set Principle We understand, you may ask, when we are programming, for primary, slaver so many db, we must be to the primary node to write data, if the primary node downtime, How should the program be detected, and how to find a new primary node?
Don't worry, MongoDB has solved your doubts. MONGODB provides driver support for all languages, just call the Replica set interface and use it with reference to the instructions below, with the 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
}
Collection 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 operations
Db.open (function (err, db) {
var collection = db.collection (' user ');
Querying a document
Collection.findone ({
Name: ' Jerry '
}, function (err, results) {
Console.info (' query: ', results);
});
Insert a document
Collection.insert ({
Name: ' OK ',
Age:28
}, function (err, results) {
Console.info (' Insert: ' + results);
});
});
The above configuration of a few nodes 9001, 9002, 9003, we do not have to pay attention to which is the main node, standby node, blanking node, the driver will automatically determine a healthy master node to give node, we just concentrate on writing the database operation logic can be.
But there is a problem here, Replica set in the Switch node, there will be a suspended period, we know that node is asynchronous/O, in this suspended period, if node in a large number of operations, the weak stack memory overflow, reported: Rangeerror:maximum call Stack size exceeded error, this error is a system-level error, will cause the app to collapse, even if the catch exception or when the DB switch is complete, the program will still hang dead where. No solution has been found, the MONGO-driven API is being researched, attempting to resolve an event that reflects the state of the switching process, and if the event is triggered, stop the DB operation and resume after the switch is complete, which should solve the problem.
node. js MongoDB Replset