AWS › Amazon ElastiCache (Redis & Memcached)

What caching strategies are commonly implemented using ElastiCache, such as lazy loading and write through?

Intermediate

Answer

Lazy loading, also called cache aside, only loads data into the cache when it is actually requested and not found there, keeping the cache smaller but risking a slightly outdated cache if the underlying data changes elsewhere, while write through caching updates the cache immediately whenever the underlying data is written, keeping the cache consistently fresh at the cost of slightly slower write operations since two systems are updated together.
// Lazy loading pattern
value = cache.get(key)
if value is None:
    value = database.query(key)
    cache.set(key, value)
Real-world example A content platform uses lazy loading for article content, since articles are read far more often than they are updated, only populating the cache the first time a specific article is requested by any user.

Common follow-ups: What is the risk of using lazy loading without also setting a time to live?;How do you handle cache invalidation when the underlying data changes?