Back to Hub
System Design (HLD)

Database Sharding, CAP Theorem & Message Queues

#sharding#cap-theorem#message-queue#kafka#database-scaling

Database Sharding, CAP Theorem & Message Queues

Database Scaling Strategies

Vertical Scaling (Scale Up)

  • Bigger machine: more CPU, RAM, faster SSD
  • Limit: Single machine has a ceiling
  • Use when: Simple, not yet at scale

Horizontal Scaling (Scale Out)

  • More machines sharing the load
  • Requires replication and/or sharding

Read Replicas

  • Use when: Read-heavy workload (90%+ reads)
  • Trade-off: Replication lag → eventual consistency for reads

Database Sharding

Sharding partitions data horizontally across multiple databases. Each shard holds a subset of the data.

Sharding Strategies

StrategyDescriptionProsCons
Range-basedShard by value ranges (e.g., user ID 1–1M, 1M–2M)Simple, range queries easyHot spots if distribution uneven
Hash-basedHash(key) mod N → shard numberEven distributionRange queries require scatter-gather
Directory-basedLookup table maps keys → shardsFlexibleLookup table = single point of failure
GeographicShard by region/locationData locality, complianceCross-region queries are complex

Consistent Hashing

Minimizes data redistribution when servers are added/removed.

Hash ring: 0 ─────────────────── 2^32
           │                      │
   Server A(100)  Server B(500)  Server C(800)
           │         │             │
   Keys 0-100    101-500       501-800

Adding Server D(300):
  Only keys 101-300 move from B to D
  (instead of rehashing everything)

Sharding Challenges

ChallengeDescriptionMitigation
Cross-shard joinsData on different shards can't be joined easilyDenormalize, application-level joins
Distributed transactionsACID across shards is hardSaga pattern, eventual consistency
RebalancingAdding/removing shards requires data migrationConsistent hashing, virtual shards
Hot shardsUneven data/traffic distributionBetter shard key, splitting hot shards
Operational complexityEach shard needs backup, monitoring, etc.Managed databases (Aurora, Spanner)

CAP Theorem

In a distributed system, you can guarantee at most two of three properties:

PropertyMeaning
Consistency (C)Every read receives the most recent write
Availability (A)Every request receives a response (even if stale)
Partition Tolerance (P)System works despite network partitions between nodes

The Real Choice: CP or AP

Since network partitions will happen in any distributed system, you really choose between C and A during a partition:

SystemChoiceBehavior During Partition
CP (Consistency + Partition Tolerance)Refuse requests if can't guarantee consistencyBank transfers, inventory
AP (Availability + Partition Tolerance)Serve requests with potentially stale dataSocial media feeds, DNS

PACELC Extension

During PartitionElse (no partition)
Choose C or AChoose L(atency) or C(onsistency)

Example: DynamoDB is PA/EL — Available during partitions, Low latency normally.


Message Queues

Decouple producers from consumers, enabling asynchronous processing, buffering, and fault tolerance.

Core Concepts

Queue vs Topic (Pub/Sub)

ModelDeliveryUse Case
QueueOne consumer processes each messageTask distribution, work queues
Topic (Pub/Sub)All subscribers receive every messageEvent broadcasting, notifications

Message Queue Technologies

TechnologyModelOrderingThroughputUse Case
RabbitMQQueue + Pub/SubPer-queue FIFOMediumTask queues, RPC
Apache KafkaLog-based topicsPer-partitionVery highEvent streaming, logs
Amazon SQSQueueBest-effort (FIFO available)HighServerless, decoupling
Redis StreamsLog-basedPer-streamHighReal-time, lightweight

Kafka Architecture

Topic: "order-events"
├── Partition 0: [msg1, msg4, msg7, ...] → Consumer A
├── Partition 1: [msg2, msg5, msg8, ...] → Consumer B
└── Partition 2: [msg3, msg6, msg9, ...] → Consumer C

Key concepts:
- Messages are immutable, append-only
- Retention-based (not deletion on consume)
- Consumer groups for parallel processing
- Offset tracking per consumer group

Message Delivery Guarantees

GuaranteeDescriptionImplementation
At-most-onceFire and forget; may lose messagesNo acks
At-least-onceRetry until acknowledged; may duplicateAck + retry
Exactly-onceEach message processed exactly onceIdempotency + transactions

Common Patterns

PatternDescription
Work QueueDistribute tasks across workers
Event SourcingStore events as source of truth, rebuild state
CQRSSeparate read/write models, sync via events
SagaDistributed transactions via compensating events
Dead Letter QueueFailed messages go to a separate queue for debugging

System Design Checklist

When designing any distributed system, consider:

  1. Requirements — Functional & non-functional (latency, throughput, consistency)
  2. Estimation — QPS, storage, bandwidth
  3. API Design — RESTful endpoints, gRPC for internal
  4. Data Model — SQL vs NoSQL, schema design
  5. High-Level Design — Components and data flow
  6. Scaling — Caching, sharding, replication
  7. Monitoring — Metrics, alerting, logging
  8. Failure Modes — What happens when X fails?