KatInterview

Build a Scalable Portfolio Optimizer with Point-in-Time Data

System DesignModel answer
HardModel answerSystem Designdistributed-systemsfinancial-systemsdata-pipelines

Last updated

Design a system that runs 30,000 portfolio optimizations in 90 minutes with strict as-of-date correctness, deterministic replay, and full audit trails.

Problem

Design a portfolio optimization engine that rebalances thousands of investment accounts every evening. The system must handle both live daily runs (applying tax-loss harvesting, asset location across account types, and alpha tilts) and large-scale historical simulations used to test new strategies across many parameter sets.

Each optimization solves a constrained mathematical program balancing expected return, risk, transaction costs, and tax consequences. Solver routines are CPU-intensive numerical algorithms (quadratic or mixed-integer programming) that respect constraints like wash-sale rules, tracking error limits, and cash availability.

Requirements

Functional:

  • Execute single-date production runs and multi-period backtests spanning dozens of rebalance dates
  • Guarantee point-in-time correctness: every input (security prices, dividend events, tax lot cost basis, risk factor loadings) reflects only what was known at the simulation timestamp, preventing look-ahead bias
  • Provide deterministic reproducibility: identical inputs produce identical outputs, enabling exact replay for audit and debugging
  • Validate results before committing: detect infeasible solutions, constraint violations, and anomalous portfolio turnover

Scale:

  • Process roughly 30,000 accounts during the evening batch window of 90 minutes
  • Each optimization consumes 1–2 CPU cores and completes in 3–15 seconds depending on portfolio complexity
  • The system must tolerate individual worker failures without restarting the entire batch

Data characteristics:

  • Tax lot data: ~15 million lot records across all accounts at any snapshot
  • Risk models: factor-based representation with ~5,000 securities and ~80 factors
  • Historical simulations reuse the same snapshot data across thousands of strategy variants

What to Design

  1. Overall architecture: how orchestration, data retrieval, compute workers, and storage layers interact
  2. Point-in-time data management: storing and serving bitemporal financial data efficiently to thousands of concurrent tasks
  3. Worker orchestration: scheduling, horizontal scaling, shared-data caching, and handling slow or failed tasks
  4. Lineage and audit: capturing complete provenance for compliance and deterministic replay
  5. Key trade-offs: exact solvers vs. heuristics, pre-materialization vs. on-demand queries, and strategies to finish within the batch window

Hints

Hint 1

Calculate the total CPU-hours required and work backward to determine how many concurrent workers you need—it's far fewer than 30,000

Hint 2

Materialize point-in-time snapshots once per batch and share them across all workers to avoid overwhelming the database

Hint 3

Use content-addressable storage for snapshots and pin solver versions to guarantee deterministic replay