ElasticCache Strategy
Caching strategies for ElasticCache
Caching design pattern
- Lazy Loading / Cache-aside / Lazy population (all the same thing)
- Only write to cache if cache does not contain the information
- Pros: Only requested data is cached.
- Cons: Cache miss resulted into 3 round trips so extra latency. Data can be stale as well in the database if cache miss keeps happening
- Write through
- Add or update the cache when database is updated
- Pros:
- Data is never stale, reads are quick
- Write penalty is only 2 calls comparing to lazy loading
- Cons:
- Data is missing until you update or add new data into database
- We can also implement lazyloading as well to counter this
- Cache churn: Add a lot but how much of the cache is actually needed
- Data is missing until you update or add new data into database
- Cache Evictions and Time-to-live (TTL)
- Remove the data out of your cache to save space
- Item is evicted because memory is full and it's not recently used (LRU)
- If too many evictions happen due to memory then increase cache size
Note
- Lazy-loading works really well and easy to implement in all situation. Especially on the read side
- Write-through is usually combined with Lazy Loading to maximise the benefit
- TTL is not bad but becareful when you use write through (because you keep writing to the cache). You need to tune to a good parameter
- Only cache the data that makes sense (user profiles, blogs, etc ...)