Answer the question
In order to leave comments, you need to log in
How to deal with a large number of requests?
The user has a certain amount of funds in the database. At the request of the front, you increase this amount by 10 rubles and you cannot increase it more, but if you immediately execute 1000 requests by the bot, the amount will increase by 10 thousand rubles and will be closed only after some time. How to fix it?
Answer the question
In order to leave comments, you need to log in
Use a transaction and take a table row write lock before incrementing the value
Simply do each request in transaction with "select for update".
1. Immediately after receiving the request, you put the "select for update" block.
2. You validate the data. You check the payment in another table, etc.
3. You add 10 rubles to the user.
If your user can even after that add 100 rubles to himself 1000 times, then the problem is in step #2.
Those. your moment with "it cannot be increased more" should be checked in step #2.
but if the bot immediately executes 1000 requests, then the amount will increase by 10 thousand rubles
Queues. Essentially there are 2 options:
add a sign / column to the database "can not be updated anymore".
When updating, check this sign and set it to the “basta” position:
UPDATE accounts
SET money = money + 10, isUpdateable = false
WHERE id = :uid
AND isUpdateable = true
A successful update closes the shop. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question