Scatter-Gather vs. Lookup Shards for Global Secondary Indexing
0 reputation · 22 Sept 2023, 19:47 UTC
0 reputation · 22 Sept 2023, 19:47 UTC
In Vitess, managing queries that lack the primary sharding key typically requires a choice between scatter-gather routing via VTGate or the implementation of a dedicated lookup shard to maintain a global secondary index.
Scatter-gather routing provides maximum query flexibility by broadcasting requests to all tablets, but it introduces significant tail latency and CPU overhead as the number of shards increases. Conversely, using a lookup shard enables targeted routing for secondary keys, reducing the load on the cluster at the cost of additional write amplification and the need to maintain consistency between the primary and lookup shards.
When designing for a growing cluster where predictable latency is a priority but write throughput is high, the trade-off between these two routing strategies becomes critical.
32275 reputation · 23 Sept 2023, 01:27 UTC
For clusters scaling beyond ten tablets where predictable tail latency is the priority, lookup shards are the superior choice. Scatter-gather routing suffers from the "long tail" effect, where the total response time is dictated by the slowest performing shard in the cluster. As the shard count increases, the probability of encountering a slow node (due to GC pauses, network jitter, or resource contention) increases linearly, causing p99 latency to degrade.
While lookup shards stabilize read latency, they introduce a write penalty. The decision to move away from scatter-gather depends on the ratio of reads to writes and the specific impact of write amplification.
| Metric | Scatter-Gather | Lookup Shard |
|---|---|---|
| Read Latency | Increases with shard count | Constant (Targeted) |
| Write Overhead | Minimal (Primary only) | High (Primary + Index) |
| Cluster Load | High CPU/Network broadcast | Localized to index shard |
There is no universal numeric threshold for write volume, as it depends on your hardware and the size of the secondary index. However, the overhead of a lookup shard typically outweighs its benefits when the write amplification causes the index shard to become the primary bottleneck for the entire system.
You have reached this threshold when:
To determine the optimal path for your specific workload, perform the following scoped tests:
vttablet metrics for the index shard. Compare the maximum sustainable writes per second against the scatter-gather baseline.Diagnostic Requirement: To refine this recommendation, please provide the current Read:Write ratio for the secondary key queries. If the workload is write-heavy (e.g., >10:1 writes to reads), the write amplification of a lookup shard may be prohibitive regardless of shard count.
Use comments to ask for clarification. Post a solution as an answer.
4,320 reputation · 22 Sept 2023, 21:09 UTC
Before committing to a lookup vindex, run a VExplain on the query. The plan will show whether VTGate is scattering to ALL tablets or routing to a single shard via a lookup. This quick check helps confirm that the query actually benefits from the vindex and that no implicit scatter is occurring.
Lookup tables are sharded themselves. If the indexed column has a skewed distribution, one shard can become a hotspot, re‑introducing tail latency. Monitor per‑shard CPU and IOPS and consider adding a consistent lookup variant if stale reads are unacceptable.