LESSON 02 · One database

How far one database goes

Benchmark your own workload, and learn what usually gives out before raw throughput does.

SYSTEM VIEW

The decision

Diagram view
Peak queries—
Holds to—
Headroom—

Watch one database meet your peak, benchmark a copy to find its limit, then see what gives out first and what earns a second database.

  1. At its busiest the app takes 25 requests a second, each running 8 queries: 200 queries a second on the Database. How far one database goes depends on your schema, query mix, data size and hardware, so someone else’s figure tells you little.

    Peak
    25 requests/s
    Each request
    8 queries

    What changed

    • 25 requests a second
    • 200 queries a second
  2. Production-sized data goes into a Test copy, and the Load test replays your mix of reads and writes, raising the rate until p95 latency (the time 95% of requests finish within) climbs sharply. The Test copy holds its latency target to 4,000 queries a second.

    Data
    production-sized
    Mix
    your reads and writes
    Stop when
    p95 climbs sharply

    What changed

    • Test copy
    • Load test replays your mix
    • Holds the target to 4,000/s
  3. As an illustrative example, not a general claim: the peak of 200 queries a second is a twentieth of the 4,000 the benchmark held. With 20 times headroom, throughput gives no case for a second database yet.

    4,000 ÷ 200
    20×

    What changed

    • Load test and Test copy
    • Database at 200 of 4,000/s
    • No second database yet
  4. One bad pattern can spend that margin. The invoice list fetches 100 rows, then the App server runs one extra query per row, an N+1 pattern: a single request for 100 rows becomes 101 queries.

    Rows on the page
    100
    Queries
    1 + 100 = 101

    What changed

    • Invoice list of 100 rows
    • 1 request → 101 queries
  5. Something specific often gives out before total capacity. Try each limit: a missing index forces a scan that slows as rows grow; idle connections fill Postgres’s limit, which a pooler eases; a long transaction or migration holds locks that make queries wait; data outgrows memory, pushing reads to disk.

    Postgres
    a process per connection
    Pooler
    PgBouncer, for example

    What changed

    • A specific limit gives out first
    • Throughput still has headroom
    Limit
  6. Later, if reads stay slow across many queries even after tuning, add a Read replica. The Database becomes the Primary and takes every write; the replica copies it and serves reads a little behind, so it fits only if you can accept replication lag.

    When
    reads slow after tuning
    Cost
    replication lag

    What changed

    • Read replica
    • Database → Primary
    • Reads go to the replica
  7. If writes still saturate the Primary after tuning and a larger machine, that is real evidence for splitting data across databases, such as sharding. The Read replica can’t help: it serves reads but takes no writes away from the primary.

    After
    tuning + larger machine
    Then
    split the data

    What changed

    • Primary on a larger machine
    • Writes still saturate it
    • Next: split the data

Space plays · ← → step · Home resets

THE IDEA

How it works

How much one relational database can handle depends on your schema, query mix, data size, and hardware, so someone else’s figure tells you little. Get your own: load production-sized data into a copy, replay a realistic mix of reads and writes, and raise the rate until p95 latency (the time 95% of requests finish within) climbs sharply.

As an illustrative example, not a general claim, say your app peaks at 25 requests per second with 8 queries each: 200 queries per second. Your benchmark holds its latency target up to 4,000 queries per second, 20 times that peak, so throughput gives no case for a second database yet. One bad pattern can spend that margin: a list page that runs one extra query per row (an N+1 pattern) turns one request for 100 rows into 101 queries.

What breaks first is often something specific rather than total capacity. A missing index forces a scan that slows as the table grows. Connections run out: Postgres gives each connection its own process, so many idle app connections can reach its limit, which a pooler such as PgBouncer eases. A long transaction or schema migration holds locks that make other queries wait. Data can outgrow memory, pushing reads to disk.

Reads that stay slow across many queries after tuning argue for a read replica, if you can accept replication lag. Writes that saturate the primary after tuning and a larger machine argue for splitting data across databases, such as sharding, because replicas do not take writes away from the primary.

DECISION POINT

Your SaaS runs on one Postgres instance. Dashboard p95 has climbed from 200 ms to 3 seconds over six months, CPU sits near 85% during the workday, and pg_stat_statements shows a single query taking 60% of total database time. A teammate proposes a read replica and Redis. What do you do first, and what would change your mind?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Benchmark your own query mix on production-sized data; borrowed numbers do not transfer.
  • Look first for missing indexes, connection limits, locks, and memory pressure.
  • Saturated writes after tuning and a bigger machine are real evidence for splitting data.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A vendor publishes an impressive queries-per-second figure for your database. How should it affect your decision about adding a second database?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.