by miohtama · 4y ago · 0 comments
If you deal with money use PostgreSQL + SERIALIZABLE transaction isolation level to be sure.
More in PostgreSQL documentation https://www.postgresql.org/docs/current/transaction-iso.html
Also Zhong was 22 years old script kiddie when he hacked Silk Road. Any smart criminal would have left United States long time ago if you sit on the top of $3B stash.
https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-i... https://mariadb.com/kb/en/mariadb-transactions-and-isolation...
[0] https://ronaldbradford.com/blog/understanding-mysql-innodb-t...
Please pick a database (relational or otherwise) with ACID transactions and replication, buy at least two different textbooks on that specific database, read both, do any exercises they suggest, and then decide whether you still want to build that system. If yes, then cool: don't hold me responsible if anything goes wrong, but you need to learn somehow.
Yes, it's possible to build systems like that on non-ACID-compliant databases, or without using the ACID functionality. It's much harder, though, and you really must understand what you're giving up in order to have a reasonable chance of success.
$balance = sql_query(“SELECT balance FROM accounts WHERE account_id = “ . $account_id);
if ($withdrawal_amount < $balance) {
withdraw_funds($account_id, $withdrawal_amount);
}
The correct solution of course requires putting the checks in an ACID transaction along with the account balance updating. This requires more work upfront (SQL is harder to write than PHP) and an impatient amateur coder probably didn’t think of it. Also I’m guessing they didn’t properly index all the DB columns and this made transactions take longer which exacerbates race conditions.Realistically your DB of choice will probably try and tackle ACID half-heartedly because it's what devs expect, and that will lull you into the same false sense of security.
Many "eventually consistent" DBs will still have some concept that uses the word "transaction" that works like one until it doesn't (see DynamoDB and multi-region gotchas)
If you are new to the subject there are extensive reports available from Jepsten, one of the leading names in distributed system and ACID anomaly testing and research.
Even executing queued operations with few seconds delay allow you to notice say a series of withdrawals putting account in negative
I alerted the exchange operator and he let me keep the 10$.
Seems to me that REPEATABLE READ mode should be sufficient, but I'm not sure.