Zero-Drift Cashback Wallet Design Using PostgreSQL Ledger RPCs and Row Level Security
A developer describes how the Vietnamese cashback platform Hoàn Tiền (nhanhoantien.vn), which serves shoppers on Shopee, TikTok Shop, Lazada, and Tiki, avoided common money-handling bugs. A simple mutable balance column risks double-spending during concurrent withdrawals, duplicate or out-of-order affiliate webhooks, floating-point rounding errors, and no audit trail. The fix routes every monetary change through SECURITY DEFINER PostgreSQL functions that append to an insert-only transactions table, with row locking for withdrawals. Currency is stored as integer VND across separate pending, payable, and paid totals, and Row Level Security scopes each user's access. The author reports that testing a database invariant that totals match transaction sums eliminated a whole class of financial bugs before launch.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in