Skip to main content

Concepts

Look-aside caching pattern

Intuition

Think of a person who keeps recent notes on a notepad on the desk, instead of opening the full file in the cabinet every time they need a piece of information. They look at the notepad first; if the information isn't there, they go to the cabinet, pull out the document, copy the information onto the notepad, and carry on. The next time someone asks the same thing, they only look at the notepad — the cabinet is opened only when the notepad doesn't have the answer.

How it works

In the look-aside pattern (also called cache-aside), the application itself is responsible for orchestrating the cache: when it needs a piece of data, it first queries memcached; if the item is not there (cache miss), it fetches it from the database and then writes the result to the cache so that the next read is served straight from memory. The cache is never filled ahead of time — it is only populated as real usage happens, that is, it is a 'demand-filled' cache. For writes, the application writes to the database and then invalidates (removes) the corresponding entry in the cache, rather than trying to update the cached value directly; this invalidation is triggered by watching the write operations that reach the database themselves, identifying which cache keys correspond to the changed data and deleting them in a targeted way. The result is that memcached absorbs the overwhelming majority of reads, and the relational database is only queried on cache misses or on writes.

In practice

Facebook chose this format, instead of write-through, where the cache would be updated at the same moment as the write to the database, because it is simpler to operate at massive scale and avoids actively keeping the cache in sync on every write; the cost is accepting occasional misses and dealing with derived problems, such as the 'thundering herd' on popular invalidated items, which the paper solves with a lease mechanism. This pattern is today the de facto default whenever Redis or Memcached is placed in front of a relational database, and it is taught as a basic design option alongside write-through and write-behind.

Use it in a design

Practice the cases that rely on this concept in the System Design school.