QUICK START:HTMLCSSJavaScriptTypeScriptPythonSQLReactNext.jsNode.jsDSASystem DesignDevOpsCybersecurityAI / ML
Advanced 22 min readModule: Module 4: Database Scaling: Replication, Partitioning & Sharding

Database Replication, Partitioning & Sharding

Scale databases past single-machine disk and CPU limits using master-slave replication and horizontal sharding.

What You Will Learn in This Lesson

  • Primary-Replica (Master-Slave) replication for read scaling
  • Horizontal Sharding across multiple database instances based on Shard Key
  • Handling cross-shard joins and distributed transactions

Introduction & Core Concept

When a single database server runs out of disk space or CPU capacity to process queries, databases scale out horizontally through Replication and Sharding.
WHY DOES THIS MATTER IN THE REAL WORLD?

Sharding splits a 100TB database across 10 database servers of 10TB each based on user_id hashing.

Shard Key Routing Logic

javascript
javascript
1
2
3
4
5
6
function getShardDatabase(userId) {
const shardCount = 4;
const hash = hashFunction(userId);
const shardIndex = hash % shardCount;
return `db_shard_${shardIndex}`;
}

Line-by-Line Technical Breakdown

1Choosing a poor shard key (like country) causes hotspotting where one shard receives 90% of traffic.

Try It Yourself (Interactive Editor)

Modify the code in real-time and click Run to test live browser output and console logs.

Intelligent Code Runner & Live Sandbox[JAVASCRIPT]
JAVASCRIPT SOURCE EDITOR
Interactive Live Code

Industry Best Practices & Professional Standards

  • Select high-cardinality, evenly distributed shard keys (like UUID user_id).

Lesson Summary & Core Takeaways

  • Replication and sharding scale databases to petabytes of data.