For PHP high concurrent under the lottery program, how to avoid the repetition of the winning and the same award in many people?

Source: Internet
Author: User
Tags repetition
Concurrent PHP Sweepstakes

My practice is: First Prize: one, Second Prize: 2, three grade: 3, so there is an award of 6, if 100 people participate in the lottery at the same time, then the empty prize is 100-6 = 94.

First from the database MySQL query out there are 6 unique number of prizes as the redemption number:
SQL1:
$sql = "Select unique number from table where status=0";
Get array $real=array (' 001 ', ' 002 ', ' 003 ', ' 004 ', ' 005 ', ' 006 ',); where 001,002 is the unique number

Then generate 94 Empty prizes
$empty =array (', ',..., ',);

Finally, the array is merged to get a completely new array.
$arr = Array_merge ($real, $empty);

The user randomly extracts an item from the array
$rand = Mt_rand (0, Count ($arr)-1);

Like $rand pumped 001.
if ('! = $rand) {
Indicates the pumping, modify the data state, here need to manipulate the database

SQL2:
$sql = "Update table set Status=1 where unique number = 001";
Operation completed

}else{
Means no pumping, no operation.
}



The problem is that when 100 people enter the lottery at the same time, in the red part of the SQL2, has not come to the urgent modification of the situation, SQL1 will continue to use 001 as a no-smoking award, so that other users continue to smoke, it is possible that 001 is pumped again.

I would like to ask you to help PHP high concurrent under the lottery program, how to avoid repeating the jackpot and the same award in many people?

Or consider the lottery program from other ideas.


Reply to discussion (solution)

It's hard to imagine how you were designed to be on the side of the lottery ...
Are there no deadlines for participating in the lottery? If there's two processes, the problem doesn't exist.

It's hard to imagine how you were designed to be on the side of the lottery ...
Are there no deadlines for participating in the lottery? If there's two processes, the problem doesn't exist.

There's a deadline. For example, the draw time is today 2013-09-15 until 2013-10-15
Can you give me a detailed description of your plan?

Since there is a cut-off time, what is the relationship with high concurrency?
Read the qualifying to an array, draw out the extracted from the array, then draw it.

Since there is a cut-off time, what is the relationship with high concurrency?
Read the qualifying to an array, draw out the extracted from the array, then draw it.
Number of SQL1 not winning 001 002 003 004 005 006
SQL2 User 1 in 001, under normal circumstances not winning the number left 002 003 004 005 006
Under special circumstances, when the SQL2 also the future of urgent changes 001 winning status for the winning situation, the User 2 query SQL1 get the winning number will be 001 002 003 004 005 006 So user 2 may also be 001

Are you playing this game online? Need client involvement?

1, the production into the prize pool $arr = Array_merge ($real, $empty);
2. Save in a file or stand-alone table
3. Open the file or table exclusively at the time of the Lottery (subsequent sweepstakes this can only be queued for the release of resources)
4. Modify the prize pool and release resources

Whether it's on or off, you can do it all at once.

Draw concurrent, let each user implement the queue draw.

1, the production into the prize pool $arr = Array_merge ($real, $empty);
2. Save in a file or stand-alone table
3. Open the file or table exclusively at the time of the Lottery (subsequent sweepstakes this can only be queued for the release of resources)
4. Modify the prize pool and release resources

Whether it's on or off, you can do it all at once.
It's supposed to be the queue.
Is it better to deposit memcache directly?

This is a feature (or disadvantage) of PHP, because it is multi-process independent, itself cannot solve the problem of concurrent synchronization, can be implemented with other programs, such as the use of memcached single-threaded features.

  • Related Article

    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.