This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
Implementation of Saga
In the ordering scene of an e-commerce platform, it involves the creation of orders, deduction of goods inventory, wallet payment, bonus gift and other operations. The overall sequence diagram is as follows:
Electric business platform order process is a typical long transaction scenario, based on the definition of Saga pattern, first the long transaction split into multiple local short transactions, local affairs according to the order of execution of each service one by one to submit, once one of the service transaction anomalies, withdraw compensation mode is adopted, the implementation of this process involves coordination model of the Saga, It has two common coordination modes.
- Event choreography: The decision and execution sequence logic of a Saga is distributed among each Saga participant, who communicates by exchanging events.
In compiling pattern based on events, the first service execution after a local transaction, send a time, the event will be one or more service to monitor, listening to the time of service and launch a new event to perform local affairs, continues to serve this event trigger mode, until the last of the business process to service local transactions executed over, This is equivalent to the completion of the entire distributed long transaction.
The process is complex, but the common solution is as follows:
- The order service creates a new order, sets the order status to be paid, and issues an order_CREATE_event event
- The inventory service listens for the order_CREATE_event event, executes the local inventory freeze method, and, if successful, issues an order_prepareD_Event event
- After listening for the Order_prepareD_Event event, the payment service executes the account debit method and publishes the pay_ORDER_Event event.
- The points service listens for the pay_order_event event, increments account points, and updates the order status to success.
If one of the preceding steps fails to be performed, the system sends a failure time. Each service needs to monitor the failure and roll back one by one based on actual requirements.