← Building blocks

Know your read amplification

Storage & data modellingsenior

One logical read touches many places: several LSM levels, a cache miss then the DB, a fan-out to N shards, or N+1 queries.

Say it like this

A home feed is one logical read but hundreds of physical ones once I count the timeline fetch, the fan-out to post and user caches and the misses that hit the database, so I denormalise the read model and batch the lookups into a handful of calls.

Basic solution

Make the common read a single lookup: denormalise, cache the assembled result, keep per-key data on one shard, and use Bloom filters so missing keys skip disk.

Bloom filtersdenormalised read modelssingle-shard access patternsMGET batching

One feed read, before and after batching

The naive path issues a cache call per post and per author and a database query per miss. Batching turns that into two MGETs and at most two IN queries; denormalising the author fields removes a call entirely.

How it works

  1. Trace one user-facing read from the request to the response and list every store it touches: each cache lookup, each database query, each shard the query fans out to, and each service call.
  2. For each store, count how many physical operations happen (one GET per id is fifty operations for fifty ids) and note which of them run on the critical path one after another, versus in parallel.
  3. On an LSM engine, add the cost inside one lookup: a point read checks the memtable and then every SSTable whose key range could hold the key. Bloom filters let it skip most files, but a miss can still touch several.
  4. Multiply by the miss rate, because a cache hit costs one operation and a miss costs the database as well: a 95% hit rate means one read in twenty pays the database cost, and those reads set the tail latency.
  5. Reduce the count: store the assembled result so one read returns it (denormalise), fetch many ids in one call (MGET, IN lists), keep everything a user's hot path needs on one shard, and use Bloom filters so a lookup for a missing key never reaches disk.

The number interviewers want

  • Physical reads per logical read on the hot path, before and after your change.

Worked example

News feed home timeline

  • One GET /feed/home: 1 ZREVRANGE on the timeline cache (800 ids, keep 50), then hydrate 50 posts and 50 authors.
  • Naive: 50 GET post + 50 GET user + misses to the database at 5% = 100 cache calls plus 5 database queries per feed read, all on the hot path. At 20K feed reads per second that is 2M cache ops and 100K database queries per second.
  • With batching: 1 MGET for posts, 1 MGET for users (deduplicated, usually 30–40 authors), and the misses batched into one IN (...) query each: 3 cache calls plus up to 2 database queries. 2M cache ops becomes 60K and the database sees about 40K queries per second, mostly from a replica.
  • With denormalisation: store author handle and avatar URL inside the cached post body, so the user MGET disappears for the common case. 2 calls per feed read.
  • Latency: each hop is 0.3–1 ms on the cache and 2–5 ms on the database; cutting 100 sequential-ish calls to 2 batched ones drops p50 from about 30 ms to 3 ms and takes the database off the p99.

Distributed cache point reads on an LSM-backed store

  • Without Bloom filters, a miss checks every SSTable in the key's range (say 8 files × 1 disk read). With 10-bit-per-key Bloom filters the false-positive rate is about 1%, so a miss costs roughly 1.08 reads instead of 8.

Variants and when each wins

Denormalised read model

  • The data is stored in the shape the read wants and updated whenever the source changes. It wins when reads dominate, because a read is one operation; the cost is that writes maintain more copies and must handle partial failure.

Batching and pipelining

  • The data stays normalised but is fetched in bulk, many keys per request. It wins as the first step because it is cheap and safe, cutting round trips without changing the data model; the hot path still depends on every key being cached.

Materialised views and CQRS

  • A separate read store is built from the write store's events and shaped for each query. It wins when the read shape and the write shape genuinely differ, at the cost of eventual consistency and a pipeline to operate.

Fewer, larger records

  • A conversation's recent messages are stored as one blob instead of one row each. It wins when a read always wants the whole group, because one read replaces fifty; updates rewrite the blob and concurrent writers need care.

Start with batching (cheap and safe), denormalise the two or three fields that cause most reads, and reach for a full read model only when the read shape and the write shape genuinely diverge.

Failure modes

N+1 queries

  • Latency grows in proportion to the length of the list being rendered. It happens because the code runs one query per item inside a loop. Collect the ids first and issue one IN query or one MGET, or use DataLoader-style batching in the API layer.

Cache miss storm on the hot path

  • p99 latency jumps every time a cache node restarts. It happens because each miss becomes a synchronous database read, and a cold node produces thousands at once. Warm caches before they take traffic, serve stale values while refreshing, and cap the number of misses one request may trigger.

Cross-shard fan-out for one key

  • Every read touches every shard. It happens because one user's data was spread across shards by a poorly chosen partition key. Pick the partition key so a user's hot data lives on one shard, and use a secondary index only for rare queries.

Too many SSTables

  • Point reads on a store that was fast last month are now slow. It happens because compaction fell behind, so each read has to check dozens of files. Tune compaction, enlarge the Bloom filters, or switch the read-heavy table to levelled compaction.

Denormalised copy goes stale

  • A user who changed their handle still shows the old one in feeds. It happens because the handle embedded in cached post bodies was never refreshed. Version the embedded fields and refresh them on read when the version is old, or accept the staleness for cosmetic fields and say so.

Interviewer follow-ups

Answer out loud before you open each one.

How do you find read amplification in an existing service?

Trace one request end to end and count spans per store: a distributed trace shows the fan-out directly. Then compare cache and database operations per second against user-facing requests per second; a ratio in the hundreds is the sign.

Batching reduces round trips, but does it reduce work on the store?

Partly. The store still looks up every key, but it does so in one request with shared parsing, one network transit and the chance to read keys on the same page together. The big win is latency and connection count; the store’s CPU drops less than its request count does.

Why do Bloom filters reduce read amplification on LSM trees?

Each SSTable carries a filter that answers "definitely not here" for most keys it does not contain, so a point read skips those files without touching disk. With about 10 bits per key the false-positive rate is near 1%, turning a miss from N file reads into roughly one.

When is denormalising for reads the wrong call?

When the embedded fields change often or must be exactly current, such as balances or inventory, because every change then fans out into many copies and the window of inconsistency is user-visible. Keep those as a single lookup and denormalise only slow-changing fields.

Connections

Built on
LSM tree · Bloom filter
Pairs with
Precompute joins · Batch small operations · Write-optimised vs read-optimised storage · Index the columns you filter on · Know your write amplification
Pulls against
Know your write amplification
Used in
News Feed (Twitter / X timeline) · Distributed Key-Value Cache (Redis / Memcached-like)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2 Auto ScalingFeed serviceCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon ElastiCacheTimeline cache, Post cache, User cacheMemorystoreAzure Managed RedisRedis, Valkey, Memcached
Amazon AuroraDB replicaAlloyDB for PostgreSQL, Cloud SQLAzure Database for PostgreSQL or MySQLPostgreSQL, MySQL, CockroachDB