The database is hot: where should 90 percent of reads go?
FotoFlow has 5,000 users and every photo page asks the database for the same kind of thing: name, tags, likes. The database is now 92 percent busy, reads queue up, and pages slow to nearly half a second at p95. Databases are usually the hardest part to scale, because they hold the data.
The same few things, over and over
Look at what is actually read: a small set of popular photos accounts for most traffic, and the database recomputes the same answers thousands of times a minute. Answers that are read often and change rarely are perfect to keep somewhere closer and faster.
Cache-aside: the default pattern
Add an in-memory cache such as Redis or Memcached. The server checks it first: a hit returns in about a millisecond, because a cache lookup is one key fetched from memory, no query planning, no joins, no disk. On a miss, the server reads the database and saves a copy for next time. This is cache-aside, and it typically answers 10 to 100 times faster than the query it replaces.
Hit rate is everything
The share of reads answered by the cache is the hit rate, and because misses are so much slower, small changes matter enormously. Going from 80 to 95 percent hits more than halves the average read time and cuts database load by three quarters. A cache only needs to hold the hot set; 10 percent of the data can easily serve 90 percent of reads. When memory runs out, LRU eviction removes the least recently used entry, keeping hot items while the long tail cycles through.
Keeping the copy honest
A cache is a second copy of the truth, and an admin changing a price from 20 to 25 dollars does not change the cached copy: users still see 20. When data changes, the server writes the database and deletes the cached copy, so the next read reloads the fresh value. Deleting beats overwriting here, because two racing writes can leave the older value in the cache, while a delete is always safe. As a safety net, every entry gets a TTL, so even a missed invalidation disappears within a minute. Pick the TTL by asking how stale is acceptable: seconds for counts and stock, hours for a description.
When the hot key expires
The home feed is the hottest key in FotoFlow, 2,000 reads a second. Its TTL expires, and in the next instant every one of those requests misses and hits the database with the same query. The database, used to 8 percent load, stalls. This is a cache stampede, and the more popular a key, the more dangerous its expiry. The fix is request coalescing: the first miss takes a short lock and fetches once, everyone else waits milliseconds for that one result. Refresh hot keys just before expiry, and add random jitter to TTLs so keys do not expire together.
Asking for what is not there
A crawler requests photo ids that do not exist, one after another. Each misses the cache, searches the database, finds nothing, and caches nothing, so every repeat pays full price forever. Cache-aside only remembers answers, and no such row is an answer too. First fix: negative caching, store "not found" with a short TTL. For crawlers that never repeat, keep a Bloom filter of real ids in memory: a compact bit set answering "definitely not here" or "probably here" in microseconds, at about 10 bits per key.
What this does not solve
A cache in one region makes the database cheap for reads, but FotoFlow users are worldwide, and no cache fixes 200 milliseconds of physics between Sydney and Virginia. Bytes that are the same for everyone need to live near the users. That is the next post.
If your hottest cache entry expired right now, how many identical queries would hit your database in the same second?
What is the acceptable staleness for the numbers on your most-viewed page: seconds, minutes, or hours?
Fast reads are remembered answers.
Next in the series: Why are your photos slow on the other side of the world?
Sources
Redis Documentation
MDN Web Docs: HTTP caching
undefined
Designing Data-Intensive Applications
Recommended for you
- Craft & Skills
One server, 10 million users: how does a photo app survive that?
A 40-post series that follows FotoFlow, a photo app, from one server and 100 users to 10 million users with direct messages. One problem, one fix, one honest cost at a time.
- Craft & Skills
Ten million users want direct messages: could you design the chat system?
The final design: 20 million open sockets and a promise that every message arrives, in order, exactly once. Dumb gateways, a connection registry, store-first durability and sync by cursor.
- Craft & Skills
Five million home feeds: how do you build the page everyone opens?
The home feed must load in 200 ms from two hundred followed accounts. Fan-out on read melts at 4.6 million lookups a second; fan-out on write melts on celebrities; the hybrid survives both.
- Craft & Skills
FotoFlow wants short share links: could you design one today?
The first full design walkthrough: turn a one-line brief into numbers, the simplest system, then fix codes, caching, storage and abuse one bottleneck at a time. The method is the point.
Enjoyed this article?
Subscribe for new articles. No spam. Unsubscribe anytime.
Typed live, never pasted.