I did nothing to do with a small program, the main function is that users buy products, for example, a product is priced at 40 yuan, each user out 1 yuan, the total number of user flash sales records is equal to 40, then the product will be closed for purchase. I have doubts. What should I do if there are more concurrent users? If there are 10 users at the same time... I did nothing to do with a small program, the main function is that users buy products, for example, a product is priced at 40 yuan, each user out 1 yuan, the total number of user flash sales records is equal to 40, then the product will be closed for purchase.
I have doubts. What should I do if there are more concurrent users? If 10 users buy and pay at the same time, how can we ensure that the total number of purchases in the purchase record does not exceed the total price of the product?
If the product value is 40, 36 people have successfully paid for and purchased the product, and there are 4 remaining users. This is another 10 users who want to win the remaining 4 places, that is to say that six people cannot be taken. How can we control it? Never let the total number overflow ···
Thank you ~~
Some people say that the database is locked. I don't know what it means ···
Reply content:
I did nothing to do with a small program, the main function is that users buy products, for example, a product is priced at 40 yuan, each user out 1 yuan, the total number of user flash sales records is equal to 40, then the product will be closed for purchase.
I have doubts. What should I do if there are more concurrent users? If 10 users buy and pay at the same time, how can we ensure that the total number of purchases in the purchase record does not exceed the total price of the product?
If the product value is 40, 36 people have successfully paid for and purchased the product, and there are 4 remaining users. This is another 10 users who want to win the remaining 4 places, that is to say that six people cannot be taken. How can we control it? Never let the total number overflow ···
Thank you ~~
Some people say that the database is locked. I don't know what it means ···
Update Item set count = count + 1 where id = $ id and count = $ count;
General logic:
Try = 0;
While true do
Select * from item where id = 1;
If $ count> = 40 then close item; break end
If ++ try> 10 then break end
Update item count = count + 1 where id = $ id and count = $ count;
If update OK then
Xxxx
Break;
End
End
For this application scenario, you can consider memcache or redis.
Mysql has to use the lock mechanism in high concurrency.
InnoDB engine, which supports row locks, transactions, and foreign keys.
You can use for update to share the lock. The transaction is as follows.
Begin;
Select price from good where id = 1For update; // You must add the price of the current product for update.
Then determine whether it is 40
Executed at 40
Update good set price = price + 1 where id = 1 (you can open another mysql client to operate on this data before committing it, and you will find that the lock has been waiting for release) commit
Roll back and terminate
rollback
A lock is a mechanism by a computer to coordinate multiple processes or threads to concurrently access a certain resource. To ensure data consistency and effectiveness.
Locks are divided into Table locks, row locks, and page locks. I recommend using row locks in highly concurrent environments.
Table-Level Lock
Small overhead, fast locking; no deadlock; large lock granularity, the highest probability of lock conflict, the lowest concurrency; suitable for more query operations
Row-Level Lock
High overhead, slow locking, deadlock, minimum lock granularity, the lowest probability of lock conflict, and the highest concurrency. Suitable for Concurrent Data Update
Page lock
The overhead and lock time are between the table lock and the row lock. A deadlock occurs. The lock granularity is between the table lock and the row lock, and the concurrency is normal.