Shard Key Design for Multi-Tenancy
Parent: MongoDB Multi-Tenancy · Published reference · snapshot 2026-10-23 · skill mongodb-cohort-18121faaf93a
↓ Facts as markdown↓ Portable .llms packagent context libraries
Source-anchored research for Shard Key Design for Multi-Tenancy. Run status: COMPLETED.
These notes link each claim to its source. Some citations identify archived research reports that are no longer hosted on this site. A published reference means the content is available; it does not certify independent review or accuracy.Read the editorial policy and follow the sources before relying on a claim.
Structure and components
- Evaluate candidate keys against distinct-value cardinality, value frequency and tenant skew, insert monotonicity, and common query shapes. High cardinality alone does not guarantee balanced load. Queries that omit the shard key or the prefix of a compound shard key require scatter-gather across the shards that may contain matching data. (high confidence) Sources: https://www.mongodb.com/docs/manual/core/sharding-choose-a-shard-key.md ; https://www.mongodb.com/docs/manual/core/sharded-cluster-query-router.md [source 1] [source 2] [source 3] [source 4] [source 5]
How it works
- Choose the key and query shapes together. Queries that include the shard key or a prefix of it can be targeted to a subset of shards, while queries without the shard key are broadcast to all shards. If the data has high-frequency values, a compound key that adds a unique or low-frequency field can help; with a tenant-first prefix, queries filtering on tenant can be targeted. Sources: https://www.mongodb.com/docs/manual/core/sharding-choose-a-shard-key/ ; https://www.mongodb.com/docs/manual/core/sharded-cluster-query-router/ [source 1] [source 2] [source 3] [source 4]
- For a shared-collection multi-tenant design, store tenantId on every document and include it in every query. When tenant-scoped reads and writes dominate, including tenantId in the shard key aligns routing with the application’s tenant boundary; the boundary remains logical and must be enforced in application code. (medium confidence) Sources: https://www.mongodb.com/docs/atlas/build-multi-tenant-arch.md ; https://mongodb.com/docs/vector-search/deployment/multi-tenant-architecture/ ; https://www.mongodb.com/docs/manual/core/sharded-cluster-query-router.md [source 1] [source 2] [source 3] [source 4]
- When a shard key has high-frequency values, a compound key that adds a unique or low-frequency field can help. Queries that include a prefix of the shard key (such as a tenantId prefix) can be routed to a specific shard or set of shards, so tenant-only queries may reach more than one shard if a tenant's ranges span shards. A compound hashed key can combine a non-hashed prefix with one hashed field. Sources: https://www.mongodb.com/docs/manual/core/sharding-choose-a-shard-key/ ; https://www.mongodb.com/docs/manual/core/sharded-cluster-query-router/ ; https://www.mongodb.com/docs/manual/core/hashed-sharding/ [source 1] [source 2] [source 3] [source 4] [source 5] [source 6] [source 7]
Problems, failure modes and limitations
- With a range shard key consisting only of tenantId, all documents for a tenant share one shard-key value. Each unique shard key value can exist on no more than a single chunk at any given time, so a very large or active tenant cannot be split by tenantId alone and its chunk can become an indivisible bottleneck on one shard. Sources: https://www.mongodb.com/docs/manual/core/sharding-choose-a-shard-key/ [source 1] [source 2] [source 3] [source 4] [source 5] [source 6] [source 7]
- Hashed sharding gives a more even data distribution at the cost of targeted operations: ranged queries on the hashed field are more likely to be broadcast, though equality matches can be targeted to a single shard. Hashing suits monotonically changing values such as ObjectId or timestamps; hashing tenantId alone still gives each tenant one shard-key value (one chunk per value), so it cannot split an unusually large tenant. A compound hashed key with a non-hashed prefix and one hashed field is supported, with the prefix supporting zone ranges and the hashed field giving even distribution. A key that omits the queried field, such as hashed _id, is broadcast for queries that lack the shard key. Sources: https://www.mongodb.com/docs/manual/core/hashed-sharding/ ; https://www.mongodb.com/docs/manual/core/sharding-choose-a-shard-key/ [source 1] [source 2] [source 3] [source 4] [source 5] [source 6]
Facts and statements
- Evaluate a shard-key candidate by cardinality, frequency of each value, monotonicity, and the common query patterns. High cardinality alone does not guarantee even distribution. (high confidence) [source 1] [source 2] [source 3]
- For a high-frequency shard key value such as a tenant ID, MongoDB recommends a compound key that adds a unique or low-frequency field, since a chunk holding a single shard key value cannot be split. Yorkie's MongoDB sharding design documents a similar pattern: after limited project_id values concentrated data on a few chunks, it proposes a composite shard key (project_id, key) so large chunks can be split by key values. Sources: https://www.mongodb.com/docs/manual/core/sharding-choose-a-shard-key.md ; https://www.mongodb.com/docs/manual/core/sharding-data-partitioning.md ; https://github.com/yorkie-team/yorkie/issues/673 [source 1] [source 2] [source 3] [source 4] [source 5]
- For insert-heavy workloads with monotonic suffix values, a compound hashed key such as {tenantId: 1, _id: "hashed"} can distribute inserts more evenly within the tenant-prefixed key space. Hashed shard keys trade range locality on the hashed field for more even distribution; MongoDB supports one hashed field in a compound hashed key. (high confidence) (verified as of 2026-10-19) [source 1] [source 2]
- In shared collections, tenantId is logical segmentation: queries must filter by tenantId and the application tier must enforce tenant access controls. MongoDB's Vector Search guidance also uses tenant_id as a prefilter for tenant-scoped searches. (high confidence) Sources: https://www.mongodb.com/docs/atlas/build-multi-tenant-arch.md ; https://mongodb.com/docs/vector-search/deployment/multi-tenant-architecture/ [source 1] [source 2] [source 3]
- Zones can associate shard-key value ranges with selected shards, supporting placement policies such as isolating a specific subset of data on a specific set of shards. A chunk that represents a single shard key value cannot be split, so repeated tenantId values in a tenant-only key do not become splittable. Sources: https://www.mongodb.com/docs/manual/core/zone-sharding.md ; https://www.mongodb.com/docs/manual/core/sharding-data-partitioning.md [source 1] [source 2] [source 3] [source 4] [source 5]
- A non-MongoDB outlier-handling example is Datadog Husky: it assigns a variable shard count per tenant from observed traffic and changes future placements as load changes. This illustrates an application-managed placement strategy, not behavior provided by MongoDB's native shard key. (low confidence) [source]
- MongoDB zones can constrain shard-key ranges to designated shards for deliberate placement. For tenant placement, the range boundaries must use fields in the shard key (for a compound key, the range must include the key prefix); if the key field is hashed, zone ranges refer to hashed values and do not have predictable routing relative to the original tenantId values. Sources: https://www.mongodb.com/docs/manual/core/zone-sharding/ [source 1] [source 2] [source 3] [source 4]
- Before committing to a shard key, MongoDB’s analyzeShardKey can use sampled queries to report key cardinality, frequency, monotonicity, query targeting, and read/write distribution. MongoDB documents this capability as available starting in version 7.0. (medium confidence) (verified as of 2026-10-19) [source 1] [source 2] [source 3]
Children
- Zone sharding for tenant placement (frontier)
- Resharding and shard-key refinement (frontier)
Frontier under this node: Resharding and shard-key refinement, Zone sharding for tenant placement