


user_id, since every user has a unique ID. A simple and fast sharding rule uses modulus division:
- Alan’s ID = 10 →
10 % 4 = 2→ Alan stored on shard 2 - Another user ID = 13 →
13 % 4 = 1→ that user stored on shard 1

- Hot shards
By unlucky distribution, many high-traffic users (e.g., celebrities) could be mapped to the same shard. If millions of users view those celebrity profiles, that shard becomes overloaded while others remain underutilized. Mitigations include choosing a shard key or hashing strategy that evens out access patterns, adding capacity, or using virtual nodes to spread load across physical machines.

- Resharding (rebalancing) pain with naive modulus sharding
With modulus-based sharding, the mapping depends on the total number of shards. Changing the shard count (for example from 4 to 5) will change the remainder for almost everyuser_id. Alan moves from shard 2 to shard 0 (10 % 4 = 2→10 % 5 = 0), which implies massive data movement across nodes — costly and disruptive.

Consistent hashing maps both data keys and shard nodes onto a circular hash space (a hash ring). Each key is assigned to the first node encountered clockwise from the key’s hash. When a new shard joins, it only takes responsibility for a small range of keys (typically between its predecessor and itself), so most keys remain on their original shard. Using virtual nodes (multiple positions per physical node) smooths distribution and helps avoid hot spots.

Practical reminders
- Choose the shard key to align with your most frequent access patterns: storage balance, query efficiency, and joinability are all impacted.
- Sharding complicates cross-shard joins and multi-shard transactions. These require application-level coordination, two-phase commits, or compensating logic.
- Avoid premature sharding. Introduce sharding when a single machine can no longer handle your storage or throughput needs — sharding is difficult to undo.
Choose a shard key that matches your dominant access patterns. A poor shard key can create hot shards, hurt performance, and make queries or transactions inefficient.
Sharding adds operational complexity: monitor load distribution, plan for rebalancing, and test how your application handles partial failures and cross-shard operations.