MongoDB Data Synchronization

MongoDB replica set (V3.0) synchronizes member status information through heartbeat information. Each node periodically sends heartbeat information, such as the replica set status information shown in the rs.status() method, to other members in the replica set.

The node initiating the heartbeat request is called the source, and the member receiving the heartbeat request is the target. A heartbeat request is divided into three phases.

  1. The source sends a heartbeat request to the target.
  2. The target handles the heartbeat request and sends a response to the source.
  3. The source receives a heartbeat response and updates the status of the target node.

Let us examine the main state synchronization logic in these three phases.

Phase 1

In the default configuration, nodes of a replica set send a heartbeat request to the other members once every two seconds, namely the replSetHeartbeat command request. The content of the heartbeat request is similar to the one shown below (obtained through mongosniff packet capturing). It mainly contains the replSetName, the address of the heartbeat sending node, and the replica set version.

command: replSetHeartbeat database: admin metadata: { $replData: 1 } commandArgs: { replSetHeartbeat: "mongo-9552", pv: 1, v: 22, from: "10.101.72.137:9552", fromId: 3, checkEmpty: false }

Phase 2

When a member in the replica set receives a heartbeat request, it begins processing the request and returns the processing result to the requesting node.

If the node is not of the replica set mode, or the replica set name does not match, an error response will be returned. If the replica set version configured (the content of rs.conf()) for the source node is lower than that of the target node, the target node adds its own configuration to the heartbeat response message, and adds its own oplog and other status information to the heartbeat response message. If the target node is uninitialized, it immediately sends the heartbeat request to the source node to update its replica set configuration.

commandReply: { ok: 1.0, time: 1460705698, electionTime: new Date(6273289095791771649), e: true, rs: true, state: 1, v: 22, hbmsg: "", set: "mongo-9552", opTime: new Date(6272251740930703361) } metadata: { $replData: { term: -1, lastOpCommitted: { ts: Timestamp 1460372410000|1, t: -1 }, lastOpVisible: { ts: Timestamp 0|0, t: -1 }, configVersion: 22, primaryIndex: 2, syncSourceIndex: -1 } }

Phase 3

Phase 3 is the most important part of processing. After receiving the heartbeat response, the source node will update the status of the peer node according to the response message and determine whether a re-election is required based on the final status.

When an error response to the heartbeat request is received (a response timeout is also considered an error response), if the current number of retries is fewer than or equal to kMaxHeartbeatRetries (two by default), and the last heartbeat request was sent within kDefaultHeartbeatTimeoutPeriod (10 by default), the next heartbeat request will be sent immediately. When the number of retries exceeds kMaxHeartbeatRetries, or a period of kDefaultHeartbeatTimeoutPeriod has elapsed since the last heartbeat, the node is considered down. If the replica set version of the peer node is higher than that of the node itself, the configuration of the node will be updated and stored persistently in the local database, and the node will update the peer status information based on the response message. If the node itself is the master node, and it finds another node with a higher priority level has been elected as the master node, it takes the initiative to downgrade itself to a slave node. If the node itself is a slave node but finds that it has a higher priority level and is eligible to be elected as a master node, it will take the initiative to request the current master node to downgrade. (This logic still contains some bugs, so self-downgrading by the master node will take priority so as to ensure that the node with the highest priority can act as the master node). If there is no master node at the moment, the node will take the initiative to trigger an election. A new master node can then be elected after a majority of nodes agree with the election result.

Conclusion

MongoDB synchronizes information between nodes through heartbeats and triggers the election to achieve final consistency in the replica set.

However, there is no theoretical basis for the correctness of the process. In MongoDB 3.2, a new version of the replica set communication protocol is used and election is conducted through raft, which can further shorten the time for fault discovery and instance restoration.

时间: 2024-10-22 09:27:39

MongoDB Data Synchronization的相关文章

How Should I Backup MongoDB Databases?

Abstract: MongoDB replica set is composed of a group of MongoDB instances (processes), including one primary node and multiple secondary nodes. All the data on the MongoDB Driver (client) is written to the primary node, and the secondary node synchro

Six Reasons to Embrace MongoDB 3.x Over MongoDB 2.x

Are you still deciding if you should upgrade to the next MongoDB version? This article is lists five reasons why we think you should switch from MongoDB 2.x to MongoDB 3.x. MongoDB 3.x has been a welcome addition to the world of growing databases. Th

Mongodb的安装配置

下载软件,版本 mongodb-linux-x86_64-2.0.2.tgz 解压缩后的目录:mongodb-linux-x86_64-2.0.2 然后转移到目录 /usr/local/mongodb 建立相应的数据存放目录和日志目录 mkdir  /usr/local/mongodb/{data,log} 简单的启动服务 执行/usr/local/mongod 就可以了,但是这样一但退出终端,服务业就停止了 以后台方式运行 /usr/local/mongod  --dbpath=/usr/lo

MongoDB数据库简介和安装

1.安装准备 [root@chen download]# tar zxvf mongodb-linux-x86_64-2.2.0.tgz [root@chen download]# cp mongodb-linux-x86_64-2.2.0 /usr/local/mongodb -r 2.创建数据库文件夹与日志文件 [root@chen download]# mkdir /usr/local/mongodb/data [root@chen download]# touch /usr/local/

MongoDB 内存使用情况分析

MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似json的bson 格式,因此可以存储比较复杂的数据类型.Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数 据库单表查询的绝大部分功能,而且还支持对数据建立索引. 先 ps 一下看

Master-Slave Synchronization for MySQL

Abstract MySQL is the world's most popular open-source database. As an important part of LAMP, a combination of open-source software (Linux + Apache + MySQL + Perl/PHP/Python), MySQL is widely used in various applications. Chinese Internet forum syst

Increasing the OpLog Size in MongoDB(don't need full resync)

OpLog用于MongoDB数据库复制场合,特征: 1. 设置容量上限, 2. 循环使用 3. local数据库专用 对于MongoDB来说设置好适当的OpLog上限是非常有必要的,因为加大OpLog Size需要重启Master和Slave数据库,并且Slave数据库需要重新同步(full sync). 如果不做FULL SYNC,看看会出现什么样的情况: MASTER: [root@db6 data1]# /opt/mongodb-linux-x86_64-1.6.4/bin/mongo 1

Install MongoDB Community Edition on Windows

网上关于安装MongoDB的中文介绍坑太多,请参考下面的官方文档,原文地址 Overview Use this tutorial to install MongoDB Community Edition on Windows systems. PLATFORM SUPPORT Starting in version 2.2, MongoDB does not support Windows XP. Please use a more recent version of Windows to us

[Mongodb]安装与启动

Windows   推荐下载版本1.4.3(Windows 32 bit) 下载链接:http://downloads.mongodb.org/win32/mongodb-win32-i386-1.4.3.zip 假设安装路径在E:\mongodb,设置Mongodb数据库的数据路径E:\data\mongodb 开启命令行(开始--运行)   输入以下命令: 1. mongod命令建立一个mongodb数据库链接,数据存放路径为E:\data\mongodb E: cd mongodb\bin