Remix.run Logo
exabrial a day ago

> What if Alice and Bob both try to transfer money to each other at the same time?

> Let’s map out the transactions again:

> T1: Acquire a lock on Alice’s account

> T2: Acquire a lock on Bob’s account......

::loud sigh::

In MySQL, if you acquire a lock on Alice, then attempt acquire on Bob... then another session acquire Bob, then acquires Alice: The engine notices. Nothing bad happens. One of the threads gets marked as a deadlock and is rolled back, the other succeeds. So... yeah, not really a problem. MySql chooses at "random" which one lives.

Actually... hang on, pause, let me remind you what you learned in your undergrad CS 400 class: Acquire Locks in consistent order. You could sort by account id, or even their first names. Hell sort by their cats name. It doesn't matter, it just has to be deterministic, so: Alice comes before Bob. So doing this properly:

T1: Acquire a lock on Alice’s account with SELECT FOR UPDATE

T2: Acquire a lock on Alice’s account with SELECT FOR UPDATE... ::waits::

Annnnnnd, yeah, magic. Problem solved.

I don't find any of this too difficult and I really the only thing SQL engines do poorly is reporting on what locks are being held. Adding in XA Transactions can make things even more intuitive and gives you a nice building block if you need to coordinate sending messages to a broker or something else.

Oh and lastly, you should be using a ledger table for this; not storing balances as columns. Lock on both accounts, insert parallel rows into the ledger. And don't forget to use fixed point decimal datatypes.

I'll show myself to the door now. Thank you.