Talking about PHP's solution for high-traffic flash sales and talking about php flash sales

Source: Internet
Author: User
Tags flock

Talking about PHP's solution for high-traffic flash sales and talking about php flash sales

A Real-time countdown display of hours, minutes, And seconds is required. The modification date and time on the user side does not affect the normal display of the Countdown (that is, the server time prevails ).

In fact, this is the same as the Time Limit Function of many examination systems.

You cannot use ajax to retrieve server time every second, so real-time countdown must be implemented using javascript. This is a simple example.

The problem is to solve the impact of the date and time modified by the client on our display.

The solution is to calculate the time difference between the client time and the server time, so that the problem is solved.

In this way, you only need to run php once, and the real-time countdown time is synchronized with the server.

The theory is synchronous, but the actual test will have an error of 1 second. (The specific reason is that it is related to the network speed, and the faster the network speed, the smaller the error), but this will never affect our above requirements.

Note: The second kill time ranges from PM to PM.

The Code is as follows:

<? Php // php time is calculated in seconds. The js time is calculated in milliseconds as date_default_timezone_set ('prc'); // date_default_timezone_set ("Asia/Hong_Kong "); // region // configure the daily activity period $ starttimestr = "08:00:00"; $ endtimestr = "22:00:00"; $ starttime = strtotime ($ starttimestr ); $ endtime = strtotime ($ endtimestr); $ nowtime = time (); if ($ nowtime <$ starttime) {die ("activity hasn't started yet, activity time is: {$ starttimestr} to {$ endtimestr} ") ;}$ lefttime = $ endtime-$ nowtime; // The actual remaining time (seconds)?> <Script language = "JavaScript"> </script> 

There are no problems above, but there are some problems with the wrong quantity at the traffic Conference, such as the negative inventory caused by large-volume concurrent warehouse receiving.

We know that SQL statements are processed in a database. Assume that the process for purchasing a product is as follows:

Sql1: Query product inventory

If (inventory quantity> 0) {// generate order... Sql2: inventory-1}

When there is no concurrency, the above process looks so perfect. Assume that two people place orders at the same time, and there is only one inventory. In sql1stage, the inventory queried by two people is greater than 0, therefore, SQL 2 is executed in the end, and the inventory is changed to-1 at the end. If it is sold out, you can either fill the inventory or wait for the user to complain.

A popular solution to this problem:

1. when an additional single process is used to process a queue and place an order request in the queue, there will be no concurrency issues after each process. However, additional background processes and latency issues are not considered.

2. optimistic database lock, which roughly means to query the inventory first, then immediately add inventory + 1, and then query the inventory before updating the inventory after the order is generated, check whether the inventory quantity is consistent with the expected quantity. If the inventory quantity is inconsistent, the system will roll back, prompting you that the inventory is insufficient.

3. Based on the update results, we can add a condition for SQL 2, update... Where inventory> 0. If false is returned, the inventory is insufficient and the transaction is rolled back.

4. with the exclusive file lock, when processing the order request, use flock to lock a file. If the lock fails, it indicates that 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

<? Php $ fp = fopen ("lock.txt", "w +"); if (flock ($ fp, LOCK_EX )){//.. process Order flock ($ fp, LOCK_UN);} fclose ($ fp);?>

Non-Blocking Mode

<? Php $ fp = fopen ("lock.txt", "w +"); if (flock ($ fp, LOCK_EX | LOCK_NB )){//.. process the order flock ($ fp, LOCK_UN);} else {echo "the system is busy. Please try again later";} fclose ($ fp);?>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.