Card 08/ 09
Practice: A Cache Policy for a News Site
Headlines change hourly, articles almost never, and the homepage every minute.
Invalidation, eviction and the stampede are practised together on one news site here. Write down a policy for each kind of value before opening the answer.
The brief. A news site caches three kinds of value in a shared Redis cache with 8 GB of memory.
- Articles: hundreds of thousands, written once and corrected rarely; a correction must show at once.
- Section headlines: a list per section, updated about hourly by editors; a few minutes late is acceptable.
- The homepage: built every minute from the headlines, read 5,000 times a second.
- Which invalidation strategy suits each?
- Which articles should the cache hold, and what happens when it is full?
- What could go wrong when the homepage entry expires, and how do you prevent it?
Check your policy
- Articles: delete on write, so a correction shows on the next read, with a long TTL as a backstop. Headlines: a TTL of a few minutes is enough, since a short delay is allowed. The homepage: a TTL of about a minute, matching how often it is rebuilt.
- Hundreds of thousands of articles will not all fit, and most are rarely read after their first days. LRU eviction with a memory limit keeps the articles being read now and drops the old ones.
- A stampede. At 5,000 reads a second, every request in the rebuild window would rebuild the homepage at once. Let one request rebuild while the others wait or are served the previous version.
← → move through sections and cards