AlgoViz

Cache-aside

easy 22 min

With cache-aside, also called lazy loading, the application looks in the cache first, reads the database on a miss and stores a copy for next time.

Where it shows up

The same pattern appears across everyday software.

  • Facebook memcache

    The 2013 paper describes memcache as a demand-filled look-aside cache. Web servers delete cached data after a database update, because deletes can be repeated safely, and leases protect against stale sets.

  • Profile pages

    A profile is read thousands of times between edits. The row is cached on first view and deleted when the person saves a change.

  • Product catalogue

    Popular products stay in the cache by themselves, rare ones expire. Only a small part of the catalogue uses memory.

  • Discussion posts

    A popular thread is read by many people within minutes. The post is cached when the first person opens it and cools down with its TTL.

  • Feature settings

    Servers read settings on every request. They are kept for a few seconds, so a change is visible everywhere soon without a query per request.

  • Amazon ElastiCache guidance

    The documentation describes lazy loading with the same strengths and weaknesses: only requested data is cached, a node failure is not fatal, a miss costs three trips, and data can be stale. It suggests adding a TTL.