微服务中的分布式事务 [英] Distributed transactions in microservices

查看:85
本文介绍了微服务中的分布式事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个微服务S1S2. S1调用S2来更新数据,然后S1插入另一个数据,但是让我们考虑S1失败,然后我们需要回滚S2更新的数据,否则我们将处于不一致状态.

I have 2 microservices S1 and S2. S1 invokes S2 to update a data and then S1 inserts another data,But let's consider S1 fails,Then we need to rollback the data updated by S2 or else we'll be in inconsistent state.

我也经历了Saga模式,它将满足这种矛盾之处

I also gone through Saga patterns.will it satisfy this inconsistency

有人可以为此提出任何更好的解决方案吗?

Can anyone suggest any better solutions for this?

推荐答案

在大多数情况下,分布式事务都存在问题,并且对服务不利

Distributed transactions are problematic for most circumstances and they are bad for services

  • 服务边界–服务边界是信任边界.原子 交易需要持有锁并代表他们持有 外交部门正在开设一个安全漏洞(这使得 进行拒绝服务攻击)您不能假设两者之间的原子性 两个不同的实体或资源. Esp.这些资源何时属于 到不同的业务.
  • 事务引入了时间和操作上的紧密耦合
  • 交易阻碍了可扩展性–这不是您无法扩展 但这要困难得多
  • Service Boundary – service boundary is a trust boundary. Atomic transactions require holding locks and holding them on behalf of foreign service is opening a security hole (makes it much easier to do a denial of service attack) You cannot assume atomicity between two different entities or resources. Esp. when these resources belong to different businesses.
  • Transactions introduce tight coupling both temporal and operational
  • Transactions hinder scalability – It isn’t that you can’t scale but it is much harder

Sagas(顺便说一句,不需要进行编排)作为一种协调解决方案而出现,因为它们使服务更灵活-实际上更接近现实生活的工作方式.您可以将Sagas与Sagas结合使用以帮助延迟效果的另一种模式是预订.

Sagas (which, by the way, do not necessitate orchestration) emerged as a solution for coordination because they allow services to be more flexible - and are in fact closer to how real life work. Another pattern you can combine with Sagas to help with delaying effects can be reservation.

您可能要考虑的另一种选择是重新考虑如何对服务进行分区.可能是您现在所拥有的服务边界不正确,并且重新设计会将所需的事务包含到一项服务中

Another option you have might be reconsidering how you partitioned your services. It might be that the service boundaries you have now are not correct and a redesign will contain the needed transaction into one service

这篇关于微服务中的分布式事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆