# The cache that broke the camel's back

- Speaker: [Rami Stern](/wiki/speakers/rami-stern.md)
- Event: [Reversim Summit 2022](/wiki/years/2022.md)
- Video: https://www.youtube.com/watch?v=MKnc31NWIOs
- Transcript: [/transcripts/2022/12-MKnc31NWIOs.md](/transcripts/2022/12-MKnc31NWIOs.md)
- Language: Hebrew · Type: talk · Level: intermediate
- Topics: [Scalability](/wiki/topics/scalability.md), [Performance](/wiki/topics/performance.md), [Architecture](/wiki/topics/architecture.md), [Distributed Systems](/wiki/topics/distributed-systems.md)

> Written by an LLM from machine transcripts. It can contain errors. Check the transcript before you quote it.

## Summary

This talk explores the hidden complexities and architectural challenges of implementing caching systems in high-scale web applications. The speaker details how basic in-memory caches can introduce performance bottlenecks, memory pressure, and stale data issues, and shares iterative solutions such as TTL randomization, background updates, and push-based invalidations. Ultimately, the audience learns how to design robust caching layers—ranging from single-instance caches to distributed second-level caches and unified multi-team caching models—while balancing latency, throughput, and consistency.

## Key points

- [1:13](https://www.youtube.com/watch?v=MKnc31NWIOs&t=73s) Caching helps reduce latency and increases system throughput, but naive implementations often mask underlying problems rather than solving them.
- [3:39](https://www.youtube.com/watch?v=MKnc31NWIOs&t=219s) The fundamental trade-off in caching is balancing data freshness against read speed and system availability.
- [7:23](https://www.youtube.com/watch?v=MKnc31NWIOs&t=443s) Uniform TTLs across many objects can cause traffic spikes and database bottlenecks when keys expire simultaneously.
- [9:34](https://www.youtube.com/watch?v=MKnc31NWIOs&t=574s) Adding background updates and push-based invalidation mechanisms significantly reduces the frequency and cost of cache updates.
- [11:52](https://www.youtube.com/watch?v=MKnc31NWIOs&t=712s) Large in-memory caches can heavily strain the garbage collector and consume resources needed by the application itself.
- [14:52](https://www.youtube.com/watch?v=MKnc31NWIOs&t=892s) Decoupling cache management and loaders from the main business logic allows for asynchronous processing and deduplication of requests.
- [22:52](https://www.youtube.com/watch?v=MKnc31NWIOs&t=1372s) Introducing a distributed second-level cache layer protects the primary database from multi-instance thundering herd problems.
- [28:52](https://www.youtube.com/watch?v=MKnc31NWIOs&t=1732s) As organizations scale, consolidating multiple independent caches into a single unified cache structure reduces complexity and resource waste.

## Takeaways

- Always randomize time-to-live (TTL) values to prevent synchronized cache expiration spikes.
- Offload cache updates to background threads or push-based mechanisms to avoid blocking user requests.
- Consolidate multiple small, isolated caches into unified logical caches to simplify management and reduce memory overhead.
- Evaluate whether your scale truly justifies the complexity of a custom caching layer before building one.

## Questions this talk answers

- How do you prevent synchronized cache expiration spikes when dealing with millions of items sharing similar TTLs?
- What is the best way to handle garbage collection pressure caused by large in-memory caches in a JVM environment?
- When should an application move from a local in-memory cache to a distributed second-level cache?

## Technologies

JVM, Redis

## Tags

architecture, caching, distributed systems, jvm, latency, performance, scalability
