How multilevel cache works
masterThe multilevel-cache-spring-boot-starter implements a two-layer caching strategy to optimize performance and reduce network I/O overhead:
- L1 Cache (Local): Uses Caffeine (JVM-level memory cache) for extremely high-speed access.
- L2 Cache (Distributed): Uses Redis to provide shared state across microservices.
Data Flow
- Read Operation: The system first checks the Caffeine L1 cache. If the key is missing (cache miss), it queries Redis (L2). If found in Redis, the value is automatically promoted to the Caffeine L1 cache for subsequent requests.
- Write/Evict Operation: To maintain data consistency across multiple application instances, all write operations (
put,evict,clear) use Redis Pub/Sub. When one instance modifies the cache, it publishes aCacheMessagevia Redis. All other instances listening to the topic receive this message and invalidate the corresponding key in their local Caffeine L1 cache.