Alex has one hundred dollars. He pays the pizza shop twenty and the flower shop forty, at the same moment, from one bank account. Both payments say done. He should have forty left. He has sixty, and the bank is twenty dollars short. There was no error anywhere. This article shows why, and three ways to make it safe.
The video tells the same story in five minutes. This is the written version, with the SQL.
One account, one row
Alex's account is one row in MySQL:
CREATE TABLE accounts ( owner VARCHAR ( 64 ) NOT NULL , balance INT NOT NULL , PRIMARY KEY ( owner ) ) ENGINE = InnoDB ;
INSERT INTO accounts VALUES ( 'Alex' , 100 ) ;
Enter fullscreen mode
Exit fullscreen mode
What MySQL gets right: the row lock
First, the case that works.…