4 ms·
You can use an ACID compliant database and run into this problem, it's a classical race condition. Isn't it a quick fix to add an ACID-friendly clause to the U
by mootothemax 13y ago
You can use an ACID compliant database and run into this problem, it's a classical race condition.
Isn't it a quick fix to add an ACID-friendly clause to the UPDATE statement, e.g. some pseuosql like:
UPDATE balances SET balance = (balance - amount) WHERE user_id = input_user_id AND (balance - amount > 0)
- eterm 13y agoYou shouldn't ever even have a "balance" that you update. You should have a ledger. "INSERT INTO Ledger (Account, Date, Amount) VALUES (PersonToDebit, Now, -withdrawl)" And then someone's "balance" is the sum of their ledger. If multiple withdrawls go through, the account could still go negative if you don't check at the time of the withdrawl, but worse case you still end up with negative balances rather than flat out losing track of the balance because you have a balance that you UPDATE.
- mootothemax 13y agoYou shouldn't ever even have a "balance" that you update. I was arguing against the grandparent's claim of ACID race conditions; my intention wasn't to advocate in favour of a "balance" column. Edit: that said, I wouldn't mind hearing the pros or cons of maintaining a convenience (e.g. page display) "running balance" that is updated from the ledger after every transaction.
- unclebucknasty 13y agoWell, I think the parent was saying that you shouldn't have a balance that is actually relied upon for record-keeping or transactional purposes. If you want to keep one for optimization purposes (i.e. you are reading more than writing and don't want to constantly sum the ledger table), then that's probably outside the scope of the parent's point.
- rdtsc 13y agoWell you'd probably want to have both, a negative balance is a problem with bitcoin if it can be arbitrarily negative. Say they issue 1000 withdraws in a millisecond. The balance will be "very" negative and there is nothing that can be done. And it might have to be done only a few times to bankrupt the exchange. With the regular banking system or even with stock market, some trades and transactions can be reversed in case of errors and mistakes. So just having a ledger works, some bad updates can be reversed. Not so with bitcoin. So when dealing with someone's account at a place like this an ACID compliant balance update is also needed. Amateurs have jumped in and started developing banking software that deals with hundreds of millions of dollars -- so this is not surprising. But at the same time it is kind of nice. It is sort of a Darwinian if you wish, the weakest and unlucky ones fail and get robbed the competent and/or lucky get to survive.