Ask the php high-concurrency lottery program, how to avoid repeated wins and multiple people draw the same prize? Concurrent php lottery
My practice is as follows: First Prize: One prize, second prize: two, third grade: Three. in this way, the prize is 6. if 100 people participate in the lucky draw at the same time, then the empty Prize is 100-6 = 94.
First, query the database mysql for the unique number with 6 awards as the reward number:
Sql1:
$ SQL = "select unique number from table where status = 0 ";
Get the array $ real = array ('001', '002', '002', '002', '002', '001',), where 001,002 is a unique number.
Then 94 empty prizes are generated
$ Empty = array ('','',...,'',);
Finally, the array is merged to get a brand new array.
$ Arr = array_merge ($ real, $ empty );
The user randomly extracts an item from the array.
$ Rand = mt_rand (0, count ($ arr)-1 );
For example, $ rand received 001.
If (''! = $ Rand ){
// Indicates the data is being extracted and the data status is changed. you need to operate the database here.
Sql2:
$ SQL = "update table set status = 1 where unique id = 001 ";
Operation completed
} Else {
// Indicates that the image is not being extracted and no operation is performed.
}
The problem now is that when 100 people enter the lucky draw at the same time, sql1 will continue to use 001 as the undrawn award even when the red part of sql2, to allow other users to continue pumping, so 001 may be pulled again.
I would like to ask you to help the php high-concurrency lottery program. how can we avoid repeated prizes and multiple people winning the same prize?
Or consider the lottery program from other perspectives.
Reply to discussion (solution)
It's hard to imagine how your design could be used to draw a lottery while one side ......
Do you have no deadline for participating in the lottery? If there are two processes, the problem does not exist.
It's hard to imagine how your design could be used to draw a lottery while one side ......
Do you have no deadline for participating in the lottery? If there are two processes, the problem does not exist.
The deadline is the same. for example, the lucky draw time is and ends at-15.
Can you introduce your solution in detail?
Since there is a deadline, what is the relationship with high concurrency?
Read the qualified items to an array, extract the extracted items from the array once, and draw the lottery in sequence.
Since there is a deadline, what is the relationship with high concurrency?
Read the qualified items to an array, extract the extracted items from the array once, and draw the lottery in sequence.
Sql1 unwon: 001 002 003 004 005 006
In sql2, user 1 is in 001. under normal circumstances, 002 003 004 005 006 is not winning.
In special cases, when sql2 is about to change the 001 winning status to "already, if user 2 queries sql1, the unwon number will still be 001 002 003 004 005 006, so user 2 may be in 001 again.
Are you an online game? Need the client to participate?
1. generate prize pool $ arr = array_merge ($ real, $ empty );
2. stored in files or standalone tables
3. open a file or table in exclusive mode during the lucky draw (in the future, the lottery can only be queued to wait for the release of resources)
4. modify the prize pool and release resources
Whether it is an instant payment or a one-time expiration, it can be processed as a method.
Lottery concurrency allows each user to draw a lottery in queue.
1. generate prize pool $ arr = array_merge ($ real, $ empty );
2. stored in files or standalone tables
3. open a file or table in exclusive mode during the lucky draw (in the future, the lottery can only be queued to wait for the release of resources)
4. modify the prize pool and release resources
Whether it is an instant payment or a one-time expiration, it can be processed as a method.
It should be a queue.
Is it better to store memcache directly?
This is a feature (or disadvantage) of PHP, because it is independent of multiple processes and cannot solve the problem of concurrent synchronization itself. it can be implemented through other programs, for example, Memcached single-thread features are used.