Field note / 04
Row locks, cursor outboxes, and Redis rebuilds
Caches and job queues make an economy fast, but they should never own the truth. Serialize every mutation in PostgreSQL, resume large payouts by cursor, and treat Redis purely as a rebuildable read index.
Nov 2026 · 3 min read · from IdleForge
Serialize the balance, persist the key #
Mobile retries, double-taps, and overlapping offline-earnings requests break naive balance math immediately. In-memory de-duplication masks the bug only on a single node.
command (idempotency_key)
└─ purchase_commands check ─▶ SELECT ... FOR UPDATE (player row)
└─ balance + ledger_entries + event_outbox (1 tx)
└─ BullMQ drains via last_player_id cursor
Resume by cursor, fail closed on cache #
- Cursor-resumable bonus batches: world events commit an
event_outboxrecord and sweep players in keyset slices, savinglast_player_idafter every batch so worker restarts resume cleanly without double-paying earlier accounts. - Append-only verification: every balance change writes a
ledger_entriesrecord in the same transaction; runningledger:verifyasserts that ledger sums match every player balance. - Rebuildable Redis leaderboards: sorted sets and exact score hashes hydrate atomically from PostgreSQL keyset pages. When Redis is unreachable, the API fails closed with
503instead of serving drifted rankings.
Speed layers stay safe only when you can drop them cleanly and reconstruct them deterministically from SQL.