Skip to main content
PreparationZone Logo
PrepZone

Replication and Sharding

Master-replica reads, partition keys and the trade-offs of splitting data across nodes.

Pass checkpoint below

Replication basics

RDS primary + read replicas

writeasyncasyncreadCOMPUTE
EKS APIread/write split
DATABASE
RDS primaryus-east-1a
DATABASE
Read replica 1dashboards
DATABASE
Read replica 2EU analytics
Writes hit primary; read-heavy queries route to replicas with bounded staleness.

Replication goals

  • Read scaling — route SELECT queries to replicas.
  • High availability — promote replica if primary fails.
  • Geographic proximity — read replica in EU for EU users.
  • Backup / DR — delayed replica for point-in-time recovery.

Leader-follower (primary-replica) is the most common pattern: all writes go to the primary; replicas apply the write-ahead log asynchronously or semi-synchronously.

Java
-- App routing (conceptual)
-- writes → primary.db.streamhub.internal
INSERT INTO videos (id, creator_id, title) VALUES ($1, $2, $3);

-- reads → replica.db.streamhub.internal (accept replica lag)
SELECT id, title, view_count FROM videos WHERE creator_id = $2 ORDER BY created_at DESC LIMIT 20;

Replication lag and consistency

Replicas lag the primary by milliseconds to seconds. Implications for StreamHub:

AspectRead from replicaRead from primary
LatencyLower load on primaryHigher primary load
FreshnessMay miss last few writesAlways latest
StreamHub usePublic feeds, browse pagesPost-upload confirmation, billing
  • Latency

    Read from replicaLower load on primary
    Read from primaryHigher primary load
  • Freshness

    Read from replicaMay miss last few writes
    Read from primaryAlways latest
  • StreamHub use

    Read from replicaPublic feeds, browse pages
    Read from primaryPost-upload confirmation, billing

After a creator uploads a video, StreamHub reads from primary for the "your video is live" page; followers see it on replicas within ~1 second.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 6 sections

  • Failover
  • When to shard
  • Sharding strategies
  • Cross-shard challenges
  • Replication + sharding together
  • Migration path

Test yourself

Answer these before moving on — recall is what makes it stick.