Menu

1 / 3

Designing a Zero-Drift Cashback Wallet with PostgreSQL Ledger-First RPCs and RLS

DEV Community: nextjs·Bao Bui·about 7 hours ago
#ECujQyaJ
#devtonextjs#devto#dev#balance#user
Long article (504 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.