Skills101

System Design · Lesson 4 of 8 · Updated ·

Databases at Scale — Replication, Sharding, SQL vs NoSQL and CAP

The database is usually the hardest part to scale, because it holds state. App servers can be cloned easily; data must stay correct.

1. First: the cheap wins

Before anything complex:

  • Add the right indexes (see Indexing).
  • Fix slow queries; avoid N+1 queries.
  • Add a cache for hot reads.
  • Use connection pooling.

2. Replication: copies of the data

Replication: one primary takes writes and copies data to two read replicas; sharding: users are split across shards by name range. Big systems use both

  • One primary handles writes; replicas copy its data and serve reads.
  • Most apps read far more than they write (often 10:1 or 100:1), so replicas multiply read capacity.
  • If the primary fails, a replica is promoted (failover).

Replication lag: replicas may be a few milliseconds behind. After a user updates their profile, read it from the primary for that request ("read your own writes").

3. Sharding: splitting the data

When one machine can't hold the data or handle the writes, split rows across several databases (shards).

StrategyHowWatch out
Rangeusers A–H, I–P, Q–Zhot ranges (many names start with S)
Hashhash(user_id) % 4even spread; resharding moves data (use consistent hashing)
GeographicIndia users in Mumbai, US users in Virginiausers who move
Directorya lookup table says which shardthe lookup service must scale too

Sharding costs: cross-shard joins and transactions become hard, and rebalancing is work. Shard only when you must.

4. SQL vs NoSQL

SQL (PostgreSQL, MySQL)NoSQL (MongoDB, Cassandra, DynamoDB, Redis)
Data modeltables with a fixed schemadocuments, key-value, wide-column, graph
Relationships / joinsexcellentlimited
Transactions (ACID)strongvaries (often limited)
Scaling writesharder (sharding)built for horizontal scale
Best formoney, orders, users, anything relationalhuge volume, flexible data, simple access patterns (chats, logs, feeds, sessions)

Default to SQL for most apps; add NoSQL for specific needs. Many companies use both.

5. The CAP theorem

CAP triangle: Consistency (every read sees the latest write), Availability (always answer) and Partition tolerance; networks will split, so systems choose CP or AP

In a distributed database, when the network partitions (servers can't talk to each other), you must choose:

  • CP — stay consistent: refuse some requests until the network heals. (Banking, inventory.)
  • AP — stay available: answer with possibly old data and reconcile later. (Likes, view counts, feeds.)

Eventual consistency: all copies become the same eventually (usually within seconds). Fine for a like count; not for your bank balance.

Common mistakes

  • Sharding too early instead of indexing and caching.
  • Reading from a replica right after a write and confusing users.
  • Picking NoSQL because it's "faster" for data that is clearly relational.

Quick recap

  • Index, optimise, cache first.
  • Replication: primary writes, replicas read; watch replication lag.
  • Sharding: range, hash (consistent hashing), geo, directory — costs joins and simplicity.
  • SQL by default; NoSQL for scale and flexible data. CAP: choose CP or AP during partitions.

Practice what you learned

📝 Quiz: Databases at Scale5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →