PHP solves the problems of high-concurrency congestion inventory prevention and control, such as flash sales, flash sales, building snatching, and lottery.

Source: Internet
Author: User
Tags flock
: This article mainly introduces PHP's ideas and methods for preventing and controlling excessive blocking high-concurrency inventory, such as flash sales, flash sales, building snatching, and lottery. if you are interested in PHP tutorials, please refer to it. In the e-commerce industry, SeckillingFlash sales are already a common promotion method for sellers. However, the inventory quantity is limited while the order is placed at the same time. Number of studentsIf the inventory is exceeded, the product oversold or even the inventory becomes negative.
For example, buying train tickets, building blocks in forums, drawing prizes, and comment on Weibo may also cause high concurrency problems. If you do not take any measures, it may cause server paralysis in high moments. how can this problem be solved?
Here are some ideas and methods that I personally think are feasible:

Solution 1: Use messages QueueTo achieve

It can be based on messages such as MemcacheQ. QueueThe specific implementation scheme is described as follows:
For example, if you have 100 tickets, you can put these 100 tickets in the cache and do not lock them during read/write operations. When the concurrency is large, about 500 people may have successfully snatched the ticket, so that requests after 500 can be directly forwarded to the static page after the activity ends. 500 out of the 400 people entered cannot obtain goods. Therefore, you can QueueOnly the first 100 people can successfully purchase the order. The next 400 people go directly to the activity end page. Of course, only one example is provided for 500 people. you can adjust the number by yourself. The activity end page must use static pages instead of databases. This reduces the pressure on the database.

Solution 2: when there are multiple servers, it can be implemented in the form of shunting

Assume that there are m tickets, n product servers receive requests, and x request routing servers randomly forward
Directly allocate m/n tickets to each product server
Memory of each product server CounterFor example, allow m/n * (1 + 0.1) individuals to come in.
When memory CounterFull:
The person next to the activity directly jumps to the static page after the activity ends,
Inform the routing server that it is not routed to this server (this is worth discussing ).
M/n * (1 + 0.1) people who come in from all product servers will be forwarded to one payment server. when the payment process starts, the number of people will be small, locking is simple.

Solution 3: Use the Memcache lock to implement a single server.

Product_key is the key of the ticket
Product_lock_key is the ticket lock key.
When product_key exists in memcached, all users can enter the order process.
When you enter the payment process, first store add (product_lock_key, "1") in memcached ″),
If the return is successful, go to the payment process.
If not, it indicates that someone has entered the payment process, and the thread waits for N seconds to recursively execute the add operation.

Solution 4: exclusive file lock

When processing the order request, use flock to lock a file. if the lock fails, other orders are being processed. at this time, either wait or directly prompt the user "The server is busy"
This article will talk about 4th solutions. the code is as follows:

Blocking (wait) mode:

 



Non-blocking mode:

 

The above describes PHP's ideas and methods to deal with excessive congestion-type high-concurrency inventory prevention and control measures such as flash sales, flash sales, building snatching, and lottery, including queue, flash sales, number of people, and counter content, if you are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.