Backend Development 8 min read

Java Distributed Caching with Redis and Redisson

This article explains why caching is essential for Java distributed applications, introduces Redis as an in‑memory data store, and demonstrates how the Redisson framework provides distributed cache implementations—including RMapCache, Spring Cache integration, and JCache support—complete with code examples and configuration details.

Top Architect
Top Architect
Top Architect
Java Distributed Caching with Redis and Redisson

Caching is a crucial technique for accelerating Java distributed applications because every millisecond of latency affects user experience; studies show that more than half of mobile users abandon a site that does not load within three seconds.

Redis is a popular open‑source in‑memory data store that can serve as a database, cache, or message broker, offering much faster data access than RAM or disk. However, correctly using Redis as a distributed cache requires careful handling of cache invalidation across multiple machines.

Redisson is a Java framework that wraps Redis and provides a rich set of distributed objects such as maps, locks, synchronizers, and collections. It offers three main distributed‑cache implementations:

1. Redisson Distributed Cache

Redisson supplies RMapCache , RMapCache with local caching ( RLocalCachedMap ), and the generic RMap . Using RMapCache can speed up read‑heavy workloads by up to 45× when local caching is enabled.

RMapCache<String, SomeObject> map = redisson.getMapCache("anyMap");
map.put("key1", new SomeObject(), 10, TimeUnit.MINUTES, 10, TimeUnit.SECONDS);

When the cache is no longer needed, it can be destroyed with:

map.destroy();

2. Spring Cache Integration

Redisson integrates with Spring’s caching abstraction via RedissonSpringCacheManager and RedissonSpringLocalCachedCacheManager . The following configuration creates a Spring cache manager that uses a Redis cluster and defines a cache named testMap with a TTL of 24 minutes and a max idle time of 12 minutes.

@Configuration
@ComponentScan
@EnableCaching
public static class Application {
    @Bean(destroyMethod="shutdown")
    RedissonClient redisson() throws IOException {
        Config config = new Config();
        config.useClusterServers()
              .addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001");
        return Redisson.create(config);
    }

    @Bean
    CacheManager cacheManager(RedissonClient redissonClient) {
        Map<String, CacheConfig> config = new HashMap<>();
        // create "testMap" cache: ttl=24min, maxIdleTime=12min
        config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000));
        return new RedissonSpringCacheManager(redissonClient, config);
    }
}

Redisson’s Spring cache support also allows JSON or YAML based configuration.

3. JCache (JSR‑107) Support

Redisson provides a JCache implementation, enabling standard cache APIs. A basic example creates a cache named namedCache with default settings:

MutableConfiguration<String, String> config = new MutableConfiguration<>();
CacheManager manager = Caching.getCachingProvider().getCacheManager();
Cache<String, String> cache = manager.createCache("namedCache", config);

A more advanced setup can combine a custom Redisson configuration with JCache options:

MutableConfiguration<String, String> jcacheConfig = new MutableConfiguration<>();
Config redissonCfg = ...;
Configuration<String, String> config = RedissonConfiguration.fromConfig(redissonCfg, jcacheConfig);
CacheManager manager = Caching.getCachingProvider().getCacheManager();
Cache<String, String> cache = manager.createCache("namedCache", config);

Redisson’s JCache implementation passes all JCache TCK tests, and developers can verify it themselves.

In summary, Redisson simplifies the creation of distributed caches in Java by offering ready‑made map‑based caches, seamless Spring Cache integration, and standards‑compliant JCache support, allowing developers to improve latency, concurrency, and scalability of their distributed systems.

JavaRedisDistributed CacheRedissonSpring CacheJCache
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login 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.