When building real-money features — like the Vietnamese e-commerce cashback web platform Hoàn Tiền (nhanhoantien.vn) for Shopee, TikTok Shop, Lazada, and Tiki — the quickest MVP shortcut is storing a single balance column on users and running UPDATE users SET balance = balance + $1 from backend handlers.
In production, that shortcut breaks down fast under four real-world failure modes:
Concurrent withdrawal race conditions : Two simultaneous payout requests both pass balance >= 10000 and double-spend.
Duplicate or out-of-order affiliate webhooks : Networks frequently retry callbacks or send cancellation events before creation events.
Floating-point rounding drift : Storing money as FLOAT / DOUBLE introduces penny/dong discrepancies over thousands of rows.
Zero audit trail : A single mutable number cannot explain why a user's balance changed yesterday.
Here is the architecture I implemented using Next.js 15 (App Router), TypeScript, PostgreSQL 17 with Row Level Security (RLS), and Drizzle .
1.…