when we do the payment system for the first time, there are many merchants on our platform. Each merchant has a large number of orders per minute. I update the balance to the merchant account asynchronously. I use the mysql queue, and each successful order will be added to the queue as an item, crontab to read and update to increase the account balance, but encountered a problem of withdrawing.
this is what I do to withdraw cash, the account balance minus the withdrawal amount, and then update the remaining money to the account balance field, there will be a problem!
1. When you withdraw cash, you get an account balance of 10 yuan, and you want to withdraw 5 yuan, and the withdrawal operation is in progress, but at this time, an order of 5 yuan comes in, and asynchronously needs to update the account. You still get 10 yuan, plus 5 yuan, and then update 15 yuan to the balance field. At this time, the withdrawal operation is also under way, withdrawing 5 yuan, and then updating the remaining 5 yuan to the balance field, covering the previous 15,
.2. I didn"t add a lock when I checked the balance. Can locking solve the problem? is there any better way to solve the problem besides adding a lock? Whether the logic of updating account balance and withdrawing account asynchronously needs to be improved. I hope you can give us some comments. Thank you
the first item is entered into the account, the first field is the second balance of ID, the third order amount, the fourth updated balance,
the second is the withdrawal, the second is the withdrawal, the third is the withdrawal amount, and the fourth is the same as above