Lesson 60 — System Design IV: database choices

~25 minutes · Day 78 · system design

Tonight's interview move is choosing the right database shape before the diagram gets fancy. Most weak answers say "use SQL" or "use NoSQL" too early. Strong answers ask what the product reads, what it writes, how fresh the read must be, and which request will melt first.

The invariant

Replication copies data to scale reads and survive failures; sharding splits data to scale writes and size; indexes buy fast reads at write cost. Choose per access pattern.

An access pattern is one real product question: "get one user's orders," "append one chat message," "show the last 50 posts," "transfer money." The database choice follows that question. Do not start with a brand name; start with the request.

The script

Use the same seven-step system-design script every time. Here it is applied to database choices.

  1. Requirements: decide which reads must be fresh, which writes must be all-or-nothing, and whether the data is relational, document-like, or key-value.
  2. Estimate scale: write down read QPS, write QPS, row size, total data size, and growth. Reads, writes, and size fail in different ways.
  3. API: name the endpoints because they reveal access patterns: GET /users/{id}, POST /orders, GET /orders?user_id=....
  4. Data model: choose tables or documents around the transaction boundary. If money, inventory, or ownership can be half-wrong, prefer an ACID transaction boundary (PostgreSQL docs: transactions).
  5. High-level boxes: draw app servers, a routing layer, primary data stores, read replicas, shard boundaries, and the indexes that serve the common lookups.
  6. Deep-dive the bottleneck: if reads hurt, replicate or index; if writes or data size hurt, shard; if correctness hurts, shrink the transaction to the smallest safe boundary.
  7. Failure modes: call out replica lag, hot shards, missing indexes, cross-shard transactions, backups, schema migrations, and what happens when the primary dies.

The architecture diagram

This is the picture to redraw from memory. Notice that replication, sharding, and indexes answer different pains.

Database scaling architecture with replicas, shards, and indexes Clients call application servers, a database router sends writes to shard primaries, reads can use replicas, and indexes sit inside each shard for fast lookups. Clients web / mobile API servers validate request DB router uses shard key Shard A primary users 0...4999 index: user_id Shard B primary users 5000... index: user_id writes split by shard key Shard A replica read-only copy Shard B replica read-only copy replicate replicate stale-safe reads can use replicas Key insight choose per access pattern

The bottleneck deep-dive

In interviews, do not say "just shard it" as the first move. Name the bottleneck, then choose one of these three moves.

SQL versus NoSQL is the next layer, not the first layer. Prefer SQL when relationships, joins, and transactions are central. Prefer a key-value or document store when the access pattern is simple, high-volume, and usually reads or writes one aggregate by key. Either way, the invariant stays the same: copy for reads, split for writes and size, index for lookup speed.

Numbers that matter

Rule of thumbWhat it tells youSource
3 replicas tolerate 1 failure in quorum systemsReplication helps survival, but more replicas can add write coordination cost.etcd FAQ
1 hot request should touch 1 shardChoose a shard key that routes common reads and writes directly.MongoDB sharding
+1 index means +1 maintained structureIndexes speed lookups, but every write must keep them synchronized.PostgreSQL indexes
>0 replica lag means possibly stale readsUse replicas for reads that can tolerate freshness delay.Amazon RDS replicas

Practice — answer before you scroll past

1. Read traffic is overwhelming, writes are steady. What is the first database move?

2. A lookup is slow: GET /orders?user_id=7 scans orders. What should you try before sharding?

3. A system shards orders by created_at. Today's shard is melting. What went wrong?

4. Which request is safest to serve from an async read replica?

Your move (tonight)

Redraw the architecture diagram from memory on paper. Then explain the design out loud in 5 minutes as if to an interviewer: requirements, scale, API, data model, boxes, bottleneck, failure modes. No LeetCode tonight for this lesson.

One primary source

After your redraw, read AWS — Choosing an AWS database service as the compare step. Focus on how each database type is tied to an access pattern, not to hype.