Design an Airbnb-Style Wallet System
Context
Design and implement a comprehensive wallet system, similar to Airbnb's, that manages financial transactions for users, hosts, and the platform itself. The system must support a wide range of financial operations, maintain strict financial integrity, and be highly reliable.
Core Capabilities
The system must support the following operations:
- Customer Deposits: Allow users to add funds to their wallet.
- Booking Holds & Captures: Implement a mechanism for authorizing funds (holds) at the time of booking and capturing them at check-in.
- Hold Releases: Automatically or manually release funds from a hold upon booking cancellation or expiry.
- Refunds: Process partial or full refunds to users.
- Host Payouts: Facilitate payouts to hosts, accounting for platform fees and applicable taxes.
- Multi-Currency Management: Handle balances in multiple currencies, including explicit foreign exchange (FX) conversions and defined rounding rules.
System Guarantees & Constraints
The system must adhere to the following principles:
- Auditable Ledger: Maintain a double-entry, append-only ledger where all financial movements are recorded, ensuring debits always equal credits. Balances must be derivable from this ledger.
- Concurrency Safety: Guarantee correctness and prevent race conditions for concurrent or overlapping requests.
- Idempotency: Ensure that operations, identified by client-supplied idempotency keys, can be safely retried without unintended side effects.
- Financial Integrity:
- All amounts must be stored in minor units (integers) to prevent floating-point precision issues.
- Each user, host, and the platform itself will have a wallet, with per-currency sub-accounts.
- A materialized balances table should be maintained transactionally for performance, consistent with the ledger.
- Booking holds and captures must occur in the booking's original currency.
- Enforce a distinction between "spendable" and "total" balance, preventing the spendable balance from becoming negative.
- Reconciliation: Provide mechanisms to detect and resolve inconsistencies between the ledger and derived balances.
Deliverables
Candidates are expected to:
- Define the persistence schema (e.g., database tables or core classes) and specify appropriate transaction isolation levels.
- Design clear APIs/methods for all required operations.
- Outline a minimal
main()function to demonstrate sample scenarios. - Include unit tests covering happy paths and critical edge cases, such as:
- Insufficient funds
- Expired holds
- Attempts to double-capture
- Partial refunds
- Changes in FX rates
- Identify core classes (e.g.,
Wallet,Account,Transaction,Hold,Capture,Refund,Payout,LedgerEntry,FXConverter), their relationships, and method signatures. - Analyze the time and space complexity of critical operations.
AI interview prep
Core dilemma
Ensuring absolute financial integrity and auditability (ACID properties, double-entry ledger) while supporting high-throughput, low-latency operations, complex multi-currency transactions, and concurrent access in a distributed environment.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design an Airbnb-Style Wallet System
Context
Design and implement a comprehensive wallet system, similar to Airbnb's, that manages financial transactions for users, hosts, and the platform itself. The system must support a wide range of financial operations, maintain strict financial integrity, and be highly reliable.
Core Capabilities
The system must support the following operations:
- Customer Deposits: Allow users to add funds to their wallet.
- Booking Holds & Captures: Implement a mechanism for authorizing funds (holds) at the time of booking and capturing them at check-in.
- Hold Releases: Automatically or manually release funds from a hold upon booking cancellation or expiry.
- Refunds: Process partial or full refunds to users.
- Host Payouts: Facilitate payouts to hosts, accounting for platform fees and applicable taxes.
- Multi-Currency Management: Handle balances in multiple currencies, including explicit foreign exchange (FX) conversions and defined rounding rules.
System Guarantees & Constraints
The system must adhere to the following principles:
- Auditable Ledger: Maintain a double-entry, append-only ledger where all financial movements are recorded, ensuring debits always equal credits. Balances must be derivable from this ledger.
- Concurrency Safety: Guarantee correctness and prevent race conditions for concurrent or overlapping requests.
- Idempotency: Ensure that operations, identified by client-supplied idempotency keys, can be safely retried without unintended side effects.
- Financial Integrity:
- All amounts must be stored in minor units (integers) to prevent floating-point precision issues.
- Each user, host, and the platform itself will have a wallet, with per-currency sub-accounts.
- A materialized balances table should be maintained transactionally for performance, consistent with the ledger.
- Booking holds and captures must occur in the booking's original currency.
- Enforce a distinction between "spendable" and "total" balance, preventing the spendable balance from becoming negative.
- Reconciliation: Provide mechanisms to detect and resolve inconsistencies between the ledger and derived balances.
Deliverables
Candidates are expected to:
- Define the persistence schema (e.g., database tables or core classes) and specify appropriate transaction isolation levels.
- Design clear APIs/methods for all required operations.
- Outline a minimal
main()function to demonstrate sample scenarios. - Include unit tests covering happy paths and critical edge cases, such as:
- Insufficient funds
- Expired holds
- Attempts to double-capture
- Partial refunds
- Changes in FX rates
- Identify core classes (e.g.,
Wallet,Account,Transaction,Hold,Capture,Refund,Payout,LedgerEntry,FXConverter), their relationships, and method signatures. - Analyze the time and space complexity of critical operations.