Skip to main content
In a single-node database, joining related rows from two tables (for example, a “chirp” and the profile of the user who posted it) is simple: the database reads both tables locally and emits the combined rows.
A stylized database diagram titled "JOINS" showing two tables: "Chirps" (id, user_id, text) and "Users" (id, name). A dashed arrow illustrates a join producing a combined row linking the user Chris to the chirp "Who else is on #Chirp?".
When the database is sharded, the rows you want to join may reside on different machines. Cross-shard joins therefore often require transferring data across the network and coordinating work across shards, which adds latency and resource usage.
A dark schematic titled "Joins" showing four database shards (Shard 0–3) represented as outlined cylinders, with Shard 1 and Shard 3 highlighted and a dashed arrow indicating a join between records. Small labels above show sample user/chirp entries like "18 | Leo", "7 | 41 | Who else…" and "41 | Chris."
Common cross-shard join approaches
  • Route the query to the shard holding the “primary” side of the relation and perform remote lookups for missing rows (remote key lookups).
  • Fan-out (scatter) the query to multiple shards, gather results, and aggregate them; sometimes an intermediate shuffle of results is needed.
  • Use distributed join algorithms (for example, a repartition/repartition-join) that repartition intermediate results across nodes; this can be expensive for high-cardinality sets.
Because these approaches can be costly, many sharded systems restrict cross-shard joins or document them as expensive operations. Table — Cross-shard join patterns and trade-offs Design strategies to avoid expensive cross-shard joins Because cross-shard joins are expensive, a common design goal is to keep frequently-joined data on the same shard. The three primary strategies are:
  • Co-location
  • Denormalization
  • Replicating small reference tables
Co-location
  • Co-location means placing related rows together on the same shard. For example, if Chris’s user profile is stored on Shard 3, store Chris’s posts and likes on Shard 3 as well. That way, rendering Chris’s profile, posts, and like counts requires only local reads and joins.
Co-locating data that’s frequently accessed together minimizes cross-shard network overhead and gives the best read performance for those access patterns.
Denormalization
  • Denormalization copies frequently-read attributes into the table where the read happens. For example, when Leo (on shard 0) likes Chris’s post (on shard 3), you might copy Leo’s display name into the likes row stored on shard 3. Rendering the list of likers then requires no remote reads.
Denormalization speeds up reads but creates update complexity: profile updates must be propagated to denormalized copies, either synchronously, asynchronously, or by accepting eventual consistency.
Denormalization improves read latency but requires a strategy for keeping duplicated copies consistent (synchronous update, asynchronous propagation, or accepting eventual consistency).
A dark-themed diagram titled "DENORMALIZATION" showing example database tables (Users, Chirps, Likes) with entries for a user named "Chris." To the right, a mobile profile UI displays Chris's timeline and the corresponding chirp posts, with a highlighted storage shard below.
Replicating small, read-mostly tables
  • For very small lookup tables (country codes, currency symbols, status enums), keep a full copy on every shard so queries can be answered locally without cross-shard coordination.
Example — small lookup table (country codes) Replicated across shards: This pattern works well when the table is small and relatively static.
A dark diagram titled "SMALL TABLES" showing a small "country_codes" table with entries like IN→India, US→United States, GB→United Kingdom. Below it are four database shards (Shard 0–3), each labeled with "country_codes."
When cross-shard joins are unavoidable Some queries inherently need data from many shards. Those joins will be slower and more complex (scatter/gather, remote lookups, or repartitioning). Accepting that cost is a trade-off of sharding and must be weighed against the benefits: higher write scalability, better distribution of load, and easier horizontal scaling. In practice you usually don’t build full sharding primitives from scratch. Many distributed databases provide partitioning/hash rings, bucket maps, migration tools, and routing layers to help manage shards. Choose a system that aligns with your workload, consistency needs, and operational constraints — and that offers primitives to support co-location, replication, and cross-shard queries.
A dark, stylized diagram of a distributed database: a large yellow-outlined cylinder contains four labeled modules — Hash Ring, Bucket Map, Migration, and Routing Layer. Each module is shown as a small card with simple orange and white icons and check marks.
Quick reference — when to use which strategy Links and references
  • [Distributed SQL databases and sharding concepts — overview articles and docs]
  • For examples of distributed databases that provide partitioning and routing primitives, see CockroachDB, Google Spanner, and YugabyteDB.
  • When choosing a solution, evaluate your read/write patterns, acceptable consistency model, and operational complexity.

Watch Video