Beyond Basic Scaling
When systems grow beyond millions of users, simple horizontal and vertical scaling isn't enough. You need sophisticated patterns for data distribution, consistency management, and failure handling. This lesson covers the advanced scaling techniques used by companies like Google, Amazon, and Facebook to handle billions of requests daily.
Key Principle: At scale, everything fails. Design for failure, partition tolerance, and graceful degradation from day one.
Distributed System Calculator
Availability: 99.99%
Write Amplification: 3x
Network: 3 Mbps
CAP Theorem & Trade-offs
Consistency
All nodes see the same data simultaneously
• Sequential consistency
• Causal consistency
• Read-your-writes
Availability
System remains operational at all times
• No single point of failure
• Graceful degradation
• Fast failover
Partition Tolerance
System continues despite network failures
• Message loss
• Node isolation
• Async replication
CP Systems (Consistency + Partition Tolerance)
Sacrifice availability for consistency during network partitions.
- • MongoDB
- • Redis (with persistence)
- • HBase
- • Zookeeper
- • Financial transactions
- • Inventory management
- • Configuration management
- • Leader election
AP Systems (Availability + Partition Tolerance)
Sacrifice consistency for availability during network partitions.
- • Cassandra
- • DynamoDB
- • CouchDB
- • Riak
- • Social media feeds
- • Shopping carts
- • User sessions
- • Content delivery
Database Sharding Strategies
Range-Based Sharding
Data is partitioned based on ranges of the shard key.
Shard 1: user_id 0-999,999
Shard 2: user_id 1,000,000-1,999,999
Shard 3: user_id 2,000,000-2,999,999
- • Simple implementation
- • Efficient range queries
- • Easy to understand
- • Uneven distribution
- • Hot spots possible
- • Manual rebalancing
Hash-Based Sharding
Use a hash function to determine shard placement.
shard_id = hash(user_id) % num_shards
// user_123 → hash(123) % 4 = shard_2
// user_456 → hash(456) % 4 = shard_0
- • Even distribution
- • No hot spots
- • Automatic balancing
- • No range queries
- • Resharding is complex
- • Related data scattered
Geographic Sharding
Data is partitioned based on geographic location.
US-East: users.country IN ('US') AND state IN ('NY','MA'...)
US-West: users.country IN ('US') AND state IN ('CA','OR'...)
Europe: users.country IN ('UK','DE','FR'...)
- • Low latency for users
- • Data sovereignty
- • Regional compliance
- • Cross-region queries slow
- • Uneven growth
- • Complex failover
Replication Strategies
Master-Slave Replication
One master handles writes, slaves handle reads.
├── Slave 2 (Read)
└── Slave 3 (Read)
- • Simple to implement
- • Read scaling only
- • Single point of failure
- • Replication lag issues
Master-Master Replication
Multiple masters can handle writes with conflict resolution.
↑ ↑
↓ ↓
Master 3 ←→ Master 4
- • Write scaling
- • No single point of failure
- • Conflict resolution needed
- • Complex consistency
Quorum-Based Replication
Read/write from majority of nodes for consistency.
W = 3, R = 2, N = 5
Write to 3 nodes, Read from 2
Guarantees overlap
- • Tunable consistency
- • Handles node failures
- • Higher latency
- • Used in Cassandra, DynamoDB
Consistency Models Explained
Strong Consistency
All reads return the most recent write. Operations appear instantaneous and atomic.
Write(balance=1000) → Read() = 1000 (immediately, from any node)
Bounded Staleness
Reads may be stale but staleness is bounded by time or version.
Write(price=100) → Read() = 99 (old value for up to 5 seconds)
Session Consistency
Within a session, reads reflect previous writes from that session.
Session1: Write(item=shoe) → Read() = [shoe] ✓
Session2: Read() = [] (may not see shoe yet)
Eventual Consistency
System will become consistent over time, assuming no new updates.
Write(likes++) → Read() = various values → Eventually all nodes = same
Consensus Algorithms
Raft
Understandable consensus algorithm with leader election.
2. Log replication
3. Safety guarantees
4. Membership changes
Used in: etcd, Consul, CockroachDB
Paxos
Original consensus algorithm, complex but proven.
2. Promise phase
3. Accept phase
4. Accepted notification
Used in: Google Chubby, Apache Zookeeper
Byzantine Fault Tolerance
Handles malicious nodes, not just failures.
• Tolerates f failures
• Message authentication
• View changes
Used in: Blockchain, PBFT systems
Gossip Protocol
Eventually consistent information dissemination.
• Anti-entropy
• Rumor mongering
• Convergent
Used in: Cassandra, Riak, Consul
How Tech Giants Scale
Google Spanner
Globally distributed database with strong consistency using atomic clocks.
- • Millions of machines
- • Hundreds of datacenters
- • Trillions of rows
- • Petabytes of data
- • TrueTime API for global ordering
- • Paxos for consensus
- • 2PL for transactions
- • Automatic resharding
Facebook TAO
Graph-based data store for social graph with billions of reads/sec.
- • 1+ billion users
- • Trillions of edges
- • Billions of queries/sec
- • Sub-millisecond latency
- • Write-through cache
- • Geo-distributed caching
- • Eventual consistency
- • Asynchronous replication
Amazon DynamoDB
Fully managed NoSQL with consistent single-digit millisecond latency.
- • 10 trillion requests/day
- • Peaks of 20M requests/sec
- • Petabyte-scale tables
- • 99.999% availability
- • Consistent hashing
- • Quorum-based replication
- • Adaptive capacity
- • Global secondary indexes
Advanced Scaling Pitfalls
Distributed Monolith
Breaking monolith into services that still require synchronous communication. You get all the complexity of microservices with none of the benefits.
Ignoring Network Partitions
Assuming network is reliable. Network failures are inevitable - design for split-brain scenarios and have clear partition handling strategies.
Over-Engineering Consistency
Using strong consistency everywhere when eventual consistency would suffice. Not all data needs ACID guarantees - identify where you can relax consistency.
Cascading Failures
No circuit breakers or bulkheads. One service failure brings down entire system. Implement timeouts, retries with backoff, and circuit breakers.
Advanced Scaling Best Practices
✅ Do
- • Design for failure from day one
- • Use async communication when possible
- • Implement proper observability
- • Test chaos engineering scenarios
- • Plan for data growth (10x, 100x)
- • Use caching at multiple layers
- • Implement backpressure mechanisms
❌ Don't
- • Ignore the CAP theorem trade-offs
- • Use distributed transactions carelessly
- • Forget about network latency
- • Overlook data consistency requirements
- • Skip capacity planning
- • Ignore monitoring and alerting
- • Assume infinite resources