| ▲ | piskov 7 hours ago | |
That whole article should have been: Use transactions table (just a name, like orders) On it have an Insert trigger. It should make a single update with simple “update … set balance += amount where accoundId = id”. This will be atomic thanks to db engine itself. Also add check constraint >= 0 for balance so it would never become negative even if you have thousands of simultaneous payments. If it becomes negative, it will throw, insert trigger will rethrow, no insert will happen, your backend code will catch it. — That’s it: insert-trigger and check constraint. No need for explicit locking, no stored procedures, no locks in you backend also, nada. Just a simple insert row. No matter the load and concurrent users it will work like magic. Blazingly fast too. That’s why there is ACID in DBs. — Shameless plug: learn your tool. Don’t approach Postgresql/Mssql/whathaveyousql like you’re a backend engineer. DB is not a txt file. | ||
| ▲ | mfcl 2 hours ago | parent | next [-] | |
The point of the article is to explain barriers and demonstrate them. The logic used for crediting amounts of money is not important. | ||
| ▲ | valenterry 3 hours ago | parent | prev | next [-] | |
> Shameless plug: learn your tool. Don’t approach Postgresql/Mssql/whathaveyousql like you’re a backend engineer. Erm, knowing and understanding how to use your database is a bread and butter skill of a backend engineer. | ||
| ▲ | shshshsjjjj 5 hours ago | parent | prev [-] | |
> no locks in you backend PG is still handling the locks for you, so this isn’t like a bulletproof solution and - like always - depending on your use case, scale, etc this may or may not work. > No matter the load and concurrent users it will work like magic Postgres will buckle updating a single row at a certain scale. ————- Regardless, this article was about testing a type of scenario that is commonly not tested. You don’t always have a great tool like PG on hand that gives you solutions so this testing isn’t needed. | ||