Overview
Consider the following details:
- We have a table named
user. In it is a column namedwallet. - We have a table named
walletAction. We insert a new entry on each wallet action a user is doing. This table acts like some sort of logs in the database with some calculations. - We have a CRON command that does an update every
Nminutes. Each CRON action gets some data by using a standalone API and 'inserts' a newwalletActionentry. At the sime time, it updates theuser.wallet's value. - A
usercan buy stuff from our site. When theuserclicks the buy button, we insert a newwalletActionentry and change theuser.walletcolumn.
Problem
I am afraid that the CRON update and the action of the user when they click the buy button will happen at the exact same time causing the entries in the walletAction table to have wrong calculations.
I need some kind of 'lock' on the CRON update execution or something along those lines.
Questions
- Should I be afraid of this situation?
- How can I avoid this problem?
- Can I avoid this trouble by using MySQL transactions?
- What isolation level should I use and in which case should I use it? (In the CRON command or in the action of the
userwhen they click the buy button?)