Sharding Strategies
1 min read
Partition data across multiple database instances to distribute write load.
Partition data across database instances. Name the shard key, the strategy (hash/range/directory), and what happens when you reshard.
How It Works
Sharding splits data across multiple database instances. Hash-based sharding (hash(key) % N) distributes evenly but makes range queries impossible. Range-based sharding supports range queries but can create hotspots. Directory-based sharding is most flexible but introduces a single point of failure (see SPOF Detection). Consistent hashing minimizes data movement when adding/removing shards. In interviews, name the shard key, the strategy, and what happens when you reshard.
Real-World Example
Discord shards by guild (server) ID using consistent hashing. Each shard handles all messages for a set of guilds. Cross-guild operations (user profile, friend list) use a separate user-sharded database.
Test Yourself
Scenario: A messaging app has 500M users and its Postgres primary is at 80% write capacity. You have to shard. Pick a shard key and defend it against two obvious-but-wrong alternatives.
Get notified when we launch
One email when the full practice product is live. No spam.