When expectations disappear, fear arises.
——Gracian
outline

Case introduction
1. Business analysis
Simulate the [order] and [Payment] businesses in the shopping scenario of e-commerce websites

(1) Place an order
The user requests the order system to place an order
The order system calls the order service to place an order through RPC
The order service calls the inventory service to deduct inventory
The order service calls the coupon service and deducts the coupon
Confirm the order after the order payment is completed
(2) Pay
User request payment system
The payment system calls the third-party payment platform API to initiate the payment process
After the user pays successfully through the third-party payment platform, the third-party payment platform will call back and notify the payment system
The payment system calls the order service to confirm the order
problem analysis
1. Distributed system downtime
The whole system is distributed deployment, including order system, commodity system and member system. The three systems complete the whole order placing process through RPC call. RPC call will lead to the coupling of systems in the order. If the member system goes down, it will lead to the unavailability of the order process.
How to decouple asynchronously:
Using rocketmq, after placing an order, the order system writes the “order message” into MQ as a producer, and the commodity system and member system act as the “order message” in MQ for consumer consumption. This can achieve the purpose of asynchronous decoupling. As long as the order system is normal, the order business can be carried out normally for users.
2. Data integrity
After the user submits the order, the inventory and coupons are deducted successfully, but the order confirmation operation fails (for example, payment fails), then the inventory and coupons need to be returned.
How to ensure data integrity?
After receiving the order confirmation failure notification, send a “confirmation failure message” to MQ
The commodity system and the member system listen to the “confirmation failure message” as consumers and return the corresponding business.
3. Invalid orders, peer attacks!
The user pays through the third party payment platform (Alipay, WeChat). If the order is placed, and it has not been paid for a long time, the order should be cancelled. Therefore, this place involves a time limited order scenario. If the system fails to pay successfully within 15 minutes, it will need to cancel the order and return the inventory and coupons.
4. Spike and rush buying peak process impact
Project code introduction
1. Project engineering



2. Database

(1) Order form

(2) Commodity list

(3) User table

(4) Coupon form

3. Framework and technology
The overall project is built with springboot and integrates mybatis. Maven relies on the following:

Springboot integrates rocketmq and uses gson as a sequence tool for MQ sending and consumption

In addition, use mybaits Gengerator reverse engineering to generate curd persistence layer code for the data table


Link:https://pan.baidu.com/s/1gCOLPI76hu1IJwLJnVOdfgExtraction code: rc2r
I amRaojiang_ Chi Meng, let persistence become a habit. Thank you for your:give the thumbs-up、Collectionandcomment, I’ll see you next time!
Previous:Rocketmq common problem analysis and performance optimization