
- 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.


- 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/
- 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.
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.

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.

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.
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.

Salting increases write parallelism but requires extra reads and aggregation for total counts. Apply salting selectively to keys that are demonstrably hot.
- First, a read request checks the cache. If cached, return immediately — the routing layer is bypassed.
- On a cache miss, or for a write:
- 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.
- Is this request for a directory-mapped celebrity user (profile, posts, etc.)?
- If yes, route directly to the celebrity’s dedicated shard.
- Otherwise, perform the normal hash-and-modulo mapping and route to the computed shard.
- Is this request for a salted resource (e.g., likes on a hot post)?

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.

- 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
- MongoDB mongos router documentation
- Consistent hashing overview (concept)
- Cache patterns and invalidation strategies (overview)
- 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.