System Design Interview

Database Sharding

Learn how to partition your database for massive scale. Explore different sharding keys, the challenges of rebalancing, and how to handle cross-shard transactions.

Example: Hash-Based Sharding Logic in Python

A simple function to determine which shard a given key belongs to using a hash-based approach. This is a common way to distribute data evenly.

class HashSharding: def __init__(self, num_shards): self.num_shards = num_shards def get_shard(self, key): # Use a simple hash function (in a real system, a more robust hash like MD5 or SHA-1 would be used) hashed_key = hash(key) shard_id = hashed_key % self.num_shards return shard_id
Our AI Coach can help you explore the pros and cons of different sharding keys and discuss complex topics like consistent hashing to minimize data movement when re-sharding.

Related System Design Guides

Master more system design concepts with AI-powered preparation

System Design Caching Interview Questions
AI-powered interview preparation guide
System Design Basics For Coding Interviews
AI-powered interview preparation guide
System Design Databases Interview Questions
AI-powered interview preparation guide
System Design Load Balancing Interview Questions
AI-powered interview preparation guide