Why your first query on a client's database gets revenue wrong
The table you are reading may be a single shard, customer data may live in another database, or the region column may be a stale copy nobody updates.
In brief
- Federation splits by function, while sharding splits one table into parts that share a schema. Once a single table grows too large, federation stops helping and sharding is the option left.
- The shard key is the most important design decision and very expensive to change, so it tells you which axis the client usually queries along.
- A cross-shard query is only as fast as the slowest shard, and both kinds of split make JOINs harder, so map the data before you write a SELECT.
Your first week at a retail client. You have read-only access and need to build an agent that answers “revenue last month by region”. You write SELECT ... JOIN customers ... GROUP BY region, run it, and get a number so far below the finance report that nobody can explain it.
The query has no syntax error, and nothing is wrong with the database. The orders table you are reading may be only one shard. customers may sit in a different database altogether, or the region column in orders may be a copy made long ago and never updated since.
Working out which, if any, of these is happening means telling two ways of splitting a database apart. Federation splits databases by function: users in one place, orders in another. Sharding spreads the data of a single table across several databases that share a schema, each holding its own subset.
When one function or one table grows too large, federation stops paying off, and sharding becomes the option. For an FDE, though, the practical question is rarely “which should we choose”. It is what the client already chose, and how that changes your query.
Federation vs sharding: how do they differ?
The table below is the quickest way to recognize each technique when you open a client’s system.
| Criterion | Federation | Sharding |
|---|---|---|
| Split by | Business function | Rows, each shard holds a subset |
| Schema | Each database has its own schema | Every shard has the same schema |
| Typical traces | Each database named after a domain: users, products, orders | Several identically structured databases with numbered names |
| JOIN | Combining data across two databases gets harder | A cross-shard query is as slow as the slowest shard |
| When it stops helping | When one function or one table grows too large | When data piles up unevenly on a few shards |
| What your query must do | Combine in the application layer or a warehouse | Fan out, then aggregate with SUM and COUNT |
Both require changes to application logic, according to the system-design-primer. Federation is also described as a way to make many physical databases look like one logical database, so do not be surprised if a client’s data-access layer hides the fact that the data lives in several places.
Why a client’s database is no longer one block
MongoDB points out that SQL databases have traditionally been scaled vertically, by adding CPU, storage or memory to a single server. When one server can no longer carry the load, the client has to split its data across several places, and federation and sharding are both ways of doing so.
A third technique often comes along: denormalization, a strategy applied to an already normalized database to improve performance. It makes reads faster at the cost of some write performance.
The three tend to appear together. Azure itself recommends that sharded designs denormalize so that entities often queried together, such as customers and their orders, sit on the same shard. When you find one technique, go looking for the other two.
The shard key tells you which axis the client queries along
Azure calls the shard key the most important design decision. Changing it later usually means moving all the data to a new shard layout, an expensive and risky job on a live system. The shard key therefore shows which axis the client’s engineering team bet most queries would follow.
If the client shards by customer_id, a question such as “orders for customer X” lands on exactly one shard. Your question, revenue by region across all customers, has to go through every shard. Azure notes that even when the queries run in parallel, overall latency is set by the slowest shard.
The sharding strategy is also worth asking about. Azure lists four: lookup (also called directory-based, which uses a map to route requests to the shard holding the data), range, hash and geographic. In the lookup approach, the shard map is usually the first document to ask for.
If the client splits with hash(key) mod N, you can work out the cost of adding a shard yourself. Going from 4 to 5 shards, a key stays put only if h mod 4 equals h mod 5.
Because 20 is the least common multiple of 4 and 5, only the 20 possible values of h mod 20 need checking: four of them, 0, 1, 2 and 3, satisfy the condition, and the other 16 do not.
If the hash is evenly distributed, only about 4/20 of keys stay put, so roughly 80% of keys have to move. The figure matters because while data is being moved, an order may sit temporarily on the old shard, the new shard, or both. Ask whether a migration is under way before trusting any total.
A worked example: revenue by region
Back to the hypothetical retail client. After an afternoon reading connection strings and talking to the DBA, you have the picture: orders is sharded by customer_id across 3 shards. customers and products sit in two separate databases, federation-style. orders has a customer_region column, copied from customers when each order is created.
Thanks to that duplicated column, you do not need a JOIN to the customer database, which is the whole point of denormalizing. What remains is to fan out to each shard and combine the results:
from collections import defaultdict
SHARDS = ["orders_shard_0", "orders_shard_1", "orders_shard_2"]
SQL = """
SELECT customer_region AS region,
SUM(total) AS revenue,
COUNT(*) AS n_orders
FROM orders
WHERE order_date >= %s AND order_date < %s
GROUP BY customer_region
"""
def revenue_by_region(start, end):
agg = defaultdict(lambda: {"revenue": 0, "n_orders": 0})
for dsn in SHARDS:
for region, revenue, n in run_query(dsn, SQL, [start, end]):
agg[region]["revenue"] += revenue
agg[region]["n_orders"] += n
for r in agg.values():
r["avg_order"] = r["revenue"] / r["n_orders"]
return dict(agg)
The detail to watch is the line that computes avg_order. Each shard returns only SUM and COUNT, and the average is calculated after the results are combined.
Say shard 0 has 10 orders averaging 100 and shard 1 has 1,000 orders averaging 50. The average of the two averages is 75, while the correct figure is (1,000 + 50,000) / 1,010, roughly 50.5.
Now the denormalized column. customer_region is the customer’s region at the time of the order. If a customer moves from the North region to the South, their old orders still carry the North label. That is a business question: does finance want revenue by region at the time of purchase, or by current region? Ask before the agent answers on their behalf.
Suppose they also want revenue by product category, and category exists only in the products database. This is where federation bites, because JOINs across two databases are harder. A practical approach is to pull the product_id → category mapping table (usually small) into memory or a warehouse, then combine in the application layer.
Five steps before the first query
First, map the landscape: how many databases there are, which function each holds, and which tables are sharded. Connection strings, ORM configuration files and names such as orders_shard_07 are the quickest clues. Second, find the shard key and the sharding strategy, and ask whether any data migration is running.
Third, list the duplicated columns and trace each to its source: when it is updated, by which job, and whether it lags.
Fourth, sort every business question into one of three groups: touches a single shard, needs a fan-out, or needs data combined from several functional databases.
Finally, agree with the client on data freshness: how far a figure aggregated from several places may lag the source systems. That agreement should be in writing before the agent answers its first question.
Mistakes FDEs often make
The most common is believing you are reading all the data when you are reading one shard. The second is taking an average of averages, or running COUNT(DISTINCT) on each shard and adding the results, when the same value can appear on several shards.
The third is harder to spot: running a heavy fan-out against production at peak hours. Data can be unevenly distributed, so one “hot” shard can slow both your query and the client’s system. Ask whether a read replica exists before you run anything.
The last is proposing, in the first week, to “consolidate everything into one database to keep it tidy”, or the reverse, to “add shards to speed things up”. Azure says sharding brings significant and permanent complexity, and lists cases where it is a poor fit, including when the bottleneck is on the read side. A client’s system is the product of real trade-offs.
Is federation a kind of sharding?
Not quite, but do not argue terminology with the client. A 2007 article on High Scalability classed federation and sharding as two specific cases of partitioning, so clients may use the three words interchangeably. Ask whether the data is split by function or by row, then write it down correctly yourself.
Is denormalization a sign of poor design? Usually not. A duplicated column tells you the system is optimized for reads, and your job is to find the source column.
On a CV, a line such as “built a fan-out query layer across 3 shards and agreed with the client how to handle duplicated columns and data lag” says far more than “knows sharding”.
Next time you are given access to a client’s database, do not write a SELECT straight away. Spend an hour drawing the shards, the functional databases and the duplicated columns, because most of the right answers are there.
Was this article useful?
Thanks for the feedback!
6 sources
- Sharding Pattern - Azure Architecture Center | Microsoft Learn · 2026-04-02
- System Design: Database Federation · 2022-09-12
- GitHub - donnemartin/system-design-primer: Learn how to design large-scale systems.
- Denormalization - Wikipedia
- NoSQL Vs SQL Databases | MongoDB
- Is there a difference between partitioning and federation and sharding? · 2007-09-10