Sitemap

Distributed Databases: Scaling Data to Billions of Users 💾⚡

7 min read4 days ago

--

Press enter or click to view image in full size

Imagine launching an app. On Day 1, you have 1,000 users. A single server handles everything smoothly.

By Day 100, a viral moment brings 100,000,000 users.

Suddenly, your database hits 100% CPU usage, memory maxes out, disk I/O throttles, and every query freezes. Your app crashes, users leave angry, and revenue plunges to zero.

How do internet giants like Netflix, Uber, and Amazon serve hundreds of millions of concurrent requests without missing a beat? They don’t buy giant supercomputers — they use Distributed Databases.

1. Scale-Up vs. Scale-Out: The Fundamental Choice

When a single database server hits its hardware limits, system architects face two paths:

Press enter or click to view image in full size
Press enter or click to view image in full size

The Distributed Rule: Distributed databases exist to make Horizontal Scaling look and feel like a single database to the application.

2. Superpower #1: Replication (Keeping Copies Alive)

Replication means placing identical copies of the same data across multiple physical servers.

Press enter or click to view image in full size

Why Replicate?

  • High Availability: If Node A hardware fails, Nodes B and C keep serving traffic instantly.
  • Read Throughput: Spread 100,000 read requests across 10 replicas instead of hammering 1 machine.
  • Geographic Proximity: Put user data in Virginia, Frankfurt, and Tokyo so local users get sub-10ms response times.

Three Ways Replicas Talk to Each Other

1. Leader-Follower (Primary-Secondary)

All writes land on the Leader, which streams change logs to the Followers. Reads can be served by any node.

  • Pros: Easy to manage, avoids write conflicts.
  • Cons: The Leader remains a single write bottleneck.

2. Multi-Leader (Active-Active)

Multiple nodes act as Leaders and accept writes simultaneously.

Press enter or click to view image in full size
  • Pros: Excellent for multi-region setups (e.g., US-East and EU-West both accept local writes).
  • Cons: Complex conflict resolution when two leaders update the same row at the same time.

3. Leaderless (Dynamo-Style)

Any client can read or write to any node in the cluster.

  • Pros: Highly available; write speed stays fast even if several nodes go offline.
  • Cons: Requires client-side consensus checks and background data repairing.

Sync vs. Async: The Speed vs. Safety Spectrum

Press enter or click to view image in full size
Sync vs. Async
Press enter or click to view image in full size
Sync vs. Async vs. Semi-Sync

3. Superpower #2: Sharding (Slicing the Big Dataset)

While replication creates copies of data, Sharding (horizontal partitioning) splits a massive dataset into smaller, non-overlapping slices called shards.

Press enter or click to view image in full size

Partitioning Strategies

📌 Range-Based Sharding

Assigns contiguous blocks of keys to specific nodes (e.g., Names A-G → Shard 1, H-P → Shard 2).

  • Pro: Range queries (WHERE age BETWEEN 20 AND 30) read efficiently from a single shard.
  • Con: Creates Hotspots. For example, auto-incrementing primary keys or timestamped logs will hammer the newest shard while older shards sit idle.

📌 Hash-Based Sharding & Consistent Hashing

Applies a hash function to the key to determine its shard:

Get Ahmed Abdulwahid’s stories in your inbox

Join Medium for free to get updates from this writer.

Shard ID=hash(Shard Key) (modN)

  • Pro: Uniformly balances data distribution across all physical nodes.
  • Con: Simple modulo arithmetic breaks when you add or remove nodes. Modern systems use Consistent Hashing Ringsso adding a node only moves O(K/N) keys instead of re-sharding the whole cluster.
Press enter or click to view image in full size

The Heavy Trade-offs of Sharding

  • Hot Partitioning: If 80% of your users live in the US, choosing country_id as a shard key sends almost all traffic to one machine.
  • Cross-Shard Joins: Querying data spread across multiple shards requires sending data over the physical network, turning millisecond operations into multi-second delays.
  • Distributed Transactions: Running ACID updates across multiple shards requires heavy protocols like Two-Phase Commit (2PC), which slow down throughput.

4. Quorums: How Distributed Replicas Reach Agreement

In a distributed, leaderless cluster (or when reading from asynchronous replicas), how do you guarantee that you aren’t reading stale data?

You use Quorum Consensus.

Press enter or click to view image in full size

The Quorum Variables

  • N = Replication Factor (Total copies of the data)
  • W = Write Quorum (Minimum node approvals needed to mark a write successful)
  • R = Read Quorum (Minimum node responses needed to complete a read)

The Strong Consistency Equation

R + W > N

If R+W>N, the set of nodes accepting writes and the set of nodes serving reads must overlap by at least one node. That overlapping node holds the newest data, guaranteeing Strong Consistency.

Real-World Quorum Configurations (N=3)

Press enter or click to view image in full size

5. The Fast Lane: Caching ⚡

If Replication and Sharding are about storing petabytes of data across 100 servers safely, Caching is about avoiding the database altogether whenever possible.

Querying a distributed database requires fetching data from disk or executing complex cross-node joins, taking anywhere from 10 to 50 ms. Reading from an in-memory cache like Redis or Memcached takes microseconds — roughly 1,000 times faster.

Press enter or click to view image in full size

In almost every web application, 80% of traffic queries the same 20% of hot data (viral posts, trending products, user sessions). Caching places this hot data directly in RAM, absorbing massive traffic spikes before they ever reach your database storage layer.

How Data Moves in a Cache

Depending on your system’s read/write demands, you choose how the cache interacts with your database:

Press enter or click to view image in full size
  1. Cache-Aside (Lazy Loading): The application checks the cache first. On a hit, it returns the data. On a miss, it reads from the database and updates the cache for next time. Highly resilient — if the cache crashes, traffic falls back to the database.
  2. Write-Through: Every update writes to the cache and the database simultaneously. Data is never stale, but write latency is slightly higher.
  3. Write-Behind (Write-Back): Writes land only in the ultra-fast cache, which confirms success instantly. The cache flushes updates to the database in background batches. Insanely fast for heavy write spikes (e.g., streaming view counters or gaming leaderboards), but carries a risk of data loss if a node crashes before flushing.

The Three Classic Caching Pitfalls

Caching sounds simple until you run into production edge cases:

📌 Cache Avalanche 🌋: Millions of keys expire at the exact same second (e.g., midnight TTLs). The next wave of traffic misses the cache completely and slams the database all at once.

  • The Fix: Add random time variations (TTL Jitter) so keys expire at staggered times.

📌 Cache Stampede / Thundering Herd 𝄠: A single viral key expires. 100,000 concurrent users miss the cache at the exact same millisecond, and all 100,000 try to recalculate the data from the database simultaneously.

  • The Fix: Use Mutex Locks so only one thread recomputes the key while others wait for the cache to refresh.

📌 Cache Penetration 🕳️: An attacker repeatedly queries non-existent IDs (e.g., user_id = -9999). Every request bypasses the cache and hammers your database for missing rows.

  • The Fix: Use Bloom Filters at the gateway to quickly reject non-existent keys before touching storage.

6. Architectural Comparison

Press enter or click to view image in full size

7. Putting It Together: Sharding + Replication

Production systems at scale combine both superpowers into a unified topology:

Press enter or click to view image in full size
  • Sharding scales storage capacity and write throughput horizontally across machines.
  • Replication guarantees high availability within each shard and scales read operations.

8. What This Means for Data Engineering

These distributed principles directly impact daily data engineering workloads:

  • Apache Spark: Spark partitions work like database shards. Picking poor partition keys creates data skew, causing 99% of tasks to finish instantly while 1% takes hours on a single overwhelmed executor node.
  • Apache Kafka: Kafka topics are partitioned across brokers (sharding) and copied across cluster nodes via replication.factor (replication), using min.insync.replicas to enforce write quorums.
  • Delta Lake & Apache Iceberg: Modern data lake formats manage concurrent reads and writes over distributed object stores (like AWS S3) using optimistic concurrency control (OCC), preventing lock contention on cloud storage.

Connect: LinkedIn 😊

--

--