当前位置:网站首页>分布式事务—基于消息补偿的最终一致性方案(本地消息表、消息队列)
分布式事务—基于消息补偿的最终一致性方案(本地消息表、消息队列)
2022-06-28 01:10:00 【May Hacker】
前言
基于消息补偿的最终一致性方案是一种分布式事务的实现方式,又包括本地消息表、消息队列两种方式。
一、本地消息表
核心思想是将分布式事务拆分成本地事务进行处理,本地消息表实现最终一致性。

- 步骤1和2,系统收到用户下单请求,将订单业务数据写入订单表中,同时把该订单对应的消息数据写入本地消息表中,订单表与本地消息表为同一个数据库,更新订单和存储消息为同一个本地事务,数据库事务处理,要么都成功,要么都失败。
- 步骤345,订单服务发送消息到消息队列,库存服务收到消息,进行库存业务操作,更新库存数据
- 步骤6和7,返回业务处理结果,订单服务收到结果后,将本地消息表中的数据设置完成状态或者删除数据。
- 步骤8,另起定时任务,定时扫描本地消息表,看是否有未完成的任务,有则重试。
优缺点
- 实现简单,开发成本低
- 订单表与本地消息表为同一个数据库
二、支持事务的消息队列
RMQ支持事务消息,与常见的数据库事务流程类似,生产者生产一条事务消息,等生产者本地操作ok了,会向MQ Server发送提交确认。
在这种情况下,事务消息初始被扔到消息队列中是没办法消费的,只有当该事务消息被提交了,这个消息才能被消费者所消费。
同理,如果rollback了,RMQ会从消息队列中把该消息删掉。

- 生产者生产消息给MQ,此时未提交,称该消息为HalfMsg,即半消息
- MQ受到该消息,会给生产者一个OK确认
- 生产者执行本地事务
- 根据本地事务执行的结果向MQ提交commit或者rollback,MQ根据commit或者rollback对消息进行相应操作,即消费或者丢弃
- 如果生产者提交commit或者rollback提交超时,即第四步没有收到来自生产者的请求,MQ回调检查该消息
- MQ回调检查本地事务的状态
参考
https://www.jianshu.com/p/e31d9ebed201
边栏推荐
- ByteDance Interviewer: how to calculate the memory size occupied by a picture
- What if win11 can't drag an image to the taskbar software to open it quickly
- Mysql database foundation: DML data operation language
- 【历史上的今天】6 月 8 日:万维网之父诞生;PHP 公开发布;iPhone 4 问世
- 抓包整理外篇fiddler————了解工具栏[一]
- How fiddle uses agents
- Opencv -- geometric space transformation (affine transformation and projection transformation)
- [2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB
- Snake C language
- [today in history] June 17: the creator of the term "hypertext" was born; The birth of Novell's chief scientist; Discovery channel on
猜你喜欢

【云原生】-Docker安装部署分布式数据库 OceanBase

【历史上的今天】6 月 15 日:第一个手机病毒;AI 巨匠司马贺诞生;Chromebook 发布

Desai wisdom number - histogram (column folding mixed graph): ratio of rental price to rental income in the graduation quarter of 2021

【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线

如何判断线程池已经执行完所有任务了?

【方块编码】基于matlab的图像方块编码仿真

How to run unity webgl after packaging (Firefox configuration)

Writing C program with GCC and makefile for the first time

Initial linear regression

Usage details of staticlayout
随机推荐
【历史上的今天】6 月 5 日:洛夫莱斯和巴贝奇相遇;公钥密码学先驱诞生;函数语言设计先驱出生
Step by step interpretation of crf+bilstm code
[today in history] June 17: the creator of the term "hypertext" was born; The birth of Novell's chief scientist; Discovery channel on
【历史上的今天】6 月 16 日:甲骨文成立;微软 MSX 诞生;快速傅里叶变换发明者出生
数仓的字符截取三胞胎:substrb、substr、substring
Online text batch inversion by line tool
Flask Foundation: template inheritance + static file configuration
【 amélioration de la correction d'image de Code bidimensionnel】 simulation du traitement d'amélioration de la correction d'image de Code bidimensionnel basée sur MATLAB
第三批入围企业公示!年度TOP100智能网联供应商评选
Publicity of the third batch of shortlisted enterprises! Annual Top100 smart network supplier selection
【模糊神经网络】基于matlab的模糊神经网络仿真
【倒立摆控制】基于UKF无迹卡尔曼滤波的倒立摆控制simulink仿真
【历史上的今天】6 月 18 日:京东诞生;网店平台 Etsy 成立;Facebook 发布 Libra 白皮书
[today in history] June 15: the first mobile phone virus; AI master simahe was born; Chromebook launch
2021年软件测试工具总结——模糊测试工具
无心剑汉英双语诗004.《剑》
Get 5 offers after being notified of layoffs
Is it safe for qiniu to open an account? How do I open an account online?
Moving Tencent to the cloud: half of the evolution history of cloud server CVM
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times