KatInterview

Virtual Currency Ledger with Real-Time Aggregation

System DesignModel answer
HardModel answerSystem Designledgertransactionsaggregation

Last updated

Build a transactional ledger supporting currency transfers, 24-hour flow queries, and 30-day hourly balance history at scale.

You're building the backend for a virtual currency system used in online gaming. Players earn and spend virtual coins through gameplay, purchases, and peer-to-peer exchanges.

Your system must support:

  1. Transfers: Move currency between any two accounts atomically. Transfers must never duplicate money, lose money, or allow negative balances.

  2. Recent activity summary: For any account, calculate total money received and total money sent in the trailing 24 hours.

  3. Historical balance snapshots: For any account, return the account balance at the end of each hour over the past 30 days.

Constraints and context:

  • Expect 200 million registered accounts with 50 million active daily
  • Roughly 500 million transfers per day (average 6K/sec, peak 50K/sec during events)
  • Some accounts are extremely active (popular creator accounts may receive thousands of payments per second)
  • Correctness is paramount: this is real money to players
  • All operations must be auditable and reproducible

Design the system end-to-end. Include:

  • API contracts
  • Data schema and storage strategy
  • How you guarantee correctness during transfers
  • How you efficiently compute 24-hour flows for high-volume accounts
  • How you maintain 30-day hourly history without scanning massive transaction logs
  • Key architectural trade-offs and failure modes

Hints

Hint 1

Use double-entry bookkeeping: every transfer creates two equal and opposite ledger entries that can be reconciled

Hint 2

Pre-aggregate statistics at hourly granularity as events stream in; combine pre-computed buckets with real-time tail queries for recent windows

Hint 3

Lock accounts in consistent order during transfers to prevent deadlocks; consider whether cross-shard transfers need sagas or distributed transactions