Answer the question
In order to leave comments, you need to log in
How to protect against multi-requests (Charles)?
Let me give you an example: let's say you need to make a chat in which sending one message is paid and amounts to 1 ruble (the first condition). The second condition is that the user cannot send more than two messages in a row.
That is, the algorithm for sending a message is as follows: 1.
Check
if the user has already sent 2 messages;
enough, then:
3. Add a message to the database.
The problem is that during the checks, the requests may not have time to be executed and a couple of requests will pass the check and slip through the restrictions while the old requests will "finish their execution".
This situation is just an example, and you should not concentrate only on the chat. Let's take an example with online roulette:
Several users play in one room (no matter the number). The limit on the amount of the bet is from 10 to 50 rubles. The maximum number of bets per round is 5 bets.
That is, in this example, you also need to make several checks in order to add the bet to the base later. If the bid does not have time to be added, and the next request starts to be executed, then at this time the check for the number of bids will not be passed correctly and the bid limit may be violated.
Tell me, please, what to do in this situation?
Answer the question
In order to leave comments, you need to log in
Transactions and row locking. Dot.
https://en.wikipedia.org/wiki/Isolation_Level...
https://dev.mysql.com/doc/refman/8.0/en/innodb-tra...
You seem to want the strongest transaction isolation level, SERIALIZABLE .
A more complex but more performant way is SELECT FOR UPDATE.
https://dev.mysql.com/doc/refman/8.0/en/innodb-loc...
Here the level of transaction isolation is not important, since you yourself choose which row to isolate.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question