Database sharding is a technique to split a large database into smaller, faster, and more manageable parts called shards. Each shard holds a portion of the data, helping improve performance and scalability.
Click to reveal answer
beginner
Explain horizontal sharding.
Horizontal sharding divides data by rows. Each shard contains a subset of rows from the same table, often based on a key like user ID. This spreads the load across multiple servers.
Click to reveal answer
intermediate
What is vertical sharding and when is it used?
Vertical sharding splits data by columns or tables. Different shards hold different parts of the schema. It is useful when some tables or columns are accessed more frequently or require different resources.
Click to reveal answer
intermediate
Describe directory-based sharding.
Directory-based sharding uses a lookup service to map each data item to its shard. This allows flexible shard assignment but adds a dependency on the directory for every query.
Click to reveal answer
advanced
What are common challenges of database sharding?
Challenges include data rebalancing when shards grow unevenly, handling cross-shard queries, maintaining consistency, and increased complexity in application logic.
Click to reveal answer
Which sharding strategy splits data by rows across multiple shards?
ADirectory-based sharding
BVertical sharding
CHorizontal sharding
DFunctional sharding
✗ Incorrect
Horizontal sharding divides data by rows, distributing subsets of rows across shards.
What is a key disadvantage of directory-based sharding?
AIt duplicates data across shards
BIt cannot handle large datasets
CIt only works with vertical sharding
DIt requires a lookup service for every query
✗ Incorrect
Directory-based sharding depends on a lookup service to find the shard, adding overhead and a potential single point of failure.
Vertical sharding splits data based on:
AColumns or tables
BUser IDs
CRows
DRandom distribution
✗ Incorrect
Vertical sharding divides data by columns or tables, separating parts of the schema into different shards.
Which problem is NOT typically caused by sharding?
ACross-shard query complexity
BAutomatic data backup
CIncreased application complexity
DData rebalancing
✗ Incorrect
Automatic data backup is not a direct problem caused by sharding; it is usually handled separately.
What is the main goal of database sharding?
ATo improve performance and scalability by splitting data
BTo reduce database size by deleting data
CTo encrypt data for security
DTo merge multiple databases into one
✗ Incorrect
Sharding splits data into smaller parts to improve performance and allow the system to scale.
Describe the main types of database sharding strategies and when to use each.
Think about how data is divided: by rows, columns, or using a directory.
You got /5 concepts.
What challenges arise from implementing database sharding and how can they affect system design?
Consider operational and development difficulties after splitting data.
You got /4 concepts.
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