Interview concept

Sharding And Partitioning Time-Series Market Data

Asked of: Software Engineer

Last updated

Architecture diagram showing market-data ingestion and query routing across hash-partitioned shards with time buckets, hot-key salting, fan-out reads, merge-sort, and versioned rebalancing.

What's being tested

This topic tests whether you can design a distributed storage and query path for high-volume, append-heavy market data while preserving predictable latency and correctness. The interviewer is probing your ability to choose shard keys, reason about time-range queries, handle hot partitions, and explain rebalancing and failure behavior. Bloomberg cares because market-data systems must serve large concurrent workloads with strict latency expectations, while avoiding incorrect or incomplete results during outages and topology changes.

Core knowledge

  • Partitioning dimensions: Time-series data is commonly partitioned by instrument, venue, event type, and time bucket. A composite key such as (instrument_id, trading_date) localizes historical queries but can create hotspots for heavily traded instruments.

  • Range versus hash partitioning: Range partitioning makes time-window scans efficient and supports pruning, while hash partitioning distributes writes evenly. A practical design often hashes instruments across shards and ranges within each shard by day or hour.

  • Shard-key locality: Queries such as “one security over six months” benefit from instrument locality; queries such as “all securities during the last minute” benefit from time locality. State the dominant query patterns before choosing, because no shard key optimizes both dimensions simultaneously.

  • Time bucketing: Use fixed UTC buckets such as hour, day, or trading session, with bucket size chosen from data volume and query latency. Smaller buckets improve pruning and parallelism but increase metadata, file, index, and request overhead.

  • Hot-key mitigation: A popular instrument or live market interval can overload one shard. Mitigations include deterministic sub-sharding, such as (instrument_id, time_bucket, salt), followed by fan-out reads and merge-sort; avoid random salts unless reads can discover every destination.

  • Consistent hashing: A consistent-hashing ring or virtual-node scheme limits movement when adding shards, but it does not by itself solve time-range locality or hot instruments. For deterministic placement, persist the partition map and version it with the data.

  • Query execution: A time-range query should prune irrelevant buckets, route only to affected shards, issue requests in parallel with bounded concurrency, and merge sorted streams using a min-heap. For kk streams and nn returned records, merge cost is O(nlog⁡k)O(n \log k).

  • Ordering semantics: Market events may share timestamps or arrive with corrections. Define a stable ordering such as (event_time, sequence_number, venue, event_id) and distinguish event time from ingestion time; never rely on wall-clock timestamps alone.

  • Replication and consistency: Replication factor three is a common starting point, with quorum or leader-based writes depending on durability needs. Clarify whether readers require strongly consistent snapshots, read-after-write behavior, or merely eventually consistent historical data.

  • Indexes and physical layout: Partition pruning should eliminate most storage before row-level filtering. Within each partition, sorted segments, zone maps, Bloom filters, or columnar indexes can reduce I/O; indexes that duplicate enormous time-series volumes may cost more than they save.

  • Rebalancing: Moving a shard requires copying a consistent snapshot, replaying changes, switching routing atomically, and garbage-collecting the old copy. Use epochs or routing generations so requests do not mix incompatible partition maps.

  • Retention and corrections: Historical data may require retention tiers, such as hot recent data and colder object storage. Corrections, cancellations, and corporate actions should be modeled as immutable events or versioned records rather than silently overwriting data.

Worked example

Design a sharded time-series market-data store for historical price queries. In the first 30 seconds, clarify the read patterns, retention period, expected write and read rates, acceptable p99 latency, ordering requirements, correction semantics, and whether queries target one instrument or broad market slices. I would organize the answer around four pillars: the data model and time-bucketed layout, shard-key and routing strategy, distributed query execution, and replication/failure handling. I would initially choose instrument hashing across shard groups plus daily time buckets, because single-instrument history is likely to be common and daily partitions provide manageable pruning. For broad “market at time T” queries, I would explicitly support bounded fan-out across instrument shards rather than pretending the primary layout makes that query cheap. A key tradeoff is whether to sub-shard highly active instruments: it protects write and storage hotspots but requires parallel reads and a deterministic merge of ordered events. I would describe versioned routing metadata, snapshot-plus-replay movement during rebalancing, and deduplication using stable event identifiers. I would close by saying that, with more time, I would quantify shard sizes using production-like distributions and test p50, p99, failover, and rebalance behavior under a skewed workload.

A second angle

Design a low-latency query path for all instruments during a one-minute market interval. Here, time locality matters more than keeping one instrument’s entire history together, so a time-based partition or a secondary time-oriented index may be preferable. The main challenge becomes bounded fan-out and response-size control: parallelize across shards, enforce deadlines, and stream or aggregate results rather than buffering an unbounded response. You should also discuss partial-result policy, because returning stale or incomplete market-wide data may be worse than returning an explicit error for a pricing-sensitive client.

Common pitfalls

Pitfall: Choosing hash partitioning on instrument_id and claiming every query becomes efficient. Hashing distributes load, but it makes market-wide time-window queries fan out to many shards; explain the query workload and quantify that cost.

Pitfall: Saying “use replication” without defining consistency, failover, or duplicate behavior. A stronger answer specifies the acknowledged-write rule, reader behavior during failover, stable event IDs, and how retries remain safe.

Pitfall: Jumping directly to Kafka, Cassandra, or S3 without explaining the storage and routing invariants. Technology names do not replace a shard-key rationale, partition-map versioning, merge strategy, or recovery plan.

Connections

An interviewer may pivot to consistent hashing, quorum replication, LSM-tree compaction, backpressure, or distributed snapshot consistency. Be ready to connect partition choice to cache locality, tail latency, observability, and exactly-once effects without treating “exactly once” as a substitute for idempotent application behavior.

Further reading

Related concepts