Skip to content

Cross-shard queries

Once the data is split, a query has to find the pieces it needs. If the query names the shard key, such as “the orders of customer 17” when the table is sharded by customer, the router computes the shard and asks only that one. MongoDB calls this a targeted query. If the query does not name the key, such as “the orders placed today”, the router cannot tell which shards hold matching rows, so it asks all of them and merges the answers. MongoDB’s documentation calls that scatter-gather, and says that queries which involve several shards for each request are less efficient and do not scale linearly when shards are added. Vitess describes the same behaviour: with no vindex that fits the query, the request is scattered to every shard.

This is why the shard key lesson asked whether the frequent queries contain the key. A key chosen only to spread the data evenly can make the most common query a scatter-gather.

Mental model. A query that names the shard key costs one shard’s work. A query that does not costs the work of every shard, and waits for the slowest. The more shards there are, the worse the second gets, and the opposite of what sharding was for.

Take a cluster of 10 shards, each able to serve 1,000 operations a second, so 10,000 shard operations a second in all. Targeted queries use one operation each: the cluster can serve 10,000 queries a second, and adding a shard adds 1,000. A query that scatters to every shard uses 10 operations: the cluster can serve 1,000 queries a second, which is what one shard could do alone. Adding a shard raises the cost of each query by one operation and the capacity by one shard, and the number of queries per second stays at 1,000. These are illustrative numbers, and the arithmetic holds for any capacity: with N shards, a scatter-gather query turns N shards into the throughput of one.

Latency suffers too, because the query is as slow as its slowest shard. Suppose each shard answers within its own 99th percentile 99 times in 100, and that shards are slow independently of one another. This is an assumption, and shards that share a network or a noisy neighbour are not independent. The chance that all N answers are fast is 0.99^N, so the chance that at least one is slow is:

Shards asked Chance that at least one answer is slow
1 1%
10 9.6%
100 63.4%

A query that asks 100 shards is slower than a typical shard’s 99th percentile about two times in three. The percentiles lesson in the performance module explains why the slow tail dominates what a user sees, and this is the same effect, produced by the fan-out itself.

Check your understanding: cross-shard queries

This quiz needs JavaScript to check your answers.

Question 1 of 3

An orders table is sharded by customer id. Two queries run all day: one fetches all the orders of customer 17, the other lists all the orders placed today.

Which query is a scatter-gather, and why?

Choose one answer.

Question 2 of 3

A cluster has 8 shards. Each can serve 500 operations a second. Every query is a scatter-gather that asks all 8 shards, one operation on each.

How many such queries can the cluster serve per second?

Enter a number and choose its unit.

queries per second
Question 3 of 3

A transfer moves money from customer A to customer B. They live on different shards.

What does this write need that a write to one shard does not?

Choose one answer.

  • Choose the key for the query. If the frequent queries name the customer, shard by customer. Then every query for one customer is targeted. This is a trade: the same key may make another query, by date or by product, a scatter-gather.
  • Keep a lookup table. To find an order by its order number when the table is sharded by customer, keep a second table that maps an order number to its customer, so that the first step is a single-shard lookup and the second step is a targeted query. Vitess calls this a lookup vindex: a table, itself stored in the database, that maps a column value to the id that locates the row. Its documentation notes the price: extra work on every insert and delete, because the table has to be kept in step. That is the directory of lesson 1, built for one column.
  • Keep related data together. If an operation always touches a customer’s orders and addresses, give them the same shard key so that they live on one shard. Redis Cluster does it with hash tags: when a key contains a part in braces, only that part is hashed, so keys that share it share a slot. The specification gives {user:1000}.name and {user:1000}.surname as keys that can be used together in a multi-key operation.
  • Accept it for rare queries. A report that scans everything once a night can scatter. What matters is the share of the traffic that does.

What happens if a write touches two shards?

Section titled “What happens if a write touches two shards?”

A write to one shard is a local operation: the shard commits it or does not. Moving money from one customer to another, when the two customers live on different shards, changes data on two shards, and it has to succeed on both or on neither. No single machine can decide that alone, so the shards have to coordinate, which costs extra round trips on every such write and introduces new ways to fail: one shard commits and the other cannot be reached. The transactions module is about how databases handle this, and about what they give up.

The same reasoning applies to a join of two tables that are sharded by different keys. Each row on one side may match rows on every shard on the other side. Common answers are the ones above: shard both tables by the same key, so that matching rows live together, or copy the small table to every shard. In every case the design choice is to arrange the data so that the frequent operation touches one shard.

  • A query that names the shard key goes to one shard. One that does not is sent to all of them and merged, which is scatter-gather.
  • With N shards, scatter-gather turns the throughput of N shards into that of one: 10 shards of 1,000 operations a second serve 10,000 targeted queries a second, and 1,000 scatter-gather queries a second.
  • The query is as slow as its slowest shard. If each shard is slow 1% of the time, a query over 10 shards is slow 9.6% of the time and one over 100 shards 63.4% of the time, if the shards are independent.
  • Avoid fan-out by choosing the key for the query, keeping a lookup table for another column, or keeping related data on one shard, as Redis hash tags do.
  • A write that touches two shards needs coordination between them, which costs round trips and adds ways to fail.

Next: recap and quiz.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.