Assume I am creating a webapp which can receive money from the users to their "accounts" in my system and send them out, something like a super simplified version of a bank.<p>Are there any guides, practices to make it safe and properly? I see multiple ways of doing the same thing. For example, when a user puts in $10, then $3 then $7, then moves out $4, I can use a table in the database called balances and add or subtract a single number in the column - 0 + 10 + 3 + 7 - 4 = 16.<p>So, I keep in the column value of 16.<p>Another way is to add rows into a table called "transactions" and calculate the current balance every time I need it. In this case, if there are thousands of transaction for the user, I would need to calculate thousands of numbers every time I need to know balance.<p>I don't want to reinvent the wheel so I would be happy to just have some guide of how these things are usually implemented even though I understand there is no such scenario as "standard" one and there are always myriads of small differences between different implementations.<p>In what direction should I look to understand how to build such a system in a better way and avoid mistakes?<p>Thanks!
Another way is maintaining the final balance field for the readings. Every new transaction you save it into the transactions table and adjust the balance field as well.<p>You can have a job to go through the transactions from time to time to save important balances that user would need (e.g: daily, monthly and quarterly balances).
The first principle with money is to have one source of truth.<p>E.g. a single mainframe through which all transactions run in real time.<p>Anything else will be hacked because there's money as incentive.<p>This means accepting latency and rejected transactions.<p>There is no simplified version of this...that won't be robbed easily.<p>Good luck.
Create two tables: Account and Transaction.
In the `Account` table you store information of the account and the latest balance. In the `Transaction` table you store all the transactions.