Home > Database > Mysql Tutorial > body text

How to make the project more concurrency? Use ID auto-increment to ensure queuing order

零下一度
Release: 2017-05-06 14:57:07
Original
1790 people have browsed it

Scenario Analysis

Here we take the red envelope grabbing scenario as an example. The requirements are as follows:

    1.红包有个数限制,假设红包的个数限制为X。
    2.红包金额上线限制,假设金额上线为Y。
    3.要求用户抢红包的时候,不超过红包的个数限制X。
    4.要求用户抢红包的时候,不超过红包的金额Y。
    5.每个用户一次红包活动只能抢一个。
Copy after login

Conventional ideas

Here are the most common ideas:

    1.在用户抢红包时,检查当前发出去红包数量和金额,并加锁。
    2.检查红包数量和金额正常的后,随机用户红包金额。
    3.然后修改红包发出去的数量和金额,并给用户赠送红包,然后解锁。
Copy after login

Advantages and disadvantages of conventional ideas

First are the advantages

    1.思路简单
    2.编不下去了。。。
Copy after login

Then are the disadvantages

    1.锁数据回造成大量进程等待,造成浪费资源。
    2.锁造成的等待,用户体验奇差。
    3.对于锁机制不太了解的同学会产生一定的危险性。
Copy after login

Optimization Ideas

First analyze, why is the conventional idea slow?

    1.在抢红包的时候,每次都需要检查红包的上限 X 和 Y。
    2.锁会造成大量进程卡顿。
    3.生成红包的金额时还需要检查与上限 X 跟 Y 是否有冲突。
Copy after login

Optimization solution

Prerequisites for red envelope generation

For example, the upper limit of the number of red envelopes is X and the upper limit of the amount is Y.
Then, before the event, I insert these X red envelopes into the database
and generate serial numbers: HB1, HB2, HB3. . . . HBX

So in fact, users will only need to receive this orderly red envelope queue in order.
This operation reduces a lot of calculations that will occur online at that time. Most importantly, it can ensure the controllability of the entire activity simply and effectively.

Use ID auto-increment to ensure queuing order

An ID generation table is used here, and a unique index of user_id is established to ensure that each person can only get one serial number.

The steps to grab red envelopes are as follows

    1.活动创建之前,创建一张ID生成表,ID从 1 开始自增,且 user_id 唯一。
    2.活动开始,用户开始抢红包操作。
    3.抢红包之前,先插入ID表,获取插入ID,如果ID > X,通知用户已被抢完。
    4.如果 ID <= X,那么恭喜了,去红包表领取序号为 ID 的红包,并走异步发红包过程。
    5.活动结束之后,把相关用户领取信息存储在红包表,删除ID生成表。
Copy after login

Advantages of the scheme

    1.不需要代码实现锁机制。
    2.逻辑简单。
    3.mysql保证每个用户只能拿到一个,且有序。
Copy after login

More thoughts

Some friends mentioned that redis queues can be used to store red envelope information, but in practice Redis takes up more memory. If you need to store data for a long time, it is best to put it in mysql. In fact, you can use the incr command of redis here to get a function similar to the ID generation table mentioned above, which is faster and strictly incremental, and can make the entire project more concurrency.

【Related recommendations】

1. Free mysql online video tutorial

2. MySQL latest manual tutorial

3. Those things about database design

The above is the detailed content of How to make the project more concurrency? Use ID auto-increment to ensure queuing order. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template