


How to make the project more concurrency? Use ID auto-increment to ensure queuing order
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.每个用户一次红包活动只能抢一个。
Conventional ideas
Here are the most common ideas:
1.在用户抢红包时,检查当前发出去红包数量和金额,并加锁。 2.检查红包数量和金额正常的后,随机用户红包金额。 3.然后修改红包发出去的数量和金额,并给用户赠送红包,然后解锁。
Advantages and disadvantages of conventional ideas
First are the advantages
1.思路简单 2.编不下去了。。。
Then are the disadvantages
1.锁数据回造成大量进程等待,造成浪费资源。 2.锁造成的等待,用户体验奇差。 3.对于锁机制不太了解的同学会产生一定的危险性。
Optimization Ideas
First analyze, why is the conventional idea slow?
1.在抢红包的时候,每次都需要检查红包的上限 X 和 Y。 2.锁会造成大量进程卡顿。 3.生成红包的金额时还需要检查与上限 X 跟 Y 是否有冲突。
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生成表。
Advantages of the scheme
1.不需要代码实现锁机制。 2.逻辑简单。 3.mysql保证每个用户只能拿到一个,且有序。
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!

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

The Spring framework manages concurrency through two mechanisms: thread pool and asynchronous processing: Thread pool: Use the ThreadPoolTaskExecutor class to configure the core and maximum number of threads and queue capacity. Asynchronous processing: Use the @Async annotation to mark methods so that the method can be executed asynchronously in a separate thread without the need to manually manage threads.

With the rapid development of information technology, programming languages have become an indispensable part of today's world. Among many programming languages, Go language is very popular because of its unique advantages and characteristics. This article will deeply explore the advantages and characteristics of Go language, hoping to bring readers a clearer understanding. 1. Simple and easy-to-use syntax The syntax design of Go language is simple and clear, making the code easier to read and maintain. It abandons some cumbersome grammatical rules, focuses on simplicity and intuitiveness, and lowers the threshold for language learning. This makes the Go language more capable during the development process

In the Python world, the GIL (Global Interpreter Lock) has always been a barrier to limiting concurrency. It forces the Python interpreter to execute only one thread at a time, hindering utilization of multi-core processors and limiting program throughput. However, as the Python ecosystem has grown, several techniques have emerged to bypass the GIL and unlock the potential of Python's concurrency. Coroutines: Lightweight concurrent coroutines are a lightweight concurrency mechanism that allow multiple functions to execute simultaneously without creating separate threads. They do this by pausing and resuming during function execution. The benefits of coroutines include: Lightweight: Coroutines have less overhead than threads. Composability: Coroutines can be easily composed together to create complex concurrent applications

Nature of GIL GIL is a mutex used to serialize access to underlying CPython interpreter objects within the python interpreter. It ensures thread safety by preventing multiple threads from modifying shared data structures simultaneously. When a thread acquires the GIL, it has exclusive access to the interpreter, and other threads must wait until the GIL is released. Impact of the GIL The GIL has a significant impact on Python's concurrency. Since only one thread can execute bytecode at the same time, when one thread is running intensive calculations, other threads will be blocked and unable to execute concurrently. This is especially problematic on multi-core systems, since only one core can be utilized to execute Python code. Demonstration of the GIL The following code demonstrates the GIL pair

Go language (also known as Golang) is a programming language developed by Google. It is designed to be a concise, efficient and concurrency-rich programming language. In server-side development, Go language has many unique features and advantages. This article will explore the characteristics of Go language in server-side development and demonstrate its advantages through specific code examples. Features: 1. Strong concurrency: Go language has built-in lightweight goroutine and channel mechanisms, which makes concurrent programming more convenient.

As a statically typed, compiled programming language, Go language is widely used in the fields of network programming and distributed systems. It has many important features. This article will analyze the important features of the Go language and illustrate it with specific code examples. 1. Concurrent programming support Go language inherently supports concurrent programming, and the combination of goroutine and channel makes concurrent programming simpler and more efficient. Goroutine is a lightweight thread that can run functions or methods concurrently. Pass the pass

Java functions handle large amounts of data efficiently through lazy evaluation: data is evaluated only when needed, avoiding unnecessary loading and processing. Take full advantage of multi-core processors using multithreading and concurrency: Use ExecutorService and CompletableFuture to manage concurrency. With serverless platforms like Google Cloud Functions, challenges can be addressed without the need to manage servers.

Concurrency and Traditional Blocking Programming In traditional blocking programming, when a task waits for an I/O operation (such as reading a file or a network request), the entire program is blocked until the operation is completed. This can limit the efficiency of your application, especially when dealing with a large number of I/O-intensive operations. asyncio's event loop asyncio introduces the concept of an event loop that continuously monitors various I/O events. When an event is detected, the event loop places the appropriate callback function into the event queue. These callback functions are called coroutines, and they represent tasks that can be suspended. Coroutines and Asynchronous Programming Coroutines are the core concept of asyncio. They are lightweight, pauseable and resumeable tasks. Unlike threads, coroutines are in the same thread
