← Back to Index

6. Scalability

Horizontal vs Vertical vs Diagonal Scaling

ApproachWhat It MeansProsCons
Vertical (Scale Up)Add more power to existing machines (CPU, RAM, disk)Simple, no code changesHardware limits, expensive at high end, single point of failure
Horizontal (Scale Out)Add more machines to the poolTheoretically infinite, cost-effective, fault-tolerantRequires load balancers, distributed coordination, code changes
DiagonalVertical within a tier, horizontal across tiersBalanced approachComplexity of both
# Horizontal scaling pattern
[Users] → [Load Balancer] → [App Server 1] → [Shared DB]
                             [App Server 2]
                             [App Server N]
# Stateless apps scale easily; stateful (DB) requires sharding/replication

Load Balancers in Depth

Beyond basic distribution, modern load balancers provide:

Auto-Scaling

Auto-scaling automatically adjusts the number of compute resources based on demand. Metrics-based (CPU > 70% → add instance) or schedule-based (scale up at 8 AM, scale down at 10 PM).

# Auto-scaling configuration (conceptual)
AutoScalingGroup:
  min: 2
  max: 20
  target_cpu: 60%
  scale_up:
    cooldown: 120s
    adjustment: +2 instances
  scale_down:
    cooldown: 300s
    adjustment: -1 instance

Sharding (Horizontal Partitioning)

Sharding splits a large database into smaller, independent databases (shards). Each shard holds a subset of the data, typically determined by a shard key.

# Shard key = user_id % 4
Shard 0: user_id % 4 == 0
Shard 1: user_id % 4 == 1
Shard 2: user_id % 4 == 2
Shard 3: user_id % 4 == 3

Challenges: Choosing a good shard key, resharding (adding more shards), cross-shard queries, distributed transactions.

CAP Theorem

A distributed system can only guarantee two of three properties simultaneously:

System design pillars showing trade-offs between consistency, availability, and partition tolerance

In practice, network partitions will happen, so you choose between CP and AP. CA is not possible in distributed systems.

Consistency Models

Exercise: Design a sharding strategy for a social media platform where users can follow others and see a timeline. Describe your shard key choice, how you handle cross-shard queries (e.g., fetching a timeline from followed users), and how you would reshard from 4 to 8 shards with zero downtime.