Imagine the problem: I have a REST service that is implemented using Java / MySQL / Spring and HTTP / JSON technologies. REST clients are mobile applications. Thus, it is possible that someone will decompile the code and get the REST service API. (Yes, the code is confusing, etc., but in any case).
Problem: There is a POST method to send money to another user of the application. I am worried that someone might get an API, write a bot and make this POST request 500 or 5000 or even 50,000 times per second. As a result, he can send more money than actually, because if 1000 requests are processed at the same time, checking the balance can be successful for all 1000 requests, however, for example, 50 requests can be enough for the actual amount of money in the account.
So basically this is more like the standard race condition with multiple threads. The problem is that I have several servers, and they are in no way connected to each other. Thus, 300 requests can come to server A, 300 requests can go to server B, and vacation requests can go to server C.
The best idea that I have is to use something like "SELECT ... FOR UPDATE" and synchronize at the database level. However, I would like to consider other solutions.
Any ideas or suggestions?
source share