0
0
HLDsystem_design~12 mins

Database sharding strategies in HLD - Architecture Diagram

Choose your learning style9 modes available
System Overview - Database sharding strategies

This system explains how database sharding splits data across multiple databases to improve performance and scalability. It covers key strategies like horizontal sharding by user ID, range-based sharding, and directory-based sharding to distribute data efficiently.

Architecture Diagram
User
  |
  v
Load Balancer
  |
  v
API Gateway
  |
  +-----------------------------+
  |                             |
Shard Router                Directory Service
  |                             |
  +-------------+---------------+
                |
       +--------+--------+
       |                 |
Shard 1             Shard 2
(Database)          (Database)
       |                 |
    Cache 1          Cache 2
Components
User
client
Initiates requests to the system
Load Balancer
load_balancer
Distributes incoming requests evenly to API Gateway instances
API Gateway
api_gateway
Handles client requests and routes them to appropriate services
Shard Router
service
Determines which shard holds the requested data based on sharding strategy
Directory Service
service
Maintains mapping of data keys to shards for directory-based sharding
Shard 1
database
Stores a subset of the data based on shard key
Shard 2
database
Stores another subset of the data based on shard key
Cache 1
cache
Caches frequently accessed data for Shard 1 to reduce database load
Cache 2
cache
Caches frequently accessed data for Shard 2 to reduce database load
Request Flow - 9 Hops
UserLoad Balancer
Load BalancerAPI Gateway
API GatewayShard Router
Shard RouterDirectory Service
Shard RouterCache 1 or Cache 2
Cache 1 or Cache 2Shard 1 or Shard 2
Shard 1 or Shard 2Cache 1 or Cache 2
Cache 1 or Cache 2API Gateway
API GatewayUser
Failure Scenario
Component Fails:Shard 1
Impact:Data stored in Shard 1 becomes unavailable, causing read and write failures for that shard's data. Cache 1 may still serve stale reads if data is cached.
Mitigation:Use database replication for Shard 1 to failover to a replica. Implement retry logic and degrade gracefully by informing users of partial unavailability.
Architecture Quiz - 3 Questions
Test your understanding
Which component decides which shard holds the requested data?
ACache
BLoad Balancer
CShard Router
DUser
Design Principle
This architecture demonstrates horizontal scaling by splitting data into shards to improve performance and availability. It uses a shard router to direct requests, caches to reduce database load, and replication to handle failures, following best practices for scalable distributed databases.