Snapping, second-kill is now a very common application scenario, there are two main issues to solve:
1 high concurrency pressure on the database
2 How to deal with the correct reduction of inventory ("oversold" issue) under competitive status
For the first question, it is easy to think of caching to handle snapping, avoiding direct manipulation of databases, such as Redis.
The focus is on the second question.
General wording:
Check the inventory of the corresponding commodity, see if it is greater than 0, and then perform operations such as generating orders, but in determining whether the inventory is greater than 0, if there is a problem in high concurrency, resulting in a negative inventory volume
Optimization Scenario 1: Set the Inventory field Number field to unsigned, when inventory is 0 o'clock, because the field cannot be negative, it will return false
Optimization Scenario 2: Use MySQL transactions to lock the rows of the operation
Optimization Scenario 3: Using non-blocking file exclusive locks
Optimization Scenario 4: Use Redis queue, because the pop operation is atomic, even if there are many users to arrive at the same time, it is recommended to use (MySQL transaction under high concurrency performance degradation is very bad, file lock way also)
Snapping, describing logic
Analog 5000 High concurrency test
Webbench-c 5000-t http://192.168.1.198/big/index.php
Ab-r-N 6000-c-http://192.168.1.198/big/index.php
The above is just a simple simulation of high concurrency under the snapping, real scene is much more complicated than this, a lot of attention to the place
If the snapping page is made static, the interface is called via Ajax
Again as above will cause a user to rob multiple, thinking:
A queued queue and snapping result queue and inventory queue are required. High concurrency, the user first into the queued queue, with a thread loop processing from the queued queue to remove a user, determine whether the user has snapped the results queue, if in, then has snapped, otherwise not snapped, inventory minus 1, write the database, the user into the result queue.
Test Data Sheet
(Advanced article) PHP combined with Redis for high-concurrency snapping, seconds kill function