stackbook.io
🔍
← Back to Syllabus
Day 8Level 1 — Building Blocks

Replication and Partitioning

Learn: 40 minMap: 25 minDesign: 45 min

Core Concepts (5)

  • 1.Replication copies data for durability and read capacity
  • 2.Partitioning splits data to scale writes and storage
  • 3.Replicas can lag behind the primary
  • 4.The partition key decides balance and query cost
  • 5.Both techniques add operational complexity

Map to Your Experience

If your main database vanished tonight, how much data could you lose and how long would recovery take? Who knows the answer today?

Design Brief: Scale a User Database

Learn

A single database node is both a capacity limit and a risk. Two techniques address these problems, and they solve different things.

Replication keeps copies of the same data on several nodes. In the common primary-replica setup, all writes go to the primary, which streams changes to replicas. Replicas give you durability, because the data exists in more than one place, and read capacity, because queries can be spread over the replicas. If the primary fails, a replica can be promoted, which is called failover.

Replication has a cost. Copying takes time, so a replica can lag behind the primary by milliseconds or, under load, seconds. A user who saves a profile and immediately reloads from a replica may see old data. Applications handle this by reading from the primary right after a write, or by accepting short staleness where it does not matter.

Partitioning, also called sharding, splits the data so that each node holds only part of it. Users with ids in one range live on node A, another range on node B. This scales storage and write throughput, which replication alone cannot, because every replica still holds everything.

The partition key is the most important decision. A good key spreads data and traffic evenly and keeps the common queries on one partition. Partitioning by user id works well when users mostly read their own data. Partitioning by country might produce one enormous partition and many tiny ones, called a hot spot. Hash-based placement spreads evenly but makes range queries across partitions costly. Changing the key later means moving a great deal of data.

The practical advice is to delay partitioning as long as possible. Add replicas first, add caching, and tune queries. Partitioning complicates joins, transactions across partitions, and day-to-day operations. When you do need it, choose the key deliberately and document why.

Map

For a social app, decide whether each of these suits replication, partitioning, or both: surviving a server crash, serving 10 times more reads, storing 50 terabytes of posts. Give one sentence of reasoning for each.

Design

A user database has outgrown one node: reads are 20 times writes and storage is nearing the machine limit. Propose a plan using replicas first and partitioning later. State which reads go to replicas, how you handle read-after-write staleness, the partition key and why, and the steps of a failover. Name one query that becomes harder after partitioning.

Requirements

  • •Introduce replicas and say what reads go where
  • •Choose a partition key
  • •Describe how a failover happens

Constraints

  • •Reads are 20 times writes today
  • •Data must survive loss of a node

Must Cover

replicaspartition-keyfailover

Trade-off Axis

scale vs operational complexity

Competencies Advanced

data state → L1data state → L2

Ready to Design?

Open this day in the Design Studio. The studio follows this track: its days, concepts, patterns and trade-offs are loaded, and you keep one design across the whole track.

Open in Studio →