
- John and Alan launch a simple social app, Chirp: users sign up, post short public messages, and follow each other.
- Day 1: one app server and one database serve a few hundred users. The team focuses on features and user growth, not scaling.
- Chirp’s data is stored in structured tables: a Users table (one row per user) and a Chirps table (one row per post).
- Reads: fetching data — opening a profile or viewing a feed.
- Writes: creating or updating data — posting a Chirp, following someone, incrementing counters.
- At 200 users, a standard instance is fine.
- At thousands, the team upgrades to larger machines (vertical scaling).
- At millions of users, vertical scaling runs into practical and cost limits: providers may not offer larger instances, or costs spike sharply.

- As more clients request data concurrently, read queries queue up and latency increases.
- Example: a profile view that was ~30 ms can slow to ~3 seconds as the single database becomes overloaded.

- Industry best practice avoids relying on a single machine: add read replicas.
- A replica is a copy of the primary database. It provides:
- High availability (a replica can be promoted if the primary fails)
- Read scaling (replicas serve read-only queries)

Read replicas are commonly asynchronous: the primary applies writes, then changes are replicated to replicas. This can introduce replication lag, meaning a replica’s data may be slightly behind the primary for a short period.
- All writes must go to the primary to maintain a single source of truth.
- High write volume (tens of thousands of posts per minute) concentrates on the primary, producing throughput limits, slow writes, or failed writes during spikes.

- Imagine a post shows 99 likes. Chris and Martin like it simultaneously.
- If Chris’s like is processed by the primary and updates to 100, a replica that hasn’t applied that update yet will still show 99.
- If Martin’s like is routed to that lagging replica and increments locally to 100, the system reports conflicting totals (both 100) even though the true total should be 101.
- Without a single authoritative writer, independent updates on different machines cannot be deterministically reconciled.

Because the primary must accept every write to guarantee consistency, write throughput and concurrency are limited by the primary machine. When write latency or errors rise despite vertical scaling and replication, it’s time to consider horizontal partitioning (sharding).
- Horizontal scaling (sharding) splits your data across multiple smaller database instances so both reads and writes can be distributed.
- Typical triggers for sharding:
- Sustained write throughput exceeds primary capacity.
- Read replicas cannot keep up (replication lag causes user-visible inconsistencies).
- Cost or availability limits prevent further vertical scaling.
Next step: sharding
- When a single primary is the bottleneck and replicas no longer provide enough headroom, the natural next step is horizontal partitioning: split the database into multiple independent shards. Sharding distributes both reads and writes across many machines, reducing the load on any single primary and enabling larger overall throughput.
- Database Replication Concepts — Overview
- When and How to Shard a Database (pattern overview)
- Designing Data-Intensive Applications — Chapter on Partitioning and Replication