Caching fundamentalsmedium3-5 years

A scraper walking through sequential product ids, many of which don't exist, is sending every one of those requests straight through the cache to the database, even though the service already uses cache-aside. Why doesn't cache-aside help here, and what closes the gap?

Cache-aside only ever caches values that exist — a request for a missing product misses the cache, queries the database, finds nothing there either, and caches nothing, so the next request for that same missing id repeats the exact same round trip to the database. This is called cache penetration, and it means a cache provides zero protection against repeated requests for ids that never exist. The fix is negative caching: store a short-lived sentinel value ("looked, not there") the first time a lookup comes back empty, so a repeated request for the same missing id is answered from the cache instead of hitting the database again — the lesson's experiment cut database queries for missing-id requests by two thirds this way.

The lesson behind it →
More on Caching fundamentals