Eager caching loads and retains cache contents before requests arrive, commonly during application initialization. It can make repeated lookups fast, but it shifts cost into startup and memory consumption. The central engineering question is not whether caching improves the hot path, but whether the cached dataset, access distribution, and object size justify loading everything in advance. section 7.4.1
Evaluate eager caching against these risks:
- Memory waste increases when many cached entries are rarely used; complex objects make the cost more serious. section 7.4.1
- Startup becomes slower because the cache is initialized when the application starts. section 7.4.1
- Limit performance changes to the intended hot path so production impact remains measurable and understandable. section 7.4.1
Benchmarking the trade-off
A JMH comparison measured cached and file-based word lookup. The cached implementation reported 0.008 ms/op versus 63,631.677 ms/op for the baseline, while cache initialization was measured separately rather than treated as free.
