0
0
HLDsystem_design~7 mins

Cross-shard queries in HLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When data is split across multiple shards, running queries that need to access multiple shards at once can cause slow response times and inconsistent results. Without a proper approach, the system may have to query all shards sequentially or in parallel without coordination, leading to high latency and complex result merging.
Solution
Cross-shard queries coordinate querying multiple shards in parallel and then merge the results to present a unified response. This involves a query router or coordinator that breaks down the query, sends sub-queries to relevant shards, collects partial results, and combines them before returning to the client. This approach balances load and maintains consistency across distributed data.
Architecture
Client App
Client App
Query Router
Query Router
Shard 1
Result Merger & Sort
Result Merger & Sort
Unified Response
Unified Response

This diagram shows a client sending a query to a query router, which splits the query to multiple shards. Each shard processes its part and returns results to a merger component that combines and sorts them before sending a unified response back to the client.

Trade-offs
✓ Pros
Enables scalable querying across distributed data by parallelizing requests to shards.
Improves response time compared to sequential shard querying.
Maintains data consistency by coordinating query execution and result merging.
Supports complex queries that span multiple shards transparently to the client.
✗ Cons
Increases system complexity due to query routing, coordination, and result merging logic.
Merging large result sets can cause memory and CPU overhead on the coordinator.
Latency depends on the slowest shard response, potentially causing bottlenecks.
Use when your dataset is sharded for scale and you need to support queries that span multiple shards, especially when read traffic exceeds thousands of requests per second or data size exceeds single-node capacity.
Avoid if your queries mostly target single shards or if your system handles low traffic (under 1,000 queries per second), where the added complexity and overhead of cross-shard coordination outweigh benefits.
Real World Examples
Google
Google Bigtable uses cross-shard queries to scan data distributed across tablets, merging results to provide consistent views for large-scale analytics.
Twitter
Twitter shards user timelines and uses cross-shard queries to aggregate tweets from multiple shards when users follow many accounts.
Amazon
Amazon DynamoDB supports cross-shard queries by routing requests to multiple partitions and merging results for global secondary indexes.
Alternatives
Single-shard queries
Queries are restricted to a single shard, avoiding cross-shard coordination.
Use when: When queries can be designed to target one shard only, reducing complexity and latency.
Denormalization and pre-aggregation
Data is duplicated or aggregated to avoid cross-shard queries by storing combined views.
Use when: When read performance is critical and data update frequency is low enough to allow denormalization.
Distributed SQL engines
Use specialized distributed query engines that handle cross-shard queries with optimized execution plans.
Use when: When complex SQL queries across shards are frequent and require advanced optimization.
Summary
Cross-shard queries solve the problem of querying data distributed across multiple shards by coordinating parallel queries and merging results.
This pattern improves scalability and supports complex queries but adds system complexity and potential latency bottlenecks.
It is best used when data size and query patterns require distributed access beyond single shards.