SoFunction
Updated on 2025-04-11

Example of implementation of redis cache warm-up

1. Necessity of cache preheating

In a highly concurrency system, if the cache is empty when it is started, all requests will be directly hit to the database, which may cause the following problems:

  • High latency: Since the data is not in the cache, all requests need to access the backend database, which increases the response time.
  • Database pressure: In the early stage of system startup, due to the lack of data in the cache, a large number of requests directly hit the database, which will cause the database to have a sharp increase in the instantaneous load, which may lead to a degradation in database performance or even downtime.
  • Cache avalanche: A large number of requests to access the cache and database simultaneously may cause cache avalanche, that is, database overload problems caused by unavailability of the cache or low cache hit rate.

By caching preheating, hotspot data can be loaded into the cache in advance, thereby providing efficient services immediately upon system startup to avoid the above problems.

2. Implementation strategy of cache warm-up

In Java, cache warm-up can be implemented in many ways, and here are some common strategies:

  • Preloaded when the application starts
  • Timed task loading
  • Data access log analysis
  • Manually trigger cache preheating

1. Preloaded when the application starts

When the application starts, you can write code to load hotspot data into the cache. This method is suitable for scenarios where the data is not large and the data is fixed.

Java implementation example(Using Jedis):

import ;

import ;
import ;

public class CachePrewarmingOnStartup {

    private static final String REDIS_HOST = "localhost";
    private static final int REDIS_PORT = 6379;

    private Jedis jedis;

    public CachePrewarmingOnStartup() {
         = new Jedis(REDIS_HOST, REDIS_PORT);
    }

    public void prewarmCache() {
        // Simulate loading some hotspot data into the cache        Map<String, String> dataToCache = new HashMap<>();
        ("user:1001", "Alice");
        ("user:1002", "Bob");
        ("product:2001", "Laptop");
        ("product:2002", "Phone");

        for (<String, String> entry : ()) {
            ((), ());
            ("Preheat cache:" + () + " -> " + ());
        }
    }

    public static void main(String[] args) {
        CachePrewarmingOnStartup cachePrewarming = new CachePrewarmingOnStartup();
        ();
    }
}

In this example,prewarmCacheThe method is called when the application starts, loading some predefined hotspot data into the Redis cache.

2. Timing task loading

By performing cache warm-up logic regularly through timing tasks, you can ensure that the data in the cache is always up to date. This method is suitable for scenarios where caches need to be updated regularly.

Java implementation example(useScheduledExecutorService):

import ;

import ;
import ;
import ;
import ;
import ;

public class ScheduledCachePrewarming {

    private static final String REDIS_HOST = "localhost";
    private static final int REDIS_PORT = 6379;
    private Jedis jedis;

    public ScheduledCachePrewarming() {
         = new Jedis(REDIS_HOST, REDIS_PORT);
    }

    public void prewarmCache() {
        Map<String, String> dataToCache = new HashMap<>();
        ("user:1001", "Alice");
        ("user:1002", "Bob");
        ("product:2001", "Laptop");
        ("product:2002", "Phone");

        for (<String, String> entry : ()) {
            ((), ());
            ("Preheat cache:" + () + " -> " + ());
        }
    }

    public static void main(String[] args) {
        ScheduledCachePrewarming cachePrewarming = new ScheduledCachePrewarming();
        ScheduledExecutorService scheduler = (1);

        // Execute cache warm-up tasks every 5 minutes        (cachePrewarming::prewarmCache, 0, 5, );
    }
}

In this example, using Java'sScheduledExecutorServiceTo perform cache warm-up operations regularly. This ensures that the cached data is always up to date.

3. Data access log analysis

By analyzing historical data access logs, the most frequently visited hotspot data can be identified. These hotspot data are loaded into the cache periodically, thereby enabling effective cache warm-up.

Implementation steps

  • Collect logs: Collect the log of data access, record the key and number of accesses for each request.
  • Analysis log: Regularly analyze logs to identify the key with the highest access frequency.
  • Preheat cache: According to the analysis results, load the hotspot data into the cache.

This method requires a certain amount of log analysis capabilities, and large-scale logs can be processed and analyzed using big data technologies (such as Hadoop, Spark).

4. Manually trigger cache preheating

In some scenarios, the cache warm-up operation can be manually triggered by the operation and maintenance personnel or the application administrator. For example, when the system is updated or released a new version, a script can be manually executed to load some critical data into the cache.

Java implementation example(Trigger cache warm-up with command line input):

import ;

import ;
import ;
import ;

public class ManualCachePrewarming {

    private static final String REDIS_HOST = "localhost";
    private static final int REDIS_PORT = 6379;
    private Jedis jedis;

    public ManualCachePrewarming() {
         = new Jedis(REDIS_HOST, REDIS_PORT);
    }

    public void prewarmCache() {
        Map<String, String> dataToCache = new HashMap<>();
        ("user:1001", "Alice");
        ("user:1002", "Bob");
        ("product:2001", "Laptop");
        ("product:2002", "Phone");

        for (<String, String> entry : ()) {
            ((), ());
            ("Preheat cache:" + () + " -> " + ());
        }
    }

    public static void main(String[] args) {
        ManualCachePrewarming cachePrewarming = new ManualCachePrewarming();
        Scanner scanner = new Scanner();

        ("Enter 'prewarm' to manually trigger cache warm-up:");
        while (true) {
            String input = ();
            if ("prewarm".equalsIgnoreCase(input)) {
                ();
                ("Cache warm-up is complete!");
            } else {
                ("Invalid input, please enter 'prewarm' for cache preheating.");
            }
        }
    }
}

In this example, the user can enter via the command lineprewarmTo manually trigger the cache preheating operation.

3. Best practices for caching preheating

  • Choose the right data to warm up: When preheating cache, you should choose hot spot data with high access frequency and small amount of data to preheat to avoid excessive memory pressure by loading all data into the cache.

  • Set a reasonable expiration time: For cached preheated data, a reasonable expiration time should be set to prevent cache penetration problems caused by data expiration. The expiration time should be determined based on the update frequency of data and business requirements.

  • Monitor cache hit rate: After preheating the cache, the hit rate of the cache and the access frequency of the database should be monitored to ensure that the preheating effect meets the expected ones. If the hit rate is lower than expected, consider adjusting the preheated data set or frequency.

  • Combining automation and manual: Cache warm-up can be combined with automated scripts and manual operations. For periodic and regular data, automated scripts can be used for warm-up; for hot-spot data in special cases, warm-up can be manually triggered by the operation and maintenance personnel or application administrator.

  • Consider cache consistency: During the cache warm-up process, the consistency between the cache and the database should be ensured, especially in scenarios where data is updated frequently. A cached update or invalidation can be triggered through a database update event.

4. Summary

Cache preheating is an important means to improve system performance and user experience, especially in application scenarios with high concurrency and frequent access. Through cache preheating, it can effectively reduce cache misses, reduce database pressure, and improve system response speed. This article introduces a variety of cache warm-up strategies and their implementation methods in Java. Developers can choose appropriate strategies based on actual needs to optimize system performance. By rationally configuring and managing caches, the stability and availability of the system can be significantly improved.

This is the end of this article about the implementation example of redis cache warm-up. For more related redis cache warm-up content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!