Cache-aside
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
implement the most common pattern and know how it fails.
Planned topics
- The application owns the logic: read the cache; on a miss read the database, then fill the cache.
- On write: update the database first, then delete (not update) the cache entry.
- Failure: if the cache is down, reads fall through to the database (slower, still correct). If the delete fails, stale data stays until TTL.
- Simple and robust; the cost is repeated code and a miss penalty on first read.
Draft outline details
Demonstration
ProductDto get(long id) {
var hit = cache.get(key(id));
if (hit != null) return hit;
var p = repo.load(id);
cache.put(key(id), p, ttl);
return p;
}
void update(Product p) { repo.save(p); cache.delete(key(p.id())); }Check your understanding
On a write, which first: database or cache delete?
Show answer
Database, then delete the cache entry.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.