Skip to main content
When a service receives a request for a user’s data, some component must compute which shard holds that user, then forward the request there. That component is the routing layer — its single responsibility is to map a user identifier (or shard key) to the correct shard and forward the request.
A diagram of a routing layer that hashes a user_id (41) to a numeric hash (873899871) and computes shard = hash ÷ 4 = 3. It shows the app routing requests to one of four shards (0–3) and storing the user in shard 3.
Common search terms: database sharding, routing layer, shard mapping, shard hotspot, celebrity problem, key salting. Routing logic can live in three typical places. Each placement has different operational trade-offs.
  1. Inside the application (each microservice computes routing)
    If each microservice contains the routing logic, services compute the hash and connect directly to the chosen shard. For example, Profile, Feed, and Likes services each perform the hash-and-modulo and contact the target shard directly. This minimizes latency because there is no extra hop in the request path.
A diagram showing a user labeled "Chris" (user_id 41) sending requests to an app server with three services: Profile, Feed, and Likes. Each service hashes the user_id and takes remainder ÷4 to route requests across four database shards (Shard 0–3), shown with dashed arrows.
However, distributing routing logic creates a maintenance burden: if the sharding rule changes (for example, from 4 shards to 5), every service must be updated. A single outdated service will compute the old mapping and send requests to the wrong shard.
A schematic showing an app server routing a user (labeled "Chris")'s profile, feed, and likes using a hash and modulo-5 sharding strategy. The likes update is routed to shard 3, where the write is indicated as failing.
  1. As a separate proxy service (centralized router)
    A single proxy sits between application services and shards. Each service forwards requests to the proxy, which decides the target shard. This centralizes the sharding rule and simplifies updates (change it once in the proxy). MongoDB’s mongos is a canonical example of this design: https://www.mongodb.com/docs/manual/reference/program/mongos/
The trade-off is an extra network hop and the risk of the proxy becoming a bottleneck. In practice, teams run multiple proxy instances behind a load balancer for redundancy and scalability.
  1. In the database (shard-aware nodes)
    Some databases accept client connections to any node and internally route to the correct shard. This minimizes application work, but you must accept the database’s routing semantics and limitations.
Table — routing placement: pros and cons Whichever placement you choose, the routing layer’s objective is the same: forward each request to the shard that holds the needed data. Why does a shard get overloaded if hashing is uniform? Enter the celebrity problem. Imagine Lena, a pop star with 40,000,000 followers. If her user ID hashes to shard 2, then when she publishes a post, millions of followers try to read it at once. Suppose each shard can handle ~100,000 reads/sec — shard 2 becomes saturated while other shards remain underutilized. This hotspot caused by an extremely popular key is known as the celebrity problem.
A schematic diagram showing how an app routes users by hashing the user_id and taking a remainder mod 4 to evenly assign them to four database shards. It highlights a user named Lena (500) whose data maps to shard 2, with notes about 40,000,000 followers and shard 3 handling 100K reads/sec.
Teams commonly combine three mitigations to handle celebrity hotspots: 1 — Give the celebrity a dedicated shard (directory-based override)
Maintain a small directory of overrides mapping specific hot user IDs to dedicated shards. The routing layer checks this directory first; if the request is for a mapped user (e.g., Lena), route it to the dedicated shard. Otherwise use the normal hash-based mapping. This isolates the celebrity’s load from other users on the original shard.
A dark-themed system diagram showing a user ("Lena", user_id 500) routed through a directory/routing layer that hashes the user_id and computes a remainder to pick one of multiple database shards. The flow points to Shard 4 (highlighted, labeled ~100K reads/sec) where Lena's data is stored.
Dedicated shards isolate traffic from other users on the same shard. Note that a single dedicated machine still has a throughput limit (~100K reads/sec in our example); if peak demand exceeds that, combine dedicated shards with caching or salting.
2 — Cache popular reads heavily
Most celebrity traffic is read-heavy: a cache (CDN, in-memory cache like Redis or a dedicated caching tier) in front of the databases will serve many follower requests without hitting the shard. Use aggressive TTLs, cache invalidation patterns for writes, and layered caching near users (CDN + application edge cache) where appropriate.
3 — Spread writes via key salting (logical sharding of a hot key)
For write-heavy counters or append-only lists (e.g., likes or comments), split a hot key into N salted keys: userID-0, userID-1, …, userID-(N-1). Route each incoming write to one salted key (e.g., chosen uniformly at random). Because the shard key changes, different salted keys map to different shards and distribute write load.
Important clarifications about salting:
  • The N salted keys are logical; they do not require N new machines. Salted keys map onto the existing shard set.
  • Lena’s profile and posts remain on her (possibly dedicated) shard. Only high-volume per-post data (likes, counters) is sharded via salting.
  • To read a total count, you must query all N salted keys and sum the results, increasing read complexity.
A schematic diagram showing a caching and routing layer that salts keys and hashes user IDs to distribute requests across multiple database shards. It includes examples of salted keys, a directory mapping (user 500 → shard 4), and arrows indicating read traffic to shards (shard 4 noted at 100K reads/sec).
Salting increases write parallelism but requires extra reads and aggregation for total counts. Apply salting selectively to keys that are demonstrably hot.
Typical routing-layer decision flow (combined strategies)
  • First, a read request checks the cache. If cached, return immediately — the routing layer is bypassed.
  • On a cache miss, or for a write:
    1. Is this request for a salted resource (e.g., likes on a hot post)?
      • If yes and it is a write: choose one of the N salted keys (e.g., random or round-robin) and forward the write to the shard mapped by that salted key.
      • If yes and it is a read for the total: read all N salted keys and aggregate their values.
    2. Is this request for a directory-mapped celebrity user (profile, posts, etc.)?
      • If yes, route directly to the celebrity’s dedicated shard.
    3. Otherwise, perform the normal hash-and-modulo mapping and route to the computed shard.
A dark-themed infographic titled "How routing decides" showing a flow from a user through an app and cache into a routing layer that salts keys and hashes user IDs to map requests to multiple database shards (Shard 0–4). Colored arrows and labeled boxes illustrate salted key variants (e.g., 500-1, 500-7) and which shard each maps to.
Resharding and cluster growth As your user base grows, every shard will eventually approach capacity. The naive approach is to add shards and change the hash rule (for example, switch from hash % 4 to hash % 5). But changing the modulo remaps nearly every user to a new shard, forcing massive data movement. For example: Chris’s hash is 4,821,371. hash % 4 = 3 (shard 3). hash % 5 = 1 (shard 1). Switching the rule moves Chris’s data even though Chris’s account didn’t change.
A stylized diagram titled "More Shards" showing users and an app sending a request to a routing layer that computes "Chris's hash 4,821,371 ÷ 5 → 1." Arrows from the routing layer point to five database shards (Shard 0–4), with Shard 1 highlighted as storing Chris's data.
This problem motivates more advanced re-sharding strategies such as:
  • Consistent hashing (reduces movement when nodes are added/removed)
  • Virtual nodes / token rings (balance load while minimizing remapping)
  • Range-based re-sharding with online data migration tooling
Links and references Summary
  • The routing layer maps requests to shards and can live in the app, in a proxy, or inside the database — each option trades off latency, operational complexity, and flexibility.
  • Uniform hashing does not prevent hotspots caused by extremely popular keys (the celebrity problem).
  • Common mitigations: dedicate a shard for the celebrity, cache aggressively, and use key salting to spread write load.
  • Adding shards without massive data movement requires techniques like consistent hashing or carefully planned re-sharding processes.

Watch Video