MongoDB Data Synchronization

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: MongoDB replica set (V3.0) synchronizes member status information through heartbeat information. Each node periodically sends heartbeat information to other members in the replica set.

Several_high_availability_MongoDB_cluster_configuration_solutions

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.

相关实践学习
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
27天前
|
NoSQL Java MongoDB
Java一分钟之-Spring Data MongoDB:MongoDB集成
【6月更文挑战第11天】Spring Data MongoDB简化Java应用与MongoDB集成,提供模板和Repository模型。本文介绍其基本用法、常见问题及解决策略。包括时间字段的UTC转换、异常处理和索引创建。通过添加相关依赖、配置MongoDB连接、定义Repository接口及使用示例,帮助开发者高效集成MongoDB到Spring Boot应用。注意避免时间差、异常处理不充分和忽视索引的问题。
40 0
|
2月前
|
NoSQL MongoDB 数据安全/隐私保护
Flink CDC支持MongoDB的CDC(Change Data Capture)连接器
Flink CDC支持MongoDB的CDC(Change Data Capture)连接器
138 4
|
NoSQL MongoDB
《Data as a Service - 数据即服务 -- MongoDB⾼级应⽤模式》电子版地址
Data as a Service - 数据即服务 -- MongoDB⾼级应⽤模式
89 0
《Data as a Service - 数据即服务 -- MongoDB⾼级应⽤模式》电子版地址
|
SQL NoSQL Java
MongoDB--Spring Data MongoDB详细的操作手册(增删改查)
在NoSQL盛行的时代,App很大可能会涉及到MongoDB数据库的使用,而也必须学会在Spring boot使用Spring Data连接MongoDB进行数据增删改查操作,如下为详细的操作手册。
581 1
|
存储 JSON NoSQL
Spring认证中国教育管理中心-Spring Data MongoDB教程四
Spring认证中国教育管理中心-Spring Data MongoDB教程四
Spring认证中国教育管理中心-Spring Data MongoDB教程四
|
SQL NoSQL 关系型数据库
教程:使用Data Lake Analytics读/写MongoDB数据
Data Lake Analytics 作为云上数据处理的枢纽,最近加入了对于MongoDB 的支持, 这篇教程带你玩转 DLA 的 MongoDB 支持。 创建数据库 在 DLA 里面创建一个底层映射到 MongoDB 的外表的语法如下: CREATE DATABASE `mongo_test`.
2846 0
教程:使用Data Lake Analytics读/写MongoDB数据
|
NoSQL Java Spring
spring data mongodb 使用 之 spring boot 2.x
spring data 是一个家族在使用中大同小异 本人写的spring data是通过maven子父工程管理 parent项目的 : pom.xml pom.
2061 0
|
前端开发 NoSQL Java
|
NoSQL Java Spring
spring data mongodb之mongodbTemplate查询总结
常用的列表查询(筛选条件,查询字段,排序及分页),相当于关系型数据库的: select fields from tableName where coditions order by field skip limit; DBObject cond = new BasicDBObject(); //等于条件 cond.
3562 0