LESSON 04 · Cheaper fixes first

Do you need a cache or a queue?

Try a query fix, an index, or a database job table first, and add new infrastructure when evidence asks for it.

SYSTEM VIEW

The decision

Diagram view
Page time—
Cache saves—
DB job queries—

Watch a slow invoice list get cheaper fixes before a cache, and slow work go into a job table before a broker.

  1. In an illustrative case, User opens the invoice list and Database scans the whole table for one customer’s rows, so the page takes 1.8 s.

    Query
    one customer’s invoices
    Plan
    whole-table scan

    What changed

    • GET /invoices
    • Whole-table scan: 1.8 s
  2. Cheaper fixes come first: fix the query (remove an N+1 pattern, say), add an index, then precompute heavy totals in a summary table. Database now reads by an index on (customer_id, created_at), so the page might take 40 ms, not 1.8 s.

    1. Fix the query
    no N+1
    2. Index
    (customer_id, created_at)
    3. Summary table
    precomputed totals

    What changed

    • Index on (customer_id, created_at)
    • Page 1.8 s → 40 ms
  3. A cache brings stale data, invalidation and cold starts, and a key without the customer ID can leak data. Add one once measurements show reads that repeat, stay expensive after tuning, can be slightly old and matter to total database time. Cache’s 95% hits on 3% of database time save under 3%.

    Hit rate
    95%
    Query’s share
    3% of DB time
    Saved
    under 3%

    What changed

    • Cache
    • 95% hits on one query
    • Database time: under 3% less
  4. Cache goes, since under 3% doesn’t earn its place. For slow work such as receipt emails, start with a job table in the same database. App server marks invoice 41 paid and inserts its email job in one transaction, so neither exists without the other.

    Job
    receipt email, invoice 41
    One commit
    paid + job row

    What changed

    • Cache (saved under 3%)
    • Job table in Database
    • Paid and job in one commit
  5. Worker 1 and Worker 2 claim rows with FOR UPDATE SKIP LOCKED, so they take jobs 41 and 42 without blocking each other. At 600 jobs a minute, 10 a second, about 2 queries each (claim and finish) make 20 queries a second.

    Jobs
    600/min = 10/s
    Per job
    2 queries: claim, finish
    Job queries
    20/s

    What changed

    • Worker 1, Worker 2
    • Claims with SKIP LOCKED
    • 20 job queries a second
  6. When job queries rank among your costliest, row churn keeps Postgres vacuum busy, or several consumers need the same events, Message broker takes the jobs and Database runs no job queries. Brokers don’t give exactly-once processing, so keep every job safe to retry.

    Cost
    job queries rank high
    Churn
    vacuum kept busy
    Consumers
    several, same events

    What changed

    • Message broker
    • Job queries on Database
    • Jobs must stay retry-safe

Space plays · ← → step · Home resets

THE IDEA

How it works

A cache adds stale data, invalidation, and cold starts; in a multi-tenant product, a key that omits the customer ID can leak one customer’s data to another. So try cheaper fixes first: fix the query (removing an N+1 pattern, say), add an index, then precompute heavy totals in a summary table or materialized view (a stored query result). In an illustrative case, a page that scans a whole table and takes 1.8 seconds might take 40 ms with an index on (customer_id, created_at).

Add a cache once measurements show that the same expensive result is read repeatedly, tuning was not enough, slightly old data is acceptable, and those reads matter to total database time. A 95% hit rate on a query that accounts for 3% of database time saves under 3%.

For slow work such as emails or PDFs, start with a job table in the same database. Insert the job in the same transaction as the business change, so neither can exist without the other. Workers claim rows with Postgres FOR UPDATE SKIP LOCKED, so several workers can take different rows without blocking. For example, 600 jobs a minute is 10 per second; at about 2 queries per job (claim and finish), that is 20 queries per second.

Move to a broker such as RabbitMQ or Kafka when job queries rank among your costliest, row churn keeps Postgres vacuum (its cleanup of dead rows) busy, or several independent consumers need the same events. Brokers do not give exactly-once processing, so make jobs safe to retry either way.

DECISION POINT

The invoice list for your largest customer takes 4 seconds. A teammate’s pull request adds Redis with a 5-minute expiry. Finance users mark an invoice paid and reload this page immediately. What do you do with the pull request?

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

Keep in mind

  • Try the query, an index, and a summary table before a cache.
  • A cache earns its place when repeated, expensive, staleness-tolerant reads matter to total load.
  • A database job table is a real option until job traffic measurably competes with users.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

Order emails are sent inside the request, and a slow email provider makes checkouts time out. You run only Postgres and send about 5 emails per second. What fits first?

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.