(Advanced article) PHP combined with Redis for high-concurrency snapping, seconds kill function

Source: Internet
Author: User

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

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.