Selected work / 03
IdleForge
Idle-game economy backend with transactional offline collection, persisted idempotency, an append-only ledger, and an exact Redis leaderboard.
TypeScript · Node.js · Express · PostgreSQL · Drizzle · Redis · BullMQ · Vitest · Docker
The problem #
Economy mutations—collecting offline earnings, buying upgrades, awarding world bonuses—break under client retries and parallel requests. IdleForge keeps PostgreSQL as the single source of truth and treats Redis and BullMQ purely as speed and scheduling layers.
Architecture #
Client
│ authenticated requests
▼
Express API ────── PostgreSQL
│ ├─ players, balances, upgrades
│ ├─ purchase_commands (idempotency)
│ └─ event_outbox + ledger_entries
│
├── Redis ├─ rate-limit windows
│ ├─ leaderboard ZSET + exact hash
│ └─ BullMQ queue
│
└── Worker ├─ drain outbox · batch bonuses
└─ verify ledger · rebuild rank
Every mutation settles inside one PostgreSQL transaction; BullMQ workers drain outbox batches and rebuild Redis ranks from Postgres.
Key decisions #
Locked balance mutations + persisted idempotency
Collection and upgrades run under guarded SQL and row locks. Retries hit persisted idempotency rows and occurrence primary keys rather than in-memory caches.
Cursor-resumable batched bonuses
World bonuses enqueue through a transactional outbox and process in bounded batches with a persisted last_player_id cursor so crashes resume without double-paying.
Append-only ledger verification
Every balance change writes a ledger_entries row in the same transaction; ledger:verify asserts that ledger sums match every player balance.
Postgres owns truth; Redis owns the fast view
Leaderboards use a Redis ZSET + exact hash rebuilt atomically from Postgres keyset pages. If Redis drops, the API returns 503 instead of serving stale ranks.
Numbers #
Postgres + Redis
mixed run
concurrent readers
load run
Correctness is not the happy path—it is what holds when the same mutation arrives twice.