Compare cache-aside with write-through caching. When would you choose each?
Sketch your own answer first, then compare it with the one below.
A strong answer
In cache-aside, the application reads the cache first. On a miss it reads the database and fills the cache. On a write it updates the database and then invalidates or updates the cache key. The cache holds only what has been asked for.
In write-through, the write goes through the cache layer, which updates both the cache and the database as part of the same operation. Reads then find fresh data.
Choose cache-aside when reads dominate, the cache layer is a plain key-value store, and you can tolerate a short window of stale data. It is simple and resilient: if the cache is down, the application still works from the database.
Choose write-through when readers must not see stale values and writes are not too heavy, accepting that every write pays for the extra hop.
Both patterns have races. In cache-aside, a slow reader can fill the cache with an old value after a writer invalidated it. Production systems address this with leases or versions.
Sources
- Rajesh Nishtala, Hans Fugal, Steven Grimm, Marc Kwiatkowski, Herman Lee, Harry C. Li, Ryan McElroy, Mike Paleczny, Daniel Peek, Paul Saab, David Stafford, Tony Tung, Venkateshwaran Venkataramani (2013). Scaling Memcache at Facebook.