Bird
Raised Fist0
HLDsystem_design~20 mins

Database sharding strategies in HLD - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Database Sharding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Horizontal vs Vertical Sharding

Which statement correctly describes the difference between horizontal and vertical sharding?

AHorizontal sharding splits data by columns across multiple databases; vertical sharding splits data by rows across databases.
BHorizontal sharding splits data by rows across multiple databases; vertical sharding splits data by columns or tables across databases.
CHorizontal sharding duplicates the entire database across servers; vertical sharding partitions data by user location.
DHorizontal sharding stores data in a single large database; vertical sharding stores data in multiple identical copies.
Attempts:
2 left
💡 Hint

Think about whether data is divided by rows or columns.

❓ Architecture
intermediate
2:00remaining
Choosing a Sharding Key for User Data

You are designing a sharded database for a social media app. Which sharding key would best distribute user data evenly across shards?

AUser's unique user ID (numeric or UUID)
BUser's geographic location (country or city)
CUser's last login timestamp
DUser's account creation date
Attempts:
2 left
💡 Hint

Consider which key is unique and evenly distributed.

❓ scaling
advanced
2:30remaining
Handling Hotspots in Sharded Databases

Your sharded database has a hotspot where one shard receives most of the traffic due to popular users. Which approach best mitigates this issue?

AIncrease the hardware resources only on the hotspot shard without changing sharding strategy.
BUse vertical sharding to split user data by columns instead of rows.
CMove all popular users to a single dedicated shard to isolate traffic.
DRe-shard the database using a hash of user ID to distribute load evenly.
Attempts:
2 left
💡 Hint

Think about redistributing data to balance load.

❓ tradeoff
advanced
2:30remaining
Tradeoffs Between Range-Based and Hash-Based Sharding

Which statement best describes a key tradeoff between range-based and hash-based sharding?

ARange-based sharding allows efficient range queries but risks uneven data distribution; hash-based sharding distributes data evenly but makes range queries inefficient.
BRange-based sharding evenly distributes data but requires complex hashing; hash-based sharding is simple but causes hotspots.
CRange-based sharding is only used for vertical sharding; hash-based sharding is only used for horizontal sharding.
DRange-based sharding duplicates data across shards; hash-based sharding stores data in a single shard only.
Attempts:
2 left
💡 Hint

Consider query efficiency versus data balance.

❓ estimation
expert
3:00remaining
Estimating Number of Shards for Growing User Base

Your application currently has 10 million users stored in 5 shards. Each shard can handle up to 3 million users efficiently. You expect user growth to 50 million in 2 years. How many shards should you plan for to maintain performance?

AAt least 10 shards
BAt least 20 shards
CAt least 17 shards
DAt least 15 shards
Attempts:
2 left
💡 Hint

Divide expected users by max users per shard and round up.

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

  1. Step 1: Understand the concept of sharding

    Sharding means dividing a database into smaller parts to improve speed and manageability.
  2. 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.
  3. Final Answer:

    Splitting a large database into smaller, faster, more manageable parts called shards. -> Option B
  4. 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

  1. Step 1: Identify shard key for hash sharding

    Hash sharding uses a hash function on a key like User ID to distribute data evenly.
  2. 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.
  3. Final Answer:

    User ID hashed to assign shard -> Option A
  4. 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

  1. Step 1: Understand the age ranges for each shard

    Shard 1: 0-20, Shard 2: 21-40, Shard 3: 41-60.
  2. Step 2: Determine which range 35 falls into

    Age 35 is between 21 and 40, so it belongs to Shard 2.
  3. Final Answer:

    Shard 2 -> Option C
  4. 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

  1. Step 1: Analyze the shard overload cause

    Hash-based sharding should distribute keys evenly. Overload on one shard means uneven distribution.
  2. 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.
  3. Final Answer:

    The hash function is not distributing keys evenly. -> Option A
  4. 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

  1. Step 1: Understand the goal

    Minimize latency by serving users from nearby data centers and balance load within each region.
  2. Step 2: Evaluate sharding strategies

    Geographic sharding groups users by location to reduce latency. Hash sharding inside each region balances load evenly.
  3. 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.
  4. Final Answer:

    Use geographic sharding first, then hash shard within each region by user ID. -> Option D
  5. Quick Check:

    Geo + hash sharding = low latency + balanced load [OK]
Hint: Combine geo then hash for best latency and balance [OK]
Common Mistakes:
  • Ignoring geographic location causing high latency
  • Using only hash sharding causing uneven latency
  • Using range sharding causing load hotspots
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard