When the data you need lives on different shards: scatter-gather, and ways to avoid it.
A cross-shard join combines rows from tables whose data lives on different shards. On one database a join is routine. Across shards it becomes slow, costly and complicated.
On one database, orders JOIN users is easy. The database has both tables on its disk, picks an index, and returns the result. After sharding, the users table might be split by user_id while orders is split by order_id. Now the orders of one user sit on many shards, and the user row sits on yet another.
There is no machine that holds everything the query needs. Something has to gather the pieces. That something is usually a coordinator (a router, a proxy, or your own application code).
| Approach | How it works | Cost |
|---|---|---|
| Scatter-gather | Send the query to every shard, then merge, sort and limit at the coordinator | Load and latency grow with the number of shards |
| Two-step lookup | Fetch the main rows, then fetch the related rows by key from the other shards | An extra round trip for each step |
| Broadcast join | Send the small table to every shard so each can join locally | Fine for small tables, wasteful for big ones |
| Shuffle join | Re-send rows between shards so matching keys meet on the same machine | Heavy network traffic, used by analytic engines |
| Application-side join | Your code fetches both sides and joins in memory | Simple, but you own the batching and correctness |
| Technique | Idea | Price |
|---|---|---|
| Co-locate | Give related tables the same shard key (orders and order_items by customer_id) | One big customer can make one shard large |
| Replicate small tables | Keep a full copy of reference data on every shard | Updates must reach every copy |
| Denormalize | Store the needed user name inside each order row | Copies can go stale, and writes touch more data |
| Materialize | Precompute the joined result and refresh it on a schedule | Results are slightly old |
| Analytics store | Stream changes (CDC) into a warehouse or search index for cross-entity questions | Eventually consistent, one more system |
A tiny model: 12 users and 30 orders made by a fixed rule, spread over 4 shards. "Rows shipped" counts rows sent from a shard to the coordinator (and the lookup rows of a second round). With millions of rows the gap between the two layouts becomes enormous. The data is invented for teaching.
| Term | Meaning in easy words |
|---|---|
| Cross-shard join | A join between rows that live on different shards |
| Coordinator | The router or proxy that sends a query to shards and merges the answers |
| Scatter-gather | Ask every shard, then collect and merge the answers |
| Co-location | Keeping related rows on the same shard by using the same shard key |
| Broadcast table | A small table copied in full to every shard |
| Shuffle | Re-sending rows across the network so matching keys meet |
| Denormalization | Storing a copy of related data inside a row to avoid a join |
| Materialized view | A saved result of a heavy query, refreshed from time to time |
| Distributed transaction | A change that must succeed or fail together on several shards |
| Partial aggregate | A sub-total computed on one shard, which must be added to the others |
| Change data capture | Reading a database's change log to feed other systems |
| Choice | We gain | We lose |
|---|---|---|
| Co-locate by parent key | Local joins and local transactions | Uneven shards if one parent is huge |
| Replicate a small table | No lookup cost | Updates to many copies and a short stale window |
| Denormalize | One read per page | More storage and write work, and copies can drift |
| Join in the application | Full control and simple infrastructure | More code, and the app must batch well |
| Warehouse for cross-entity queries | Live shards stay simple | Results lag behind and one more system to run |
| Scatter-gather | Works for any query | Cost grows with the shard count |