WeChat Red Envelopes Promotion

Last updated: 2020-02-26 19:58:23


The red packet activity on the Spring Festival Gala relates to the linkage of four large systems, including WeChat, WeChat Pay, Red Packet and Tenpay.

Red Packet system: It is responsible for the sending, scrambling and opening of personal red packets, and list viewing;
Tenpay system: It is responsible for order payment, high-performance storage for asynchronous incoming payment journal, and real time display of users' balance and bill;
WeChat Access: It ensures the quality of users' access to the public network;
WeChat Pay: It is the entry to online transaction.

! [ 112046] mc.qcloudimg.com/static/img/0eb560566c1f67e302e75b1dcb515d98/image.png)

Distributed transactions similar to red packet system are the focus of attention. To take a typical example, "user A sends a red packet of 10 yuan to user B", there are the following steps:

1) Read the balance in account A
2) Execute subtraction on account A (subtract 10 CNY)
3) Write the result back to account A (verify the result once)
4) Read the balance in account B
5) Open the red packet sent by account A and read the value
6) Execute addition on account B (add 10 CNY)
7) Write the result into account B

In order to ensure the consistency of the data, there are only two results of the above steps: both completed successfully or neither of them successfully performed the rollback. And in the process of this operation, the distributed locking mechanism needs to be introduced to A and B accounts to avoid the problem of dirty data. In WeChat red packet's huge distributed cluster, things will become extremely complicated.

Tencent Cloud CMQ is introduced into WeChat red packet system to avoid the overhead of distributed transactions. In the same scenario where A user sends 10 CNY red packets to B user, the new strategy after the introduction of CMQ is described below.

  • In step 7 of the above case, user B opened the red packet with 10 yuan in it. When doing the final accounting operation, due to the great pressure of Concurrence on that day, the accounting failure often occurred.
  • The red packet team transfers all failed requests to CMQ. When user B fails to update the account balance, the mobile client displays the waiting status. Then the account system will continue to pull and retry the update operation from CMQ. CMQ guaranteed that the 10-yuan entry would never be lost until it was taken out.
  • On the same day of New Year's Eve, the actions of sending, dismantling and recording red packets by users were transformed into massive requests at the level of one billion. If the traditional transaction method is used, the pressure of Concurrence will be magnified and the system will collapse.
  • CMQ message queue ensures the reliable storage and delivery of red packet messages, and writes three copies in real time to ensure that the data is not lost. When the funds fail, you can retry many times to avoid the disadvantages of traditional methods such as failure rollback and frequent polling of the database.