0
0
HLDsystem_design~25 mins

Cross-shard queries in HLD - System Design Exercise

Choose your learning style9 modes available
Design: Cross-shard Query System
Design focuses on the architecture and flow for executing cross-shard queries efficiently and reliably. Does not cover shard key design or shard rebalancing.
Functional Requirements
FR1: Support queries that need to fetch and combine data from multiple database shards.
FR2: Ensure query results are consistent and accurate across shards.
FR3: Minimize query latency to keep response times under 500ms for 95% of queries.
FR4: Handle up to 10,000 concurrent cross-shard queries.
FR5: Support both read-only and read-write queries spanning multiple shards.
Non-Functional Requirements
NFR1: Data is horizontally partitioned (sharded) across multiple database instances.
NFR2: Each shard holds a subset of the total data with no overlap.
NFR3: System availability target is 99.9% uptime.
NFR4: Network latency between shards can vary up to 50ms.
NFR5: Queries must not overload any single shard or network link.
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Query router or coordinator to parse and distribute queries
Shard databases holding partitioned data
Result aggregator to combine partial results
Caching layer to reduce repeated cross-shard queries
Network communication layer between components
Design Patterns
Scatter-gather query pattern
Two-phase commit for distributed transactions
Query rewriting and pushdown to shards
Result merging and sorting
Caching and partial result reuse
Reference Architecture
Client
  |
  v
Query Router/Coordinator
  |
  |---> Shard 1 DB
  |---> Shard 2 DB
  |---> Shard N DB
  |
  v
Result Aggregator
  |
  v
Client Response
Components
Query Router/Coordinator
Custom service or middleware
Receives client queries, parses them, determines which shards to query, and sends sub-queries.
Shard Databases
Relational or NoSQL databases (e.g., PostgreSQL, MongoDB)
Store partitioned data subsets; execute sub-queries locally.
Result Aggregator
Service or component within Query Router
Collects partial results from shards, merges, sorts, and formats final response.
Cache Layer
In-memory cache (e.g., Redis, Memcached)
Store frequently requested query results or partial results to reduce load and latency.
Network Communication Layer
RPC or REST APIs
Facilitates communication between Query Router and shards.
Request Flow
1. Client sends a cross-shard query to the Query Router.
2. Query Router parses the query and identifies which shards hold relevant data.
3. Query Router rewrites the query into sub-queries targeted to each shard.
4. Sub-queries are sent in parallel to the respective shard databases.
5. Each shard executes its sub-query and returns partial results.
6. Result Aggregator collects all partial results.
7. Aggregator merges, sorts, and processes partial results into a single response.
8. Final combined result is sent back to the client.
9. Optionally, the result is cached for future similar queries.
Database Schema
Data is horizontally partitioned by shard key. Each shard contains the same schema but only a subset of rows. No cross-shard foreign keys exist. Entities include: - Records (sharded by key) - Metadata for shard mapping Relationships are 1:N within shards; cross-shard joins are handled at query layer.
Scaling Discussion
Bottlenecks
Query Router becomes a bottleneck handling all query parsing and coordination.
Network latency and bandwidth between Query Router and shards.
Result Aggregator delays when merging large partial results.
Individual shards overloaded by heavy sub-query load.
Cache invalidation complexity for cross-shard data.
Solutions
Scale Query Router horizontally with load balancers and stateless design.
Use efficient binary protocols and compression to reduce network overhead.
Parallelize result aggregation and stream partial results to client.
Shard data evenly and monitor shard load to rebalance if needed.
Implement fine-grained cache invalidation and TTL policies.
Interview Tips
Time: Spend 10 minutes understanding requirements and clarifying assumptions, 20 minutes designing architecture and data flow, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing.
Explain how query routing and rewriting works for cross-shard queries.
Discuss trade-offs between consistency, latency, and complexity.
Highlight importance of parallelism and result aggregation.
Mention caching strategies and their challenges.
Address scaling bottlenecks and mitigation techniques.