Sub topic 10
Sizing a Cache and Working Set in Memory
This chapter assumes you can convert daily active users (DAU) to queries per second (QPS), estimate storage from record size, and read simple Python. It also assumes you know what a cache is: a fast memory store placed in front of a slower database.
Why the whole dataset is the wrong number
A common error is to size the cache to hold all the data. That defeats the purpose. Access is usually skewed: a small share of items receives most of the reads. The set of items that is actually read during a window of time is the working set. The cache only needs to hold the hot part of it.
A frequent starting rule is the 80/20 rule. About 20% of items serve about 80% of reads. Treat this as an assumption you state, not a law.
The estimation steps
- Estimate daily read requests, using the traffic method from earlier chapters.
- Estimate the size of one cached object.
- Decide what share of the data to cache, such as 20%.
- Multiply, then add overhead for keys, metadata and headroom.
The relevant sizes come from the storage chapter, and the read rate from the traffic chapter. Latency matters here too. A memory read takes about 100 nanoseconds, while a disk-backed database query takes milliseconds. That gap is why the cache is worth paying for.
A worked calculation
Assume 10 million DAU, 10 reads per user per day, and 1 KB per cached object. Cache the hottest 20% of the requested data.
dau = 10_000_000
reads_per_user_per_day = 10
bytes_per_object = 1_000
hot_fraction = 0.20
overhead = 1.3 # keys, metadata, fragmentation
daily_reads = dau * reads_per_user_per_day
daily_bytes = daily_reads * bytes_per_object
cache_bytes = daily_bytes * hot_fraction * overhead
print(daily_reads) # 100000000
print(daily_bytes / 1e9) # 100.0 (GB of data read per day)
print(round(cache_bytes / 1e9)) # 26 (GB of cache)The result is about 26 GB. One large server holds that in RAM, so a single cache node, or a small replicated pair, is plausible.
This calculation treats every read as a distinct object. In practice many reads hit the same items, so the true unique data is smaller. Your estimate is therefore an upper bound, which is a safe direction to err in.
Hit ratio and what it buys
The hit ratio is the share of reads served from the cache. If it is 90%, the database sees only 10% of read traffic. At 2,315 peak QPS, that leaves about 230 QPS for the database. This is often the more useful number, because it shows whether the database can survive without scaling out.
| Hit ratio | Database load at 2,315 peak QPS |
|---|---|
| 80% | about 463 QPS |
| 90% | about 232 QPS |
| 99% | about 23 QPS |
Going from 90% to 99% cuts database load tenfold, but it usually needs a much larger cache, because the long tail of rarely read items is large.
Common errors
- Sizing for total stored data instead of the hot subset.
- Forgetting overhead. Keys, pointers and allocator waste can add 20% to 50%.
- Ignoring replication. If you keep two copies for availability, double the memory.
- Mixing units, such as using GB for data and GiB for server RAM.
Exercise
A service has 50 million DAU, each making 4 reads per day of 2 KB objects. You cache the hottest 10% and add 30% overhead. How much cache memory is needed?
Show answer
Daily reads = 50,000,000 × 4 = 200,000,000. Daily bytes = 200,000,000 × 2,000 = 400 GB. Hot share = 400 GB × 0.10 = 40 GB. With overhead, 40 × 1.3 = 52 GB. This fits in RAM on one large server, though you may add a replica.