TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How to better implement a deposit/withdrawal tracking system?

3 pointsby cosmorocketabout 3 years ago
Assume I am creating a webapp which can receive money from the users to their &quot;accounts&quot; 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 &quot;transactions&quot; 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&#x27;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 &quot;standard&quot; 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!

3 comments

zeuchabout 3 years ago
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).
brudgersabout 3 years ago
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&#x27;s money as incentive.<p>This means accepting latency and rejected transactions.<p>There is no simplified version of this...that won&#x27;t be robbed easily.<p>Good luck.
_448about 3 years ago
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.