| Users / Data Size | 100 Users | 10K Users | 1M Users | 100M Users |
|---|---|---|---|---|
| Number of shards | 1-2 shards | 10-20 shards | 100-200 shards | 1000+ shards |
| Cross-shard query frequency | Rare, simple queries | Occasional, moderate complexity | Frequent, complex queries | Very frequent, highly complex queries |
| Latency impact | Negligible | Noticeable delays | Significant latency increase | High latency, possible timeouts |
| Data consistency challenges | Minimal | Moderate | High | Very high, complex coordination |
| Network overhead | Low | Moderate | High | Very high |
| System complexity | Low | Moderate | High | Very high |
Cross-shard queries in HLD - Scalability & System Analysis
As the number of shards grows, the first bottleneck is the query coordinator that must gather and merge data from multiple shards. This causes increased latency and network overhead. The coordinator's CPU and memory can become overwhelmed handling many parallel cross-shard queries. Also, data consistency and transaction management across shards become challenging, slowing down responses.
- Query Routing: Design queries to target as few shards as possible by improving shard keys and data locality.
- Denormalization & Aggregation: Pre-aggregate or duplicate data to reduce cross-shard joins.
- Asynchronous Processing: Use background jobs or eventual consistency to avoid blocking user queries.
- Distributed Query Engines: Use specialized systems that parallelize and optimize cross-shard queries efficiently.
- Caching: Cache frequent cross-shard query results to reduce load.
- Horizontal Scaling: Add more query coordinators and shard servers to distribute load.
- Sharding Strategy: Revisit shard keys to minimize cross-shard queries by grouping related data.
Assuming 1M users with 100 shards:
- Average queries per second (QPS): 10,000
- Cross-shard queries: 20% -> 2,000 QPS involving multiple shards
- Each cross-shard query touches ~5 shards -> 10,000 shard queries/sec
- Network bandwidth: 10,000 shard queries x 10 KB data = ~100 MB/s
- Coordinator CPU: Must handle merging 2,000 complex queries/sec, needs multiple servers
- Storage: Each shard stores ~10,000 users' data, scaling storage horizontally
Start by explaining how sharding improves write/read scale but introduces cross-shard query challenges. Identify the coordinator as the bottleneck. Discuss trade-offs between latency, consistency, and complexity. Propose solutions like better shard keys, caching, and distributed query engines. Use numbers to justify your approach and show understanding of system limits.
Your database handles 1000 QPS. Traffic grows 10x with many cross-shard queries. What do you do first?
Answer: The first step is to reduce cross-shard query load by improving shard keys to localize queries or add caching to avoid repeated cross-shard fetches. Then, scale query coordinators horizontally to handle increased query merging load.
Practice
Solution
Step 1: Understand what shards are
Shards are parts of a database split to store data separately for scalability.Step 2: Identify the role of cross-shard queries
Cross-shard queries combine data from multiple shards to answer complex requests that need data from more than one shard.Final Answer:
To retrieve data from multiple shards to answer a single complex request -> Option DQuick Check:
Cross-shard queries = combine data from shards [OK]
- Confusing cross-shard queries with data backup
- Thinking cross-shard queries split shards
- Assuming cross-shard queries encrypt data
Solution
Step 1: Identify query processing roles
The coordinator handles query distribution and result merging across shards.Step 2: Match roles to components
Shard manager manages shards but does not split/merge queries; cache server stores data temporarily; load balancer distributes traffic but not queries.Final Answer:
Coordinator -> Option AQuick Check:
Coordinator splits and merges queries [OK]
- Confusing shard manager with coordinator
- Thinking cache server merges queries
- Assuming load balancer handles query splitting
Solution
Step 1: Understand parallel query execution
Queries run in parallel, so total time is the longest shard response time.Step 2: Identify the longest response time
Shard times are 100ms, 120ms, and 80ms. The longest is 120ms.Final Answer:
120ms -> Option CQuick Check:
Parallel time = max shard time = 120ms [OK]
- Adding all shard times instead of taking max
- Choosing shortest shard time
- Confusing sequential with parallel execution
Solution
Step 1: Analyze incomplete results cause
Incomplete results usually mean some shard data was not included in the final output.Step 2: Identify coordinator role in merging
The coordinator merges shard responses; failure here causes missing data.Final Answer:
Coordinator failed to merge all shard responses -> Option AQuick Check:
Incomplete results = merge failure [OK]
- Blaming shard key distribution for missing data
- Assuming cache overload causes missing data
- Thinking load balancer affects query merging
Solution
Step 1: Identify strategies to reduce latency
Parallel query execution via coordinator reduces wait time; caching avoids repeated work.Step 2: Evaluate options
Use a coordinator to parallelize queries and implement caching of frequent results uses both parallelism and caching, best for latency. Others increase complexity or remove coordination, causing delays.Final Answer:
Use a coordinator to parallelize queries and implement caching of frequent results -> Option BQuick Check:
Parallel + caching = lower latency [OK]
- Thinking bigger shards reduce latency
- Running queries sequentially increases latency
- Removing coordinator causes uncoordinated delays
Ready to prove it?
Pass the test and this pattern moves unknown
