Solve Cache Penetration, Breakdown, and Avalanche in Spring Boot with Simple Annotations

This article revisits cache breakdown, penetration, and avalanche issues in high‑concurrency Java applications, critiques common Bloom‑filter solutions, and demonstrates practical Spring Boot techniques—including null‑value caching, sync annotations, and TTL jitter—to implement robust, production‑ready caching without complex infrastructure.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Solve Cache Penetration, Breakdown, and Avalanche in Spring Boot with Simple Annotations

Introduction

Today we revisit the cache breakdown problem. Existing articles are hard to apply because solutions like Bloom filter or Cuckoo filter are not directly usable in projects.

The solution can be implemented with just an annotation in a Spring Boot Java project; other stacks like Go or Python may only use the ideas as reference.

Current Defects

Most online solutions lack real Spring Boot integration and stay at the Java level. This article combines them with Spring Boot to present a feasible approach.

Real Solution

Assuming you use Spring Boot 2.x, add the Redis starter dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Configure application.yml for Redis connection:

spring:
  datasource: ...
  redis:
    database: ...
    host: ...
    port: ...

Use Spring Cache annotations. Example:

@Override
@Cacheable("menu")
public Menu findById(String id) {
    Menu menu = this.getById(id);
    if (menu != null) {
        System.out.println("menu.name = " + menu.getName());
    }
    return menu;
}

Cache Penetration

When a non‑existent key is queried, the cache misses and the database is hit. Simple fix: cache null values. Set spring.cache.redis.cache-null-values=true and use a short TTL (e.g., 15 seconds).

Cache Breakdown

When a cached value expires under high concurrency, many requests hit the database. The simplest fix is rate limiting; Spring Cache provides a sync attribute (available since Spring 4.3) to ensure only one thread queries the database while others wait. @Cacheable(cacheNames="menu", sync="true") For distributed environments, a custom CacheInterceptor with a distributed lock can be used (pseudo‑code shown).

flag := acquire distributed lock
if flag {
    query database and cache result
} else {
    sleep and retry
}

Cache Avalanche

Simultaneous expiration of many keys can overload the database. Mitigation: add random jitter to TTLs. Native @Cacheable does not support random TTL, so you need to extend RedisCache and override its put method.

Conclusion

By using Spring Cache annotations, configuring null‑value caching, enabling sync, and adding TTL jitter or distributed locking, cache penetration, breakdown, and avalanche can be effectively addressed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCacheSpringBootcache-avalanchecache-breakdowncache-penetration
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.