Hermes Wiki

Database Sharding Strategies

Concept

Sharding is horizontal partitioning across separate database instances, not within one — where a table partition still lives on a single server, a shard is a physically distinct database that owns a disjoint subset of the rows, and a routing layer decides which shard a given query hits. It's the technique reached for once a single database instance's vertical scaling ceiling (bigger CPU/RAM/disk on one box) is genuinely exhausted, not a default starting point.

A shard key — typically a tenant ID, user ID, or geographic region — determines which shard owns each row. How that key maps to a shard is the actual design decision:

  • Hash-based sharding — hash the shard key and mod by shard count to pick a shard. Gives near-even data distribution automatically, and is the right default for workloads that are primarily key-based point lookups. The cost: range queries ("all orders from last week") now have to fan out to every shard, since consecutive keys are deliberately scattered.
  • Range-based sharding — assign contiguous key ranges to shards (e.g., user IDs 1-1M on shard A, 1M-2M on shard B). Keeps range queries efficient (they hit one or a few adjacent shards), but risks hot shards — if IDs are assigned sequentially and new users are the most active, the newest shard takes disproportionate load.
  • Directory-based (lookup table) sharding — an explicit mapping service tracks which shard owns which key, decoupling the assignment logic from a formula entirely. Most flexible (arbitrary rebalancing without re-deriving a hash), but the lookup service itself becomes a critical dependency and potential bottleneck.
  • Geo/tenant sharding — shard by a business-meaningful dimension (region, tenant) rather than a technical hash. Common in multi-tenant SaaS and regulated industries, since it doubles as a data-residency and blast-radius control, not just a scaling technique.

Tradeoffs

Strategy Data distribution Range queries Rebalancing Hot-shard risk
Hash-based Even, automatic Expensive (fan-out to all shards) Hard — changing shard count reshuffles most keys Low
Range-based Depends on key distribution Cheap (hits few shards) Easier — split one range into two High if keys are sequential/time-correlated
Directory-based Fully controllable Depends on mapping design Easiest — just update the mapping Low, but lookup service is a new single point of failure
Geo/tenant-based Uneven by nature (tenant sizes vary) Cheap within a tenant/region Moderate — move whole tenants, not individual rows High for large ("whale") tenants

The pattern across all four: whatever axis makes rebalancing easy (directory, range) tends to make even distribution and hashing efficiency harder to guarantee automatically, and vice versa. There's no shard strategy that's simultaneously the easiest to rebalance and the most naturally even — pick which failure mode is more tolerable for the actual workload.

When to use / when not to

  • Reach for sharding only after read replicas, caching, and vertical scaling are genuinely exhausted — it's the most operationally expensive scaling lever, not the first one to pull.
  • Use hash-based sharding when the access pattern is dominated by point lookups on the shard key (get user by ID, get order by order ID) and range scans across the whole dataset are rare.
  • Use range-based sharding when range scans are the common case and the key isn't purely sequential/time-monotonic (or accept a rebalancing strategy for the hot newest shard if it is).
  • Use geo/tenant sharding when data residency, regulatory boundaries, or blast-radius isolation (one tenant's outage shouldn't touch another's data) matter as much as raw scale — this is the default shape in regulated multi-tenant Fintech/SaaS systems.
  • Avoid sharding a dataset that's still small enough to fit comfortably (with headroom) on a single well-specced instance plus read replicas — the cross-shard query and transaction complexity is a permanent tax paid from day one.

Common pitfall

Choosing a shard key that looks natural but produces a hot shard in practice — sharding by created_at month, or by an auto-incrementing user ID, both concentrate the most active (newest) data on the most recently created shard, defeating the whole point of distributing load. The shard key has to be chosen against the actual access pattern and growth shape, not just "some column that uniquely identifies the row."

Engineering Lens

Sharding is one of the clearest tests of tradeoff maturity in an architecture review, because every shard key choice trades away something concrete: hash-based buys even distribution but taxes every range query and every future rebalance; range-based buys cheap scans but risks a hot shard; directory-based buys flexibility but adds a new critical-path dependency. The Principal-level answer to "how would you shard this" is never just naming a strategy — it's naming which cross-shard operation (a join, a transaction, a report) becomes expensive or impossible as a direct consequence, and whether the business can live with that. In Fintech/Capital Markets specifically, shard-key choice often collides with regulatory data-residency requirements (EU customer data must stay in an EU shard) before it collides with a pure performance concern — recognizing that the constraint is sometimes legal, not technical, is itself a signal of Principal-level framing.

Sources

Hermes Wiki