Design focuses on sharding strategies and architecture for relational or NoSQL databases. Out of scope: detailed application logic, specific database vendor features, and network infrastructure.
Functional Requirements
FR1: Distribute data across multiple database instances to improve scalability
FR2: Support horizontal scaling to handle increasing data volume and traffic
Client
|
v
Shard Router / Proxy Layer
|
+-----------------------------+
| |
Shard 1 Shard 2 ... Shard N
(Database Instance) (Database Instance)
Metadata Service tracks shard keys and locations
Backup & Monitoring systems connected to each shard
Components
Shard Router / Proxy Layer
Custom routing service or middleware
Routes client queries to the correct shard based on shard key
Database Shards
Relational DB (e.g., PostgreSQL) or NoSQL DB (e.g., MongoDB)
Store partitioned data subsets independently
Metadata Service
Distributed key-value store or config service (e.g., ZooKeeper, etcd)
Maintain shard key ranges and mapping info
Backup and Monitoring
Backup tools and monitoring dashboards
Ensure data durability and system health
Request Flow
1. Client sends request with data or query including shard key
2. Shard Router extracts shard key and consults Metadata Service
3. Router forwards request to the appropriate shard database instance
4. Shard processes query and returns result to Router
5. Router sends response back to client
6. Metadata Service updates if shards are added or re-sharded
Database Schema
Entities are partitioned by shard key. Each shard holds a subset of data with the same schema. Relationships that span shards require application-level joins or denormalization. Metadata Service stores shard key ranges or hash mappings.
Scaling Discussion
Bottlenecks
Shard Router becomes a single point of failure or bottleneck
Uneven data distribution causing hotspot shards
Cross-shard queries causing high latency
Complexity in re-sharding and data migration
Metadata Service availability impacting routing
Solutions
Use multiple router instances with load balancing and failover
Choose shard keys carefully or use consistent hashing to balance load
Design queries to minimize cross-shard operations or use caching
Implement online re-sharding with minimal downtime and data sync
Deploy Metadata Service in a highly available cluster with consensus
Interview Tips
Time: Spend 10 minutes clarifying requirements and constraints, 20 minutes designing the sharding strategy and architecture, 10 minutes discussing scaling and trade-offs, and 5 minutes summarizing.
Explain importance of shard key selection and its impact on load balancing
Discuss trade-offs between range-based and hash-based sharding
Highlight how routing and metadata services work together
Address how to handle cross-shard queries and consistency
Show awareness of operational challenges like re-sharding and monitoring
Practice
(1/5)
1. Which of the following best describes database sharding?
easy
A. Backing up the entire database to multiple locations for safety.
B. Splitting a large database into smaller, faster, more manageable parts called shards.
C. Encrypting the database to protect sensitive data.
D. Merging multiple databases into one large database.
Solution
Step 1: Understand the concept of sharding
Sharding means dividing a database into smaller parts to improve speed and manageability.
Step 2: Compare options with the definition
Only Splitting a large database into smaller, faster, more manageable parts called shards. describes splitting into smaller parts called shards, which matches the definition.
Final Answer:
Splitting a large database into smaller, faster, more manageable parts called shards. -> Option B
Quick Check:
Sharding = splitting database [OK]
Hint: Sharding means splitting data into parts [OK]
Common Mistakes:
Confusing sharding with backup or encryption
Thinking sharding merges databases
Assuming sharding is only about security
2. Which of the following is a valid shard key choice for hash-based sharding?
easy
A. User ID hashed to assign shard
B. Timestamp of record creation
C. Database server IP address
D. Number of active connections
Solution
Step 1: Identify shard key for hash sharding
Hash sharding uses a hash function on a key like User ID to distribute data evenly.
Step 2: Evaluate options
User ID is a stable, unique key suitable for hashing. Timestamp or server IP are not good shard keys for hashing.
Final Answer:
User ID hashed to assign shard -> Option A
Quick Check:
Hash shard key = User ID [OK]
Hint: Hash shard keys are stable unique IDs [OK]
Common Mistakes:
Using timestamps which cause uneven shard distribution
Choosing server IP which is unrelated to data
Picking dynamic values like active connections
3. Given a range-based sharding on user age groups: shard 1 for ages 0-20, shard 2 for 21-40, shard 3 for 41-60. Which shard will a user aged 35 be stored in?
medium
A. Shard 3
B. Shard 1
C. Shard 2
D. None of the above
Solution
Step 1: Understand the age ranges for each shard
Shard 1: 0-20, Shard 2: 21-40, Shard 3: 41-60.
Step 2: Determine which range 35 falls into
Age 35 is between 21 and 40, so it belongs to Shard 2.
Final Answer:
Shard 2 -> Option C
Quick Check:
35 in 21-40 = Shard 2 [OK]
Hint: Check which range the value fits [OK]
Common Mistakes:
Choosing shard 1 by mistake for age 35
Confusing shard 3 range
Selecting 'None of the above' incorrectly
4. A system uses hash-based sharding on user email addresses. Recently, many users with emails starting with 'a' are causing one shard to overload. What is the likely problem?
medium
A. The hash function is not distributing keys evenly.
B. Emails starting with 'a' are stored in a separate shard by design.
C. Range sharding was used instead of hash sharding.
D. The shard key is not unique.
Solution
Step 1: Analyze the shard overload cause
Hash-based sharding should distribute keys evenly. Overload on one shard means uneven distribution.
Step 2: Evaluate options
The hash function is not distributing keys evenly. explains uneven hash distribution. Emails starting with 'a' are stored in a separate shard by design. is incorrect because hash sharding doesn't group by prefix. Range sharding was used instead of hash sharding. is wrong as range sharding is not used. The shard key is not unique. is unrelated to overload.
Final Answer:
The hash function is not distributing keys evenly. -> Option A
Quick Check:
Uneven hash distribution = overload [OK]
Hint: Uneven load means bad hash distribution [OK]
Common Mistakes:
Assuming range sharding was used
Thinking shard key uniqueness causes overload
Believing emails with same prefix go to same shard by hash
5. You need to shard a global user database to minimize latency and balance load. Which strategy is best to combine geographic location and user ID for sharding?
hard
A. Use range sharding on user ID across all regions.
B. Use only hash sharding on user ID ignoring location.
C. Use directory sharding mapping user ID to random shards.
D. Use geographic sharding first, then hash shard within each region by user ID.
Solution
Step 1: Understand the goal
Minimize latency by serving users from nearby data centers and balance load within each region.
Step 2: Evaluate sharding strategies
Geographic sharding groups users by location to reduce latency. Hash sharding inside each region balances load evenly.
Step 3: Compare options
Use geographic sharding first, then hash shard within each region by user ID. combines geographic and hash sharding effectively. Use only hash sharding on user ID ignoring location. ignores location, increasing latency. Use range sharding on user ID across all regions. uses range sharding which may cause hotspots. Use directory sharding mapping user ID to random shards. uses random mapping, ignoring location and load balance.
Final Answer:
Use geographic sharding first, then hash shard within each region by user ID. -> Option D