Every rewards app looks simple from the outside. A customer buys something, they earn reward points, and later they redeem rewards for cash or a gift card. Under the hood, the part that decides whether users trust you is the points ledger.
If balances drift, double credit, or go negative, support tickets pile up fast. Here is a practical way to model it.
1. Never store a balance as the source of truth
Store every movement as an immutable ledger entry and derive the balance from it.
CREATE TABLE points_ledger ( id BIGSERIAL PRIMARY KEY, user_id BIGINT NOT NULL, entry_type TEXT NOT NULL, -- EARN, BONUS, REDEEM, EXPIRE, REVERSAL points INTEGER NOT NULL, -- positive for credit, negative for debit source_ref TEXT NOT NULL, -- order id, scan id, campaign id created_at TIMESTAMPTZ NOT NULL DEFAULT now(), UNIQUE (entry_type, source_ref) ); The UNIQUE (entry_type, source_ref) constraint is the cheapest protection you will ever add. If a webhook fires twice or a user double taps, the second insert fails and nobody earns twice for the same purchase.
You can still keep a cached balance column for fast reads, but update it in the same transaction as the ledger insert, and rebuild it from the ledger if they ever disagree.
2. Treat bonus reward points as their own entry type
Marketing teams love campaigns like "double points this weekend" or "50 bonus points on your first scan". Do not fold these into the base EARN row. Write a separate BONUS row linked to the campaign.
Why this matters:
- You can report how much a campaign actually cost
- You can reverse a bonus without touching the base earn
- Bonus reward points can have a shorter expiry than regular points
3. Make redemption a two step process
Redeeming is where money leaves the system, so be strict:
- Reserve: insert a REDEEM row with negative points inside a transaction that checks the derived balance is enough (
SELECT ... FOR UPDATEon the user row). - Fulfil: call the payout or gift card provider.
- Confirm or reverse: on failure, insert a REVERSAL row. Never delete the REDEEM row.
This keeps an audit trail for every rupee or point that moved.
4. Tie earning to a verifiable event
The biggest leak in most programs is fraud on the earn side: fake receipts, shared promo codes, scripted sign ups. A stronger pattern is to tie each earn to a one time token that only a real buyer can get, for example a unique QR code printed under a scratch layer on the product pack.
This is the model used by Assured Rewards, a QR cashback and loyalty platform in India. Each code can be redeemed once, the scan confirms the product is genuine, and the reward lands in a wallet that the user can withdraw through UPI or use to redeem rewards on gift cards. In ledger terms, the scan id becomes the source_ref, so the unique constraint above blocks reuse for free.
5. Handle expiry as a scheduled job, not a query filter
If points expire, write an EXPIRE entry when they do. Filtering old rows out at read time makes balances change silently, which users read as theft.
Wrapping up
A good points system is mostly accounting:
- Append only ledger
- Idempotency keys on every credit
- Separate rows for base points and bonus points
- Reserve, fulfil, confirm on every redemption
Get that right and features like tiers, streaks or daily rewards deals become easy additions instead of rewrites.