Ankit Sinha

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
IdleForge mutation pipeline: idempotent command, Postgres row lock, atomic ledger and outbox, BullMQ worker and Redis rebuild command row lock ledger + outbox worker + redis IDEMPOTENT SINGLE TX OUTBOX · CURSOR POSTGRES OWNS TRUTH · REDIS SERVES FAST RANKS

Every mutation settles inside one PostgreSQL transaction; BullMQ workers drain outbox batches and rebuild Redis ranks from Postgres.

Select a scenario above to step through live state transitions.

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 #

72tests against real
Postgres + Redis
28.5 msp95, 300-request
mixed run
519req/s at 10
concurrent readers
0errors in local
load run

Correctness is not the happy path—it is what holds when the same mutation arrives twice.