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.
So, I keep in the column value of 16.
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.
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.
In what direction should I look to understand how to build such a system in a better way and avoid mistakes?
Thanks!
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).
E.g. a single mainframe through which all transactions run in real time.
Anything else will be hacked because there's money as incentive.
This means accepting latency and rejected transactions.
There is no simplified version of this...that won't be robbed easily.
Good luck.