Java Caching

The first answer to almost every latency problem, and the source of the second problem.

3 concepts · 9 interview questions

What this topic covers

Every concept in caching, and the questions each one gets asked as. Where a question links, it has a full write-up.

Where the cache sits

Cache-aside, read-through, write-through and write-behind differ in who populates the cache and when, which decides what happens on a miss and on a failure.

  • Explain cache-aside, and what happens on a miss.
  • Write-through or write-behind?
  • Where would you put a cache — client, CDN, service or database?

Invalidation and staleness

Knowing when a cached value stopped being true is the hard part. A TTL bounds the wrongness; explicit invalidation is exact and easy to miss.

  • How do you invalidate a cache?
  • How long should a TTL be?
  • What is a cache stampede, and how do you prevent one?

Eviction and sizing

A cache has a bound, so something must be discarded. The policy decides the hit rate, and the hit rate decides whether the cache is worth its complexity.

  • LRU or LFU?
  • What hit rate makes a cache worth having?
  • Local cache or distributed cache?

More in System design

See the whole reference map →