preface

This article is a follow-up to Understanding Distributed Transactions: The Ultimate Consistent Implementation of Distributed Transactions.

The same e-commerce demand, a business scenario after the payment of an order, has the following operations:

  1. Change the status of the order to paid
  2. Deduct goods inventory
  3. Add points to members
  4. Create outgoing order to inform warehouse of shipment

We use the final consistency scheme to achieve this.

What is final consistency?

Ensure that the data is consistent in the end.

To reduce the system cost, if the intermediate node fails to process the data, the other nodes do not automatically roll back the data, but process the failed data through the retry mechanism and manual participation to ensure the consistency of the data.

Implementation scheme

Use local message tables + background tasks + message queues + interface idempotency.

Local message table: a local message table added to the corresponding service database. This table stores the messages generated by services and ensures the consistency between service data and message data through local transactions. For example: Msg_published and MSG_received represent the published and received message tables, where there is a status to indicate whether the business has been successfully executed.

Background task: If a service fails to be executed exists in the message table, the background task will retry based on the configured retry policy. For example, if a message fails to be sent or consumed, the background task will retry for three times. After three times, the retry poll is entered. Retry will begin 4 minutes after sending and consuming messages fail to avoid possible problems with setting message status delays; By default, the system tries again every one minute. The maximum number of retries is 50 by default. When this number reaches 50, the system does not retry.

Message queues: Invocations across services use message queues to decouple services.

Interface idempotency: The interface must ensure that the results of one or more requests initiated by the same operation are consistent.

Code implementation

C# open source project CAP is recommended for your reference.

This project only supports C# code to integrate, if other languages can refer to its design ideas, and then carry out a simple implementation.

summary

This article is purely to introduce jade, if you have any questions, welcome criticism.

Do you have a better way to do it? Welcome to leave a message

Recommended reading

  • Answer two frequently asked code writing questions
  • Based on user feedback, two new features have been added to the open source project GO-Gin-API
  • Share my technical solution on the transfer of order status in e-commerce system (attached source code)
  • How do I write Git Commit message?