BE3: Fast queries

Lesson 5 of 5 · reading 15–25 minutes; practice and project time is on the module page

When one database isn't enough

🎯You'll be able to

Explain read replicas, partitioning and sharding, what each solves, and what each costs, so you can choose (or decline) them.

🏠The everyday picture

A busy library first adds more reading-room copies of popular books (replicas). Then it shelves old newspapers by year in separate rooms (partitions). Only when one building is truly full does it open branches in other areas, each holding part of the collection (shards), and now finding "every book by this author" means asking several branches.

📘What it really is

📝A worked example

TiffinBox grows to 400 kitchens and 50 million orders. Choices, in order:

Problem Option Trade-off
Kitchen reports slow down checkout Read replica for reports Reports may be a few seconds behind
orders table hard to maintain at 50 million rows Monthly partitions on placed_at Queries without a date range read every partition
Write load beyond one server Shard by kitchen_id "All orders for one customer" spans shards; needs a separate index or service

Expected outcome: a written decision for each step, with the measurement that triggered it.

🌍Where it's used

📝Case scenario

A startup shards its database across four servers before launch "to be ready for scale". Most of their queries are "everything for one customer", but they sharded by kitchen. Every customer page now queries four servers, development slows down, and their real traffic would have fitted on one database for years. A single well-indexed PostgreSQL with a replica would have served them better.

🖱️Try it

For TiffinBox, write which single change you'd make first if (a) the kitchen dashboard is slow every evening, (b) the orders table reaches 200 million rows, (c) checkout writes exceed what one server can take. Success: each answer names the option, the measurement you'd check first, and one cost.

⚠️Common mistakes

🛠️For your project

Your capstone handover notes how TiffinBox would scale its database next, and what you'd measure before doing it.

✅Quick check

What is replication lag, and when does it matter?

Show the answer

The delay before a replica has the latest changes; it matters when a user expects to see what they just wrote.

What does sharding make harder?

Show the answer

Queries and transactions across shards, and moving data between shards.

← PreviousConnections and big result sets
ProLearners Assist