Using estimates to pick a design
35 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)You have the numbers: about 1,700 reads a second and 150 GB of data. Now the owner asks whether to buy a cache, more database servers or split the data. One number against one limit decides each.
From numbers to decisions: replicas, cache, sharding
Sharding is splitting the data across several databases by a key, such as the customer id. It is the most expensive step and is used only when one primary cannot hold the writes or the data.
Each decision compares one estimate with one limit: repeated reads need a cache, too many reads left need replicas, too many writes or too much data need sharding. The code decides for three systems.
Example 1 · From numbers to decisions
record Estimate(String name, double peakReadRps, double peakWriteRps, double dbGigabytes) { }
public class DecisionsDemo { // Rough comfort limits for ONE MySQL primary on a decent server (check with your own load tests) static final double DB_READS_OK = 5_000; static final double DB_WRITES_OK = 1_000; static final double DB_SIZE_OK_GB = 1_000;
static void decide(Estimate e) { System.out.println(e.name() + ":"); double dbReads = e.peakReadRps() * 0.2; // assume an 80% cache hit ratio System.out.println(" cache: " + (e.peakReadRps() > 1_000 ? String.format("yes - %,.0f reads/s, most of them repeat", e.peakReadRps()) : "not yet")); System.out.println(" replicas: " + (dbReads > DB_READS_OK ? String.format("yes - %,.0f reads/s still reach the database after the cache", dbReads) : String.format("not needed for load (%,.0f reads/s after the cache); keep one for failover", dbReads))); System.out.println(" sharding: " + (e.peakWriteRps() > DB_WRITES_OK || e.dbGigabytes() > DB_SIZE_OK_GB ? "consider it - writes or size beyond one primary" : "no - one primary holds the writes and the data")); }
public static void main(String[] args) { decide(new Estimate("ShopLite today", 1_724, 12, 150)); decide(new Estimate("ShopLite in 3 years", 17_240, 122, 600)); decide(new Estimate("Chat app, 50M DAU", 60_000, 25_000, 40_000)); }}ShopLite today: cache: yes - 1,724 reads/s, most of them repeat replicas: not needed for load (345 reads/s after the cache); keep one for failover sharding: no - one primary holds the writes and the data ShopLite in 3 years: cache: yes - 17,240 reads/s, most of them repeat replicas: not needed for load (3,448 reads/s after the cache); keep one for failover sharding: no - one primary holds the writes and the data Chat app, 50M DAU: cache: yes - 60,000 reads/s, most of them repeat replicas: yes - 12,000 reads/s still reach the database after the cache sharding: consider it - writes or size beyond one primary
What this code does
- Each decision compares one estimate with one limit: reads decide caching and replicas, writes and data size decide sharding.
- Both ShopLite estimates call for a cache. Even at 17,240 peak reads in three years, an 80% hit ratio leaves the database about 3,450 reads a second, within one server, and writes stay tiny. Only the chat app needs replicas and, with 25,000 writes a second and 40 TB, sharding.
- The limits are rough rules of thumb written as named constants, so the reasoning is visible and easy to adjust.
A cache keeps popular answers in memory, and a read replica is a copy of the database that only answers reads. Ask about them in that order and about sharding last.
Rules of thumb: one MySQL server, and the 80/20 hot set
The hot set is the part of the data that gets most of the reads. The 80/20 rule says about 20% of items get about 80% of the reads, so a cache only needs to hold that 20%.
To compare an estimate you need rough limits for one app server, one MySQL primary and one Redis node. The code prints them as a one-page table.
Example 2 · Rules of thumb in one table
public class RulesOfThumbDemo { public static void main(String[] args) { String[][] rules = { {"One app server (simple JSON)", "a few hundred to a few thousand RPS", "load test it"}, {"One MySQL primary, simple reads by key", "several thousand to tens of thousands QPS", "if the hot data fits in memory"}, {"One MySQL primary, writes", "about a thousand to a few thousand per second", "depends on disk and indexes"}, {"One MySQL server, data size", "up to around 1 TB is comfortable", "backups and changes slow down beyond"}, {"One Redis node", "tens of thousands of ops/s or more", "data must fit in its RAM"}, {"Hot set", "about 20% of items get about 80% of reads", "the 80/20 rule"}}; for (String[] r : rules) { System.out.printf("%-40s %-46s (%s)%n", r[0], r[1], r[2]); } }}One app server (simple JSON) a few hundred to a few thousand RPS (load test it) One MySQL primary, simple reads by key several thousand to tens of thousands QPS (if the hot data fits in memory) One MySQL primary, writes about a thousand to a few thousand per second (depends on disk and indexes) One MySQL server, data size up to around 1 TB is comfortable (backups and changes slow down beyond) One Redis node tens of thousands of ops/s or more (data must fit in its RAM) Hot set about 20% of items get about 80% of reads (the 80/20 rule)
What this code does
- These are orders of magnitude, not promises. Real limits depend on hardware, query shapes and indexes, so state them as assumptions and confirm with a load test.
- The last row, the 80/20 rule, is the basis for sizing a cache: you only need to keep the popular 20% in memory to serve most reads.
A well-tuned MySQL primary on a decent server comfortably handles thousands of simple reads by key per second, about a thousand or more writes per second, and data up to roughly 1 TB. These are orders of magnitude, so say them as assumptions.
Worked example: sizing a cache from the hot set
Cache size = items × bytes per item × hot share × overhead. For ShopLite in year five, the code works it out and shows how few reads reach MySQL.
Example 3 · Sizing a cache from the hot set
public class CacheSizingDemo { public static void main(String[] args) { long products = 150_000; int cachedBytesPerProduct = 5_000; double hotShare = 0.20; // 20% of products ... double readsOnHotSet = 0.80; // ... get 80% of reads double overhead = 2.0; // keys, pointers, fragmentation in Redis
double allBytes = (double) products * cachedBytesPerProduct; double hotBytes = allBytes * hotShare * overhead; System.out.printf("All product JSON: %.0f MB%n", allBytes / 1e6); System.out.printf("Hot 20%% with overhead: %.0f MB%n", hotBytes / 1e6);
double peakReads = 17_240; double dbReads = peakReads * (1 - readsOnHotSet); System.out.printf("Peak reads: %,.0f/s -> database still sees %,.0f/s (cache hit ratio 80%%)%n", peakReads, dbReads); System.out.println("Decision: one small Redis node (1 GB) holds the hot set, with room to spare"); }}All product JSON: 750 MB Hot 20% with overhead: 300 MB Peak reads: 17,240/s -> database still sees 3,448/s (cache hit ratio 80%) Decision: one small Redis node (1 GB) holds the hot set, with room to spare
What this code does
- The cache does not need every product, only the popular ones. 20% of 750 MB is 150 MB; doubling it for Redis overhead gives about 300 MB.
- With an 80% hit ratio the database sees only 1 in 5 reads: 17,240 peak reads become about 3,450, back within one MySQL server's comfortable range.
- So the estimate turns into two concrete choices: a 1 GB Redis node, and fewer read replicas than the raw read numbers suggested.
By hand: 150,000 products × 5 KB = 750 MB; the hot 20% is 150 MB; doubling for Redis overhead gives about 300 MB. With an 80% hit ratio (the share of reads the cache answers) only 20% of reads reach MySQL, so a cache this small removes most of the read load.
Presenting estimates aloud in under 3 minutes
A three-minute script is a fixed order for speaking an estimate: assumptions, traffic, storage, decisions, and what would change them, with rounded numbers.
The code prints a ShopLite estimate in a fixed order, with every number rounded the way you would say it.
Example 4 · A 3-minute estimate, said out loud
public class EstimateScriptDemo { public static void main(String[] args) { long dau = 1_000_000; int perUser = 30; int peak = 5; double avg = dau * perUser / 100_000.0; // quick: divide by 10^5 String[] script = { "1. Assumptions: " + dau / 1_000_000 + "M DAU, " + perUser + " requests each, peak " + peak + "x, reads:writes about 150:1.", "2. Traffic: 3 x 10^7 a day, so about " + (int) avg + " RPS average and about " + String.format("%,d", (int) (avg * peak)) + " at peak; writes about 10 a second.", "3. Storage: 50,000 orders a day x 200 B = 10 MB a day, about 20 GB in 5 years; images about 1.4 TB.", "4. Decisions: a few stateless app servers, a cache for products, one MySQL primary plus a replica, images in object storage with a CDN.", "5. Watch: if writes grow 100x, revisit sharding."}; for (String line : script) System.out.println(line); }}1. Assumptions: 1M DAU, 30 requests each, peak 5x, reads:writes about 150:1. 2. Traffic: 3 x 10^7 a day, so about 300 RPS average and about 1,500 at peak; writes about 10 a second. 3. Storage: 50,000 orders a day x 200 B = 10 MB a day, about 20 GB in 5 years; images about 1.4 TB. 4. Decisions: a few stateless app servers, a cache for products, one MySQL primary plus a replica, images in object storage with a CDN. 5. Watch: if writes grow 100x, revisit sharding.
What this code does
- Five short parts, in the same order every time: assumptions, traffic, storage, decisions, what would change them. It fits in under three minutes.
- The numbers are rounded (300, 1,500, 20 GB) because they are spoken. Saying "about" is honest and saves time for the decisions, which are what interviewers score.
Write each number on the board as you say it. Keep the maths simple (divide by 10⁵, round to one or two figures) and spend the saved time on the decisions. If the interviewer changes an assumption, redo only that line.
Order of execution
Order the script
Put the parts in the order you would speak them.
Common estimation mistakes
False precision is giving more digits than the guessed inputs justify, such as 347.22 RPS. Round to two significant figures and say "about".
The code shows three mistakes next to their fixes: false precision, ignoring the peak and mixing units.
Example 5 · Fixing common estimation mistakes
public class EstimationMistakesDemo { static String roundTo2Figures(double x) { if (x == 0) return "0"; double scale = Math.pow(10, Math.floor(Math.log10(Math.abs(x))) - 1); return String.format("%,.0f", Math.round(x / scale) * scale); }
public static void main(String[] args) { double avg = 1_000_000 * 30 / 86_400.0; System.out.println("False precision: " + avg + " -> say about " + roundTo2Figures(avg) + " RPS");
System.out.println("Designed for average: " + roundTo2Figures(avg) + " RPS; real peak (5x): " + roundTo2Figures(avg * 5) + " RPS");
// 3. Mixing units: 20 KB responses at 1,736 RPS double wrong = 1_736 * 20; // KB/s, then read as MB/s by mistake double right = 1_736 * 20_000 * 8 / 1e6; // bytes -> bits -> Mbit/s System.out.println("Mixed units: " + roundTo2Figures(wrong) + " \"MB/s\" (wrong) vs " + roundTo2Figures(right) + " Mbit/s (right)"); }}False precision: 347.22222222222223 -> say about 350 RPS Designed for average: 350 RPS; real peak (5x): 1,700 RPS Mixed units: 35,000 "MB/s" (wrong) vs 280 Mbit/s (right)
What this code does
- False precision is giving more digits than the guesses behind them justify.
roundTo2Figureskeeps two significant figures: 347.22… becomes 350. - Ignoring the peak designs for the average; the busiest minutes are five times higher. Mixing units (KB as MB, bytes as bits) makes answers wrong by a factor of 1,000 or 8.
Ignoring the peak designs for the average, and the system fails at 9 p.m. on sale day. Mixing units (ms and s, KB and MB, bytes and bits) makes answers wrong by 8× or 1,000×. A quick guard: write the unit next to every number and check that the units cancel.
Find the bug
Find the mistake
Click the line that gives the wrong answer.
This should size the app servers for the busiest moment.
Round 1 of 2Your turn: ShopLite in three years
In three years ShopLite has 17,000 peak reads a second, 120 writes a second and 600 GB of data. Decide on paper first: cache, replicas, sharding? Then check each number.
Predict the output
Decide, then check
Work it out yourself, then pick the best answer.
Round 1 of 2
With an 80% cache hit ratio, how many reads reach MySQL?
Next step
Check your understanding
12 quick questions