Make the right trade-off / CHAPTER 45
Read-heavy versus write-heavy workloads
Count the work behind each read and write, including popular keys and copied views, before choosing an optimization.
Builds on SQL versus NoSQL data stores, Back-of-the-envelope estimation, Data partitioning.
- Estimate total and per-key operations.
- Explain index and projection write amplification.
- Choose scaling controls for distinct bottlenecks.
01A ratio needs a cost model
A feed receives many page reads and fewer reactions. Calling it “99% reads” counts requests, but does not tell us how much work each causes.
- A read-heavy workload reads much more often than it changes records.
- A write-heavy workload spends much of its work accepting or changing records.
- One page request may load 20 profiles; one reaction may update several display totals, indexes and replicas.
- One broad scan can cost more than thousands of keyed reads. One viral article can overload one partition while other machines are mostly idle.
For every important operation, name the records examined, bytes transferred, required completion time and failures the saved result must survive. Then measure which resource is full.
Caching, indexes and read replicas can help some read loads. Efficient appends, partitions and batching can help some write loads. They are possible responses to measured work, not mandatory templates inferred from the ratio alone.
02Count amplification on both paths
Follow both sides of the article feed:
- A reader requests a page. If the application loads 20 profiles, that one user operation causes 20 profile lookups. This extra underlying read work is read amplification.
- A writer edits a post or adds a reaction. The source record may also require updates to indexes, copied display views, logs and replicas. This extra work is write amplification.
- Count the logical storage operations first. Physical disk work can differ because the storage engine buffers, compresses or rewrites data.
- Measure popular keys and partitions. Cluster-average spare capacity does not help a single record whose updates must pass one saturated owner.
| Measured problem | Candidate change | What must be paid for? |
|---|---|---|
| Many reads repeat one article | Reuse sufficiently recent cached copies. | Edits, missing copies and cache failure still load the source. |
| Finding one author’s 20 newest posts scans many unrelated rows | Index the author ID together with publication time. | Post writes must maintain those index entries; measure the resulting query rather than assuming the index fixes every read. |
| Display reads tolerate more delay than the replica is observed to have | Send those reads to that replica. | A confirmation needing a saved edit must check that the selected copy has applied that version. |
| Writes maintain unnecessary views | Remove, batch or delay that work. | Required views still need completion, recovery and an allowed delay. |
| Independent records overload one machine | Partition by suitable keys. | Uneven traffic and operations crossing partitions remain. |
A copied display result is often called a projection. Name its original records and how it is rebuilt; it is not another independent way to change the source facts.
03Worked example: reactions and article reads
Assume 100,000 article reads/s and 1,000 edits/s. At an assumed 98% cache hit rate, about 2,000 reads/s still need the source, plus refresh work.
Follow an edit to popular article 42:
- Save the new article version and pending work for required display copies.
- Retain the new version as the minimum allowed cache version, even if removing the value. A fill compares its loaded version with that minimum in the same cache operation that saves it; an older delayed reader then cannot restore the previous article.
- Readers now missing that article share one compatible source fetch where possible, instead of all loading it separately.
- Measure the hit rate under these edits. Frequent invalidation can defeat the original 98% reuse assumption.
- Test cache failure. Up to 100,000/s could reach the source: 50 times the normal 2,000/s miss rate. Limit that fallback workload before it overloads storage.
Now accept 20,000 reactions/s and update four display views for each. That is 80,000 logical view updates/s before source writes, replicas and retries. Removing one unused view saves 20,000 updates/s. A queue can delay these writes, but sustained processing must still catch arrivals or waiting work grows.
If each account may react only once per article, enforce a unique account/article record in the reaction store. The display count may update later if allowed. Splitting or approximating that count must not permit duplicate saved reactions. The source rule and the display's delay answer different questions.
Read-heavy versus write-heavy workloads — decision flow
Follow the numbered arrows
- Article feed users → Article/profile cacheread reusable article or profile data
- Article/profile cache → Saved article/reactionsload a missing value from its source
- Article feed users → Saved article/reactionssave the article edit or unique reaction
- Saved article/reactions → Pending view updatessave pending copy-update work
- Pending view updates → Copied display viewsapply each required view update
Trace the diagram in words
- A page request can require many profile lookups or missing-cache loads.
- Save the source change before maintaining copied display views.
- Count normal work plus cache-refill bursts and backlog processing after failure.
04Choose a bottleneck-specific response
Optimize the demonstrated bottleneck:
- For reads: inspect the query, page size and batched lookups before adding copies. Cache only where reuse is useful; for an edit confirmation, pass the saved edit version and use a source whose query-visible version is at least that number.
- For writes: remove unused indexes or views, batch only within the allowed delay, and spread genuinely independent records.
- For a popular single key: explain why spare machines can or cannot share that exact operation. Adding unrelated partitions is not automatic relief.
- For recovery: returning workers must process old work alongside current traffic. Limit catch-up concurrency and track the oldest pending update.
- For every comparison: test normal, burst and failure loads, with the same correctness rules.
In an interview, count the storage work behind one page and one reaction. Then change popularity or disable the cache. A read/write percentage becomes useful only when attached to this concrete cost and failure model.
DRAW. CALCULATE. EXPLAIN.
Count the feed’s real work
20 minutes · guided practice
A feed receives 20,000 page reads/s, each requiring 15 profiles. It accepts 6,000 reactions/s and maintains three projections. One projection is unused. Calculate work, propose read and write improvements, and state a cache-outage policy.
Compare the worked solution
- Twenty thousand pages/s × 15 profiles is 300,000 profile lookups/s before batching or cache reuse.
- Six thousand reactions/s × three views is 18,000 view updates/s. Removing the unused view saves 6,000 updates/s.
- Batch profile lookups or cache reusable profiles with a stated allowed age, then measure actual backend work.
- Enforce one saved account/article reaction; delayed display totals do not enforce that rule.
- During cache failure, cap source reads and concurrent work. Serve older display data only within its permitted age, or reject excess requests rather than assuming the normal miss rate.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01Why is a read/write percentage insufficient?
- Count records and bytes behind each request.
- Include copied-view and index updates.
- Measure traffic concentrated on popular keys.
One broad read or amplified write can dominate the ratio.
CARD 02What does read acceleration add to writes?
- Indexes, replicas and display copies need updates.
- They consume storage and recovery work.
- Compare those costs and permitted delay with the actual read work saved.
CARD 03What happens when a read cache fails?
- Every read may reach the source.
- Simultaneous misses can multiply identical fetches.
- Limit fallback traffic and share compatible fills while preserving purchase and permission checks.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.