当前位置:网站首页>Handling write conflicts under multi-master replication (3) - Convergence to a consistent state and custom conflict resolution logic
Handling write conflicts under multi-master replication (3) - Convergence to a consistent state and custom conflict resolution logic
2022-07-31 16:40:00 【HUAWEI CLOUD】
3.2.3 Convergence to a consistent state
In the master-slave replication model, data updates conform to the sequential principle, that is, if there are multiple updates to the same field, the last write operation will determine the final value of the field.
In the multi-master replication model, since there is no such write order, the final value is also uncertain.In Figure-7:
- The title in master node 1 is first updated to B and then to C
- In master node 2, first update to C, then update to B
The two cannot identify which is "more correct".
If each replica executes in the order in which it sees the writes, the DB will end up in an inconsistent state, such as master 1 seeing the final value C and master 2 seeing B.This is not acceptable, all replication models must at least ensure that the final state of the data is consistent across all replicas.Therefore, the DB must resolve conflicts in a convergent way, which means that all replicas must end up with the same value when all changes are replicated.
3.2.3.1 Convergence conflict resolution
- Give each write a unique ID (such as a timestamp, a long nonce, a UUID, or a hash), pick the write with the highest ID as the winner, and discard the others.If based on timestamps, this technique is called last write wins (LWW, last write wins).Although this method is popular, it is prone to data loss.Discussed in detail later.
- Assign a unique ID to each replica and make rules such that writes to replicas with higher ID numbers always have higher priority.However, data loss may also occur
- Merge the values in some way, such as alphabetically, and then concatenate (Figure-7, the combined title may be something like "B/C")
- Record and retain all conflict-related information in a predefined format, and then rely on application-layer logic to resolve the conflict afterwards (possibly prompting the user)
3.2.4 Custom conflict resolution logic
The most appropriate way to resolve conflicts is to rely on the application layer, so many multi-master replication models have tools that allow application code to resolve conflicts, which can execute these code logic when writing or reading:
p>Execute on write
The conflict handler is called whenever the DB system detects a conflict in replicating the changelog.Such as Bucardo allows to write a piece of Perl code.This handler usually cannot prompt the user online, it can only run as a background process
Execute while reading
When a conflict is detected, all conflicting writes are staged.On the next read, multiple versions of the data are returned to the application layer.The application may prompt the user or resolve the conflict automatically and write the final result back to the DB.Such as CouchDB.
Conflict resolution usually applies to a single line or document, not the entire transaction.Therefore, if an atomic transaction contains multiple different write requests, each write request still needs to be considered separately to resolve conflicts.
What is a conflict?
Some conflicts are obvious, as shown in Figure-7. Two write operations concurrently modify the same field in the same record and set them to two different values.
Other types of conflicts may be subtler.Such as a conference room reservation system, which records who has booked which room at which time period.The app needs to ensure that each room is booked by only one group of people at the same time (no double bookings for the same room).At this point, if two different reservations are created for the same room at the same time, there is a conflict.Although the application checks for room availability when booking, there may be a conflict if the two bookings are made by two different primary nodes.
Automatic conflict resolution
Conflict resolution rules can become increasingly complex, and custom code is prone to errors.Amazon is a classic counter-example: For a while, the conflict resolution logic on the shopping cart relied on the user's cart page (where all the items were saved), but customers sometimes found items that had been removed before reappearing in their cart.
Some interesting research attempts to automatically resolve conflicts due to concurrent modification of data:
- Conflict-free replicated datatypes (CRDT) A series of data structures such as collections, maps, ordered lists, counters, etc. that can be edited by multiple users simultaneously, theyAutomatically resolve conflicts in a reasonable manner.Some CRDTs have been implemented in Riak 2.0
- **Mergeable persistent data structures** explicitly track history, similar to the Git version control system, and use a three-way merge feature (whereas CRDT uses a two-way merge)
- **operational transformation** The conflict resolution algorithm behind collaborative editing applications like Etherpad and Google Docs.Designed for simultaneous editing of ordered lists of items, such as lists of characters that make up a text document
The implementation of these algorithms in databases is still young, but it is likely that they will be integrated into more replicated data systems in the future.Automatic conflict resolution can make it easier for applications to handle multi-leader data synchronization.
Unfortunately, there is no ready-made answer, which will be explained in more depth later.
边栏推荐
- 6-22 Vulnerability exploit - postgresql database password cracking
- 入职一个月反思
- C程序是如何跑起来的01 —— 普通可执行文件的构成
- 智能垃圾桶(九)——震动传感器(树莓派pico实现)
- Qt practical cases (54) - using transparency QPixmap design pictures
- LeetCode_733_图像渲染
- 牛客网刷题(四)
- [7.28] Code Source - [Fence Painting] [Appropriate Pairs (Data Enhanced Version)]
- Handling write conflicts under multi-master replication (4) - multi-master replication topology
- C language - function
猜你喜欢
随机推荐
npm安装时卡在sill idealTree buildDeps,npm安装速度慢,npm安装卡在一个地方不动
Character pointer assignment [easy to understand]
牛客 HJ16 购物单
软件实现AT命令操作过程
宁波大学NBU IT项目管理期末考试知识点整理
tensorflow2.0 cnn(layerwise)
Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
最后写入胜利(丢弃并发写入)
LevelSequence源码分析
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
自动化测试—web自动化—selenium初识
IP协议从0到1
2.索引及调优篇【mysql高级】
华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
【源码解析】BeanFactory和FactoryBean
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
Implementing DDD based on ABP
复杂高维医学数据挖掘与疾病风险分类研究
After the form is submitted, the page does not jump [easy to understand]