


Conjecture on the implementation principle of WeChat red envelopes
The following content comes from the background of the chat records of a high-availability architecture group at QCon: A friend consulted about the architecture of WeChat red envelopes, and the following discussion content was drawn from the explanations and discussions of official or unofficial classmates. During this period, there were many A classmate gave a red envelope to test the algorithm on the current network.
The process of grabbing red envelopes
When someone sends a red envelope to N people in the group, with a total amount of M yuan, what happens in the background is as follows:
1. Background operation for sending red envelopes:
Add a red envelope record in the database, store it in CKV, and set the expiration time;
In Cache (maybe Tencent’s internal kv database, based on memory, It has been implemented and has a kernel-state network processing module to provide services in the form of a kernel module)). Add a record to store the number of people grabbing red envelopes N
2. Backstage operation of grabbing red envelopes:
Grabbing red envelopes It is divided into grabbing and demolishing. The grabbing operation is completed at the Cache layer. The number of red envelopes is decremented through atomic subtraction operation until it reaches 0. This means that they are all robbed. In the end, the actual amount of background teardown operations is not large. Through the separation of operations, invalid requests are directly blocked outside the Cache layer. The atomic subtraction operation here is not an atomic subtraction operation in the true sense, but The CAS provided by the Cache layer keeps trying by comparing version numbers. There is a certain degree of conflict. The conflicting user will be released and allowed to proceed to the next step of disassembly. This also explains why some users grabbed the dismantling operation. The situation when development has been completed.
Opening of red envelopes is completed in the database. The number and amount received are accumulated through the transaction operation of the database, and a claim is inserted. Running water and recording are asynchronous operations, which also explains why red envelopes cannot be seen in the balance after receiving them during the Spring Festival. The amount will be calculated in real time when splitting. The amount is a random number between 1 point and 2 times the remaining average. A total amount A red envelope worth M yuan, the largest red envelope is M * 2 /N (and will not exceed M), when the red envelope is opened, the remaining amount and number will be updated. Tenpay prepares for 200,000 transactions per second, but the actual amount is only 80,000 per second.
FAQ
Since there are atoms reduced when grabbing, shouldn't there be a situation where there is no one after grabbing it and disassembling it?
The atomic subtraction here is not an atomic operation in the true sense. It is the CAS provided by the Cache layer. It tries continuously by comparing the version numbers.
What should I do if the cache and db are down?
Main and backup + reconciliation
Is there any red envelope that has been lost, but the balance is still there?
No, the program will have a take all operation and an asynchronous reconciliation guarantee at the end.
Why do we need to separate grabbing and demolition?
The general idea is to set up multi-layer filters, filter layer by layer, and reduce flow and pressure layer by layer. This design was originally because the grabbing operation is the business layer, and the splitting operation is the accounting operation. One operation is too heavy, and the interruption rate is high. From the interface level, the first interface is a pure cache operation and has strong compression capabilities. A simple query Cache blocks most users and performs the first filtering, so most people will see a prompt that the content has been sold out.
After grabbing the red envelope, send it out or withdraw cash. Is there any strategy here?
Large amount priority deposit strategy
Is there any data to prove whether the probability of each red envelope is equal?
It’s not absolutely equal, it’s just a simple brain-patting algorithm.
With the head-patting algorithm, will there be two best ones?
There will be the same amount, but there is only one lucky one, the one who grabs it first is the best.
Will the money of the person who sends the red envelope be frozen?
It is deducted directly in real time, not frozen.
What are the reasons for calculating the amount in real time?
Real-time efficiency is higher, but budget efficiency is low. The budget also accounts for additional storage. Because the red envelope only occupies one record and is valid for only a few days, it does not require much space. Even when the pressure is high, the horizontal expansion machine is.
Test 2: Zhihu user "Ma Jingcheng"'s experiment:
Here is a survey sample data of 100 samples, and put forward your own guess.
1. The wallet money satisfies the censored normal random number distribution. Roughly speaking, random numbers are taken from a censored normal distribution, and the summed number is divided by the total value to obtain the correction factor, and then the correction factor is multiplied by all the random numbers to obtain the red envelope value.
This distribution means: there are more red envelopes below the average, but not far from the average; there are few red envelopes above the average, but there are more red envelopes that are much larger than the average.
Figure 1. Wallet value and its frequency distribution histogram and its normal fitting
But looking at the distribution histogram does not infer that it conforms to the normal distribution , but considering the simplicity of the program and the rationality of the random numbers, this is the most reasonable guess.
2. The wallets at the back are generally more valuable
Figure 2. The relationship between the wallet serial number and its value
It can be seen from the linear fitting red line in Figure 2 that the overall change trend of wallet value is slowly increasing, and its change range is approximately a "channel" delineated by the upper and lower bounds of the green dotted line. (The curve can be enclosed in such a regular "channel", which also reflects the rationality of Rule 1 from the side and illustrates that random numbers are not uniformly distributed)
From another average This pattern can also be seen in the figure.
Figure 3. Change curve of average number with sequence number
In the sample, a wallet worth 1,000 was Divided into 100 parts, the mean is 10. However, in Figure 3 we can see that before the last wallet, the average has been lower than 10, which shows that the value of the wallet at the beginning is low, and has been pulled upward by the value of the wallet in the later period. The value is higher.
3. Of course, the average graph can also reveal another rule, that is, the last person is often lucky enough to draw more. Because the last person gets whatever is left in their wallet, and the average of everyone before is less than 10, it is at least guaranteed that the last person will be higher than the average. In this sample, wallet number 98 drew 35, while the last wallet drew 46.
In summary, based on the sample guess:
1. Most of the time, the money drawn is as small as others, but once it is more, it will be much easier a lot of.
2. The more you pull out the wallet at the back, the easier it is to make money.
3. The last person is often lucky.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

Gate.io, a leading cryptocurrency trading platform founded in 2013, provides Chinese users with a complete official Chinese website. The website provides a wide range of services, including spot trading, futures trading and lending, and provides special features such as Chinese interface, rich resources and community support.

The OKX trading platform offers a variety of rates, including transaction fees, withdrawal fees and financing fees. For spot transactions, transaction fees vary according to transaction volume and VIP level, and adopt the "market maker model", that is, the market charges a lower handling fee for each transaction. In addition, OKX also offers a variety of futures contracts, including currency standard contracts, USDT contracts and delivery contracts, and the fee structure of each contract is also different.

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

Learn to easily copy Xiaohongshu copywriting! This tutorial teaches you step by step how to quickly copy Xiaohongshu video copy, saying goodbye to tedious steps. Open the Xiaohongshu APP, find the video you like, and click on the [Copywriting] area below the video. Long press the copy text and select the [Extract Text] function from the pop-up options. The system will automatically extract the text, click the [Copy] button in the lower left corner. Open WeChat or other applications, such as Moments, long press the input box, and select [Paste]. Click Send to complete the copy. It's that simple!

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.
