Back to blog

The database is hot: where should 90 percent of reads go?

Thắng Đoàn
Thắng Đoàn

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

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.