TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

3 点作者 cosmorocket大约 3 年前
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 条评论

zeuch大约 3 年前
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).
brudgers大约 3 年前
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.
_448大约 3 年前
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.