官术网_书友最值得收藏!

Transactions

Each microservice can use a database of its choice. The chosen databases may or may not have the ACID property (https://en.wikipedia.org/wiki/ACID) and support transactions. This is one of the reasons why distributed transactions are hard to implement with microservices. However, business transactions involving changes across multiple business entities cannot be omitted entirely, and therefore microservices implement distributed transactions by using data workflows, as shown in the following diagram:

Microservices publish events whenever they make a change to the database. The events contain the type of change along with immutable data about the business entities that were affected by this change. Other services then listen to these events asynchronously and perform the changes strictly in the order in which events were published. A single transaction may contain one or more events that may result in cascading events generated by the microservices that are affected by it. Due to the asynchronous nature of the event flow, the consistency achieved across microservices in this case is eventual (https://en.wikipedia.org/wiki/Eventual_consistency).

If a transaction fails, the service that encounters the failure generates compensatory events to nullify the changes made across microservices that have already processed the transaction events in the chain. The compensatory events flow backwards towards the origin of the transaction, as shown in the preceding diagram. Compensatory events are idempotent in nature and retried until they succeed.

The transaction pattern for microservices is inspired by   Sagas ( http://www.cs.cornell.edu/andru/cs711/2002fa/reading/sagas.pdf)  and was proposed by Hector Garcia-Molina and Kenneth Salem, as published in an ACM paper in 1987. Sagas may be implemented as a set of workflows, where at each step of failure a compensating action is triggered to bring the system back to its original state as it was before the workflow was triggered.
主站蜘蛛池模板: 临夏县| 福州市| 安阳市| 拜城县| 清丰县| 平武县| 酉阳| 阿拉善左旗| 山阳县| 承德市| 安新县| 大连市| 仪陇县| 巢湖市| 温州市| 鱼台县| 塔城市| 民丰县| 景泰县| 化州市| 富裕县| 镇赉县| 焉耆| 琼海市| 葫芦岛市| 株洲县| 土默特左旗| 神池县| 长乐市| 云浮市| 潍坊市| 福安市| 武冈市| 钦州市| 太白县| 聂拉木县| 呼玛县| 宣化县| 高州市| 友谊县| 资兴市|