Spring Boot Starter

Integrates Redisson with Spring Boot library. Depends on Spring Data Redis module.

Supports Spring Boot 1.3.x - 3.4.x

Usage

1. Add redisson-spring-boot-starter dependency into your project:

Maven

  1. <dependency>
  2. <groupId>org.redisson</groupId>
  3. <artifactId>redisson-spring-boot-starter</artifactId>
  4. <version>xVERSIONx</version>
  5. </dependency>

Gradle

  1. compile 'org.redisson:redisson-spring-boot-starter:xVERSIONx'

redisson-spring-boot-starter depends on redisson-spring-data module compatible with the latest version of Spring Boot. Downgrade redisson-spring-data module if necessary to support previous Spring Boot versions:

redisson-spring-data
module name
Spring Boot
version
redisson-spring-data-16 1.3.y
redisson-spring-data-17 1.4.y
redisson-spring-data-18 1.5.y
redisson-spring-data-2x 2.x.y
redisson-spring-data-3x 3.x.y

For Gradle, you can downgrade to redisson-spring-data-27 this way:

  1. implementation ("org.redisson:redisson-spring-boot-starter:xVERSIONx") {
  2. exclude group: 'org.redisson', module: 'redisson-spring-data-34'
  3. }
  4. implementation "org.redisson:redisson-spring-data-27:xVERSIONx"

2. Add settings into application.settings file

Using common Spring Boot 3.x+ settings:

  1. spring:
  2. data:
  3. redis:
  4. database:
  5. host:
  6. port:
  7. password:
  8. ssl:
  9. timeout:
  10. connectTimeout:
  11. clientName:
  12. cluster:
  13. nodes:
  14. sentinel:
  15. master:
  16. nodes:

Using common Spring Boot up to 2.7.x settings:

  1. spring:
  2. redis:
  3. database:
  4. host:
  5. port:
  6. password:
  7. ssl:
  8. timeout:
  9. connectTimeout:
  10. clientName:
  11. cluster:
  12. nodes:
  13. sentinel:
  14. master:
  15. nodes:

Using Redisson config file: (single mode, replicated mode, cluster mode, sentinel mode, proxy mode, multi cluster mode, multi sentinel mode)

  1. spring:
  2. redis:
  3. redisson:
  4. file: classpath:redisson.yaml

Using Redisson settings: (single mode, replicated mode, cluster mode, sentinel mode, proxy mode, multi cluster mode, multi sentinel mode)

  1. spring:
  2. redis:
  3. redisson:
  4. config: |
  5. clusterServersConfig:
  6. idleConnectionTimeout: 10000
  7. connectTimeout: 10000
  8. timeout: 3000
  9. retryAttempts: 3
  10. retryInterval: 1500
  11. failedSlaveReconnectionInterval: 3000
  12. failedSlaveCheckInterval: 60000
  13. password: null
  14. subscriptionsPerConnection: 5
  15. clientName: null
  16. loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
  17. subscriptionConnectionMinimumIdleSize: 1
  18. subscriptionConnectionPoolSize: 50
  19. slaveConnectionMinimumIdleSize: 24
  20. slaveConnectionPoolSize: 64
  21. masterConnectionMinimumIdleSize: 24
  22. masterConnectionPoolSize: 64
  23. readMode: "SLAVE"
  24. subscriptionMode: "SLAVE"
  25. nodeAddresses:
  26. - "redis://127.0.0.1:7004"
  27. - "redis://127.0.0.1:7001"
  28. - "redis://127.0.0.1:7000"
  29. scanInterval: 1000
  30. pingConnectionInterval: 0
  31. keepAlive: false
  32. tcpNoDelay: false
  33. threads: 16
  34. nettyThreads: 32
  35. codec: !<org.redisson.codec.Kryo5Codec> {}
  36. transportMode: "NIO"

3. Available Spring Beans:

  • RedissonClient
  • RedissonRxClient
  • RedissonReactiveClient
  • RedisTemplate
  • ReactiveRedisTemplate
  • ReactiveRedisOperations

Upgrade to Redisson PRO with advanced features.

FAQ

Q: How to replace Netty version brought by Spring Boot?

You need to define netty version in properties section of your Maven project.

  1. <properties>
  2. <netty.version>4.1.107.Final</netty.version>
  3. </properties>

Q: How to disable Redisson?

You may not have Redis or Valkey in some environments. In this case Redisson can be disabled:

  • Using Annotations
    Spring Boot 2.7+
  1. @SpringBootApplication
  2. @EnableAutoConfiguration(exclude = {
  3. RedissonAutoConfigurationV2.class})
  4. public class Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MyApplication.class, args);
  7. }
  8. }

Spring Boot up to 2.6

  1. @SpringBootApplication
  2. @EnableAutoConfiguration(exclude = {
  3. RedissonAutoConfiguration.class})
  4. public class Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MyApplication.class, args);
  7. }
  8. }
  • Using application.yml file
    Spring Boot 2.7+
  1. spring:
  2. autoconfigure:
  3. exclude:
  4. - org.redisson.spring.starter.RedissonAutoConfigurationV2

Spring Boot up to 2.6

  1. spring:
  2. autoconfigure:
  3. exclude:
  4. - org.redisson.spring.starter.RedissonAutoConfiguration

Spring Boot Starter

Integrates Redisson with Spring Boot library. Depends on Spring Data Redis module.

Supports Spring Boot 1.3.x - 3.4.x

Usage

1. Add redisson-spring-boot-starter dependency into your project:

Maven

  1. <dependency>
  2. <groupId>org.redisson</groupId>
  3. <artifactId>redisson-spring-boot-starter</artifactId>
  4. <version>xVERSIONx</version>
  5. </dependency>

Gradle

  1. compile 'org.redisson:redisson-spring-boot-starter:xVERSIONx'

redisson-spring-boot-starter depends on redisson-spring-data module compatible with the latest version of Spring Boot. Downgrade redisson-spring-data module if necessary to support previous Spring Boot versions:

redisson-spring-data
module name
Spring Boot
version
redisson-spring-data-16 1.3.y
redisson-spring-data-17 1.4.y
redisson-spring-data-18 1.5.y
redisson-spring-data-2x 2.x.y
redisson-spring-data-3x 3.x.y

For Gradle, you can downgrade to redisson-spring-data-27 this way:

  1. implementation ("org.redisson:redisson-spring-boot-starter:xVERSIONx") {
  2. exclude group: 'org.redisson', module: 'redisson-spring-data-34'
  3. }
  4. implementation "org.redisson:redisson-spring-data-27:xVERSIONx"

2. Add settings into application.settings file

Using common Spring Boot 3.x+ settings:

  1. spring:
  2. data:
  3. redis:
  4. database:
  5. host:
  6. port:
  7. password:
  8. ssl:
  9. timeout:
  10. connectTimeout:
  11. clientName:
  12. cluster:
  13. nodes:
  14. sentinel:
  15. master:
  16. nodes:

Using common Spring Boot up to 2.7.x settings:

  1. spring:
  2. redis:
  3. database:
  4. host:
  5. port:
  6. password:
  7. ssl:
  8. timeout:
  9. connectTimeout:
  10. clientName:
  11. cluster:
  12. nodes:
  13. sentinel:
  14. master:
  15. nodes:

Using Redisson config file: (single mode, replicated mode, cluster mode, sentinel mode, proxy mode, multi cluster mode, multi sentinel mode)

  1. spring:
  2. redis:
  3. redisson:
  4. file: classpath:redisson.yaml

Using Redisson settings: (single mode, replicated mode, cluster mode, sentinel mode, proxy mode, multi cluster mode, multi sentinel mode)

  1. spring:
  2. redis:
  3. redisson:
  4. config: |
  5. clusterServersConfig:
  6. idleConnectionTimeout: 10000
  7. connectTimeout: 10000
  8. timeout: 3000
  9. retryAttempts: 3
  10. retryInterval: 1500
  11. failedSlaveReconnectionInterval: 3000
  12. failedSlaveCheckInterval: 60000
  13. password: null
  14. subscriptionsPerConnection: 5
  15. clientName: null
  16. loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
  17. subscriptionConnectionMinimumIdleSize: 1
  18. subscriptionConnectionPoolSize: 50
  19. slaveConnectionMinimumIdleSize: 24
  20. slaveConnectionPoolSize: 64
  21. masterConnectionMinimumIdleSize: 24
  22. masterConnectionPoolSize: 64
  23. readMode: "SLAVE"
  24. subscriptionMode: "SLAVE"
  25. nodeAddresses:
  26. - "redis://127.0.0.1:7004"
  27. - "redis://127.0.0.1:7001"
  28. - "redis://127.0.0.1:7000"
  29. scanInterval: 1000
  30. pingConnectionInterval: 0
  31. keepAlive: false
  32. tcpNoDelay: false
  33. threads: 16
  34. nettyThreads: 32
  35. codec: !<org.redisson.codec.Kryo5Codec> {}
  36. transportMode: "NIO"

3. Available Spring Beans:

  • RedissonClient
  • RedissonRxClient
  • RedissonReactiveClient
  • RedisTemplate
  • ReactiveRedisTemplate
  • ReactiveRedisOperations

Upgrade to Redisson PRO with advanced features.

FAQ

Q: How to replace Netty version brought by Spring Boot?

You need to define netty version in properties section of your Maven project.

  1. <properties>
  2. <netty.version>4.1.107.Final</netty.version>
  3. </properties>

Q: How to disable Redisson?

You may not have Redis or Valkey in some environments. In this case Redisson can be disabled:

  • Using Annotations Spring Boot 2.7+
  1. @SpringBootApplication
  2. @EnableAutoConfiguration(exclude = {
  3. RedissonAutoConfigurationV2.class})
  4. public class Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MyApplication.class, args);
  7. }
  8. }

Spring Boot up to 2.6

  1. @SpringBootApplication
  2. @EnableAutoConfiguration(exclude = {
  3. RedissonAutoConfiguration.class})
  4. public class Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MyApplication.class, args);
  7. }
  8. }
  • Using application.yml file Spring Boot 2.7+
  1. spring:
  2. autoconfigure:
  3. exclude:
  4. - org.redisson.spring.starter.RedissonAutoConfigurationV2

Spring Boot up to 2.6

  1. spring:
  2. autoconfigure:
  3. exclude:
  4. - org.redisson.spring.starter.RedissonAutoConfiguration

Spring Cache

Redisson provides various Spring Cache implementations. Each Cache instance has two important parameters: ttl and maxIdleTime. Data is stored infinitely if these settings are not defined or equal to 0.

Config example:

  1. @Configuration
  2. @ComponentScan
  3. @EnableCaching
  4. public static class Application {
  5. @Bean(destroyMethod="shutdown")
  6. RedissonClient redisson() throws IOException {
  7. Config config = new Config();
  8. config.useClusterServers()
  9. .addNodeAddress("redis://127.0.0.1:7004", "redis://127.0.0.1:7001");
  10. return Redisson.create(config);
  11. }
  12. @Bean
  13. CacheManager cacheManager(RedissonClient redissonClient) {
  14. Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
  15. // create "testMap" cache with ttl = 24 minutes and maxIdleTime = 12 minutes
  16. config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000));
  17. return new RedissonSpringCacheManager(redissonClient, config);
  18. }
  19. }

Cache configuration can be read from YAML configuration files:

  1. @Configuration
  2. @ComponentScan
  3. @EnableCaching
  4. public static class Application {
  5. @Bean(destroyMethod="shutdown")
  6. RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  7. Config config = Config.fromYAML(configFile.getInputStream());
  8. return Redisson.create(config);
  9. }
  10. @Bean
  11. CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
  12. return new RedissonSpringCacheManager(redissonClient, "classpath:/cache-config.yaml");
  13. }
  14. }

Eviction, local cache and data partitioning

Redisson provides various Spring Cache managers with two important features:

local cache - so called near cache used to speed up read operations and avoid network roundtrips. It caches Map entries on Redisson side and executes read operations up to 45x faster in comparison with common implementation. Local cache instances with the same name connected to the same pub/sub channel. This channel is used for exchanging of update/invalidate events between all instances. Local cache store doesn’t use hashCode()/equals() methods of key object, instead it uses hash of serialized state.

data partitioning - although Map object is cluster compatible its content isn’t scaled/partitioned across multiple Redis or Valkey master nodes in cluster. Data partitioning allows to scale available memory, read/write operations and entry eviction process for individual Map instance in cluster.

Scripted eviction

Allows to define time to live or max idle time parameters per map entry. Eviction is done on Redisson side through a custom scheduled task which removes expired entries using Lua script. Eviction task is started once per unique object name at the moment of getting Map instance. If instance isn’t used and has expired entries it should be get again to start the eviction process. This leads to extra Redis or Valkey calls and eviction task per unique map object name.

Entries are cleaned time to time by org.redisson.eviction.EvictionScheduler. By default, it removes 100 expired entries at a time. This can be changed through cleanUpKeysAmount setting. Task launch time tuned automatically and depends on expired entries amount deleted in previous time and varies between 5 second to 30 minutes by default. This time interval can be changed through minCleanUpDelay and maxCleanUpDelay. For example, if clean task deletes 100 entries each time it will be executed every 5 seconds (minimum execution delay). But if current expired entries amount is lower than previous one then execution delay will be increased by 1.5 times and decreased otherwise.

Available implementations:

Class name Local
cache
Data
partitioning
Ultra-fast
read/write
RedissonSpringCacheManager
open-source version
RedissonSpringCacheManager
Redisson PRO version
✔️
RedissonSpringLocalCachedCacheManager
available only in Redisson PRO
✔️ ✔️
RedissonClusteredSpringCacheManager
available only in Redisson PRO
✔️ ✔️
RedissonClusteredSpringLocalCachedCacheManager
available only in Redisson PRO
✔️ ✔️ ✔️

Advanced eviction

Allows to define time to live parameter per map entry. Doesn’t use an entry eviction task, entries are cleaned on Redis or Valkey side.

Available implementations:

Class name Local
cache
Data
partitioning
Ultra-fast
read/write
RedissonSpringCacheV2Manager
available only in Redisson PRO
✔️ ✔️
RedissonSpringLocalCachedCacheV2Manager
available only in Redisson PRO
✔️ ✔️ ✔️

Native eviction

Allows to define time to live parameter per map entry. Doesn’t use an entry eviction task, entries are cleaned on Redis side. Requires Redis 7.4+.

Available implementations:

Class name Local
cache
Data
partitioning
Ultra-fast
read/write
RedissonSpringCacheNativeManager
open-source version
RedissonSpringCacheNativeManager
Redisson PRO version
✔️
RedissonSpringLocalCachedCacheNativeManager
available only in Redisson PRO
✔️ ✔️
RedissonClusteredSpringCacheNativeManager
available only in Redisson PRO
✔️ ✔️

Local cache

Follow options object can be supplied during local cached managers initialization:

  1. LocalCachedMapOptions options = LocalCachedMapOptions.defaults()
  2. // Defines whether to store a cache miss into the local cache.
  3. // Default value is false.
  4. .storeCacheMiss(false);
  5. // Defines store mode of cache data.
  6. // Follow options are available:
  7. // LOCALCACHE - store data in local cache only.
  8. // LOCALCACHE_REDIS - store data in both Redis or Valkey and local cache.
  9. .storeMode(StoreMode.LOCALCACHE_REDIS)
  10. // Defines Cache provider used as local cache store.
  11. // Follow options are available:
  12. // REDISSON - uses Redisson own implementation
  13. // CAFFEINE - uses Caffeine implementation
  14. .cacheProvider(CacheProvider.REDISSON)
  15. // Defines local cache eviction policy.
  16. // Follow options are available:
  17. // LFU - Counts how often an item was requested. Those that are used least often are discarded first.
  18. // LRU - Discards the least recently used items first
  19. // SOFT - Uses weak references, entries are removed by GC
  20. // WEAK - Uses soft references, entries are removed by GC
  21. // NONE - No eviction
  22. .evictionPolicy(EvictionPolicy.NONE)
  23. // If cache size is 0 then local cache is unbounded.
  24. .cacheSize(1000)
  25. // Used to load missed updates during any connection failures to Redis.
  26. // Since, local cache updates can't be get in absence of connection to Redis.
  27. // Follow reconnection strategies are available:
  28. // CLEAR - Clear local cache if map instance has been disconnected for a while.
  29. // LOAD - Store invalidated entry hash in invalidation log for 10 minutes
  30. // Cache keys for stored invalidated entry hashes will be removed
  31. // if LocalCachedMap instance has been disconnected less than 10 minutes
  32. // or whole cache will be cleaned otherwise.
  33. // NONE - Default. No reconnection handling
  34. .reconnectionStrategy(ReconnectionStrategy.NONE)
  35. // Used to synchronize local cache changes.
  36. // Follow sync strategies are available:
  37. // INVALIDATE - Default. Invalidate cache entry across all LocalCachedMap instances on map entry change
  38. // UPDATE - Insert/update cache entry across all LocalCachedMap instances on map entry change
  39. // NONE - No synchronizations on map changes
  40. .syncStrategy(SyncStrategy.INVALIDATE)
  41. // time to live for each entry in local cache
  42. .timeToLive(Duration.ofSeconds(10))
  43. // max idle time for each entry in local cache
  44. .maxIdle(Duration.ofSeconds(10));
  45. // Defines how to listen expired event sent by Redis or Valkey upon this instance deletion
  46. //
  47. // Follow expiration policies are available:
  48. // DONT_SUBSCRIBE - Don't subscribe on expire event
  49. // SUBSCRIBE_WITH_KEYEVENT_PATTERN - Subscribe on expire event using `__keyevent@*:expired` pattern
  50. // SUBSCRIBE_WITH_KEYSPACE_CHANNEL - Subscribe on expire event using `__keyspace@N__:name` channel
  51. .expirationEventPolicy(ExpirationEventPolicy.SUBSCRIBE_WITH_KEYEVENT_PATTERN)

Each Spring Cache instance has two important parameters: ttl and maxIdleTime and stores data infinitely if they are not defined or equal to 0.

Complete config example:

  1. @Configuration
  2. @ComponentScan
  3. @EnableCaching
  4. public static class Application {
  5. @Bean(destroyMethod="shutdown")
  6. RedissonClient redisson() throws IOException {
  7. Config config = new Config();
  8. config.useClusterServers()
  9. .addNodeAddress("redis://127.0.0.1:7004", "redis://127.0.0.1:7001");
  10. return Redisson.create(config);
  11. }
  12. @Bean
  13. CacheManager cacheManager(RedissonClient redissonClient) {
  14. Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
  15. // define local cache settings for "testMap" cache.
  16. // ttl = 48 minutes and maxIdleTime = 24 minutes for local cache entries
  17. LocalCachedMapOptions options = LocalCachedMapOptions.defaults()
  18. .evictionPolicy(EvictionPolicy.LFU)
  19. .timeToLive(48, TimeUnit.MINUTES)
  20. .maxIdle(24, TimeUnit.MINUTES)
  21. .cacheSize(1000);
  22. // create "testMap" Redis or Valkey cache with ttl = 24 minutes and maxIdleTime = 12 minutes
  23. LocalCachedCacheConfig cfg = new LocalCachedCacheConfig(24*60*1000, 12*60*1000, options);
  24. // Max size of map stored in Redis
  25. cfg.setMaxSize(2000);
  26. config.put("testMap", cfg);
  27. return new RedissonSpringLocalCachedCacheManager(redissonClient, config);
  28. // or
  29. return new RedissonSpringLocalCachedCacheNativeManager(redissonClient, config);
  30. // or
  31. return new RedissonSpringLocalCachedCacheV2Manager(redissonClient, config);
  32. // or
  33. return new RedissonClusteredSpringLocalCachedCacheManager(redissonClient, config);
  34. }
  35. }

Cache configuration could be read from YAML configuration files:

  1. @Configuration
  2. @ComponentScan
  3. @EnableCaching
  4. public static class Application {
  5. @Bean(destroyMethod="shutdown")
  6. RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  7. Config config = Config.fromYAML(configFile.getInputStream());
  8. return Redisson.create(config);
  9. }
  10. @Bean
  11. CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
  12. return new RedissonSpringLocalCachedCacheManager(redissonClient, "classpath:/cache-config.yaml");
  13. }
  14. }

YAML config format

Below is the configuration of Spring Cache with name testMap in YAML format:

  1. ---
  2. testMap:
  3. ttl: 1440000
  4. maxIdleTime: 720000
  5. localCacheOptions:
  6. invalidationPolicy: "ON_CHANGE"
  7. evictionPolicy: "NONE"
  8. cacheSize: 0
  9. timeToLiveInMillis: 0
  10. maxIdleInMillis: 0

localCacheOptions settings are available for RedissonSpringLocalCachedCacheManager, RedissonClusteredSpringLocalCachedCacheManager, RedissonSpringLocalCachedCacheV2Manager and RedissonSpringLocalCachedCacheNativeManager classes only.

Spring Session

Please note that Redis or Valkey notify-keyspace-events setting should contain Exg letters to make Spring Session integration work.

Ensure you have Spring Session library in your classpath, add it if necessary:

Maven

  1. <dependency>
  2. <groupId>org.springframework.session</groupId>
  3. <artifactId>spring-session-core</artifactId>
  4. <version>3.4.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.redisson</groupId>
  8. <artifactId>redisson-spring-data-34</artifactId>
  9. <version>xVERSIONx</version>
  10. </dependency>

Gradle

  1. compile 'org.springframework.session:spring-session-core:3.4.1'
  2. compile 'org.redisson:redisson-spring-data-34:xVERSIONx'

Spring Http Session configuration

Add configuration class which extends AbstractHttpSessionApplicationInitializer class:

  1. @Configuration
  2. @EnableRedisHttpSession
  3. public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
  4. @Bean
  5. public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
  6. return new RedissonConnectionFactory(redisson);
  7. }
  8. @Bean(destroyMethod = "shutdown")
  9. public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  10. Config config = Config.fromYAML(configFile.getInputStream());
  11. return Redisson.create(config);
  12. }
  13. }

Spring WebFlux’s Session configuration

Add configuration class which extends AbstractReactiveWebInitializer class:

  1. @Configuration
  2. @EnableRedisWebSession
  3. public class SessionConfig extends AbstractReactiveWebInitializer {
  4. @Bean
  5. public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
  6. return new RedissonConnectionFactory(redisson);
  7. }
  8. @Bean(destroyMethod = "shutdown")
  9. public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  10. Config config = Config.fromYAML(configFile.getInputStream());
  11. return Redisson.create(config);
  12. }
  13. }

Spring Boot configuration

  1. Add Spring Session Data Redis library in classpath: Maven:
  1. <dependency>
  2. <groupId>org.springframework.session</groupId>
  3. <artifactId>spring-session-data-redis</artifactId>
  4. <version>3.2.1</version>
  5. </dependency>

Gradle:

  1. compile 'org.springframework.session:spring-session-data-redis:3.4.1'
  1. Add Redisson Spring Data Redis library in classpath: Maven:
  1. <dependency>
  2. <groupId>org.redisson</groupId>
  3. <artifactId>redisson-spring-data-34</artifactId>
  4. <version>xVERSIONx</version>
  5. </dependency>

Gradle:

  1. compile 'org.redisson:redisson-spring-data-34:xVERSIONx'
  1. Define follow properties in spring-boot settings
    1. spring.session.store-type=redis
    2. spring.redis.redisson.file=classpath:redisson.yaml
    3. spring.session.timeout.seconds=900

Upgrade to Redisson PRO with advanced features.

Spring Transaction Manager

Redisson provides implementation of both org.springframework.transaction.PlatformTransactionManager and org.springframework.transaction.ReactiveTransactionManager interfaces to participant in Spring transactions. See also Transactions section.

Spring Transaction Management

  1. @Configuration
  2. @EnableTransactionManagement
  3. public class RedissonTransactionContextConfig {
  4. @Bean
  5. public TransactionalBean transactionBean() {
  6. return new TransactionalBean();
  7. }
  8. @Bean
  9. public RedissonTransactionManager transactionManager(RedissonClient redisson) {
  10. return new RedissonTransactionManager(redisson);
  11. }
  12. @Bean(destroyMethod="shutdown")
  13. public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  14. Config config = Config.fromYAML(configFile.getInputStream());
  15. return Redisson.create(config);
  16. }
  17. }
  18. public class TransactionalBean {
  19. @Autowired
  20. private RedissonTransactionManager transactionManager;
  21. @Transactional
  22. public void commitData() {
  23. RTransaction transaction = transactionManager.getCurrentTransaction();
  24. RMap<String, String> map = transaction.getMap("test1");
  25. map.put("1", "2");
  26. }
  27. }

Reactive Spring Transaction Management

  1. @Configuration
  2. @EnableTransactionManagement
  3. public class RedissonReactiveTransactionContextConfig {
  4. @Bean
  5. public TransactionalBean transactionBean() {
  6. return new TransactionalBean();
  7. }
  8. @Bean
  9. public ReactiveRedissonTransactionManager transactionManager(RedissonReactiveClient redisson) {
  10. return new ReactiveRedissonTransactionManager(redisson);
  11. }
  12. @Bean(destroyMethod="shutdown")
  13. public RedissonReactiveClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  14. Config config = Config.fromYAML(configFile.getInputStream());
  15. return Redisson.createReactive(config);
  16. }
  17. }
  18. public class TransactionalBean {
  19. @Autowired
  20. private ReactiveRedissonTransactionManager transactionManager;
  21. @Transactional
  22. public Mono<Void> commitData() {
  23. Mono<RTransactionReactive> transaction = transactionManager.getCurrentTransaction();
  24. return transaction.flatMap(t -> {
  25. RMapReactive<String, String> map = t.getMap("test1");
  26. return map.put("1", "2");
  27. }).then();
  28. }
  29. }

Spring Cloud Stream

This feature is available only in Redisson PRO edition.

Redisson implements Spring Cloud Stream integration based on the reliable Stream structure for message delivery. To use Redis or Valkey binder with Redisson you need to add Spring Cloud Stream Binder library in classpath:

Maven:

  1. <dependency>
  2. <groupId>pro.redisson</groupId>
  3. <artifactId>spring-cloud-stream-binder-redisson</artifactId>
  4. <version>xVERSIONx</version>
  5. </dependency>

Gradle:

  1. compile 'pro.redisson:spring-cloud-stream-binder-redisson:xVERSIONx'

Compatible with Spring versions below.

Spring Cloud Stream Spring Cloud Spring Boot
4.2.x 2024.0.x 3.4.x
4.1.x 2023.0.x 3.0.x - 3.3.x
4.0.x 2022.0.x 3.0.x - 3.3.x
3.2.x 2021.0.x 2.6.x, 2.7.x (Starting with 2021.0.3 of Spring Cloud)
3.1.x 2020.0.x 2.4.x, 2.5.x (Starting with 2020.0.3 of Spring Cloud)

Receiving messages

Register the input binder (an event sink) for receiving messages as follows:

  1. @Bean
  2. public Consumer<MyObject> receiveMessage() {
  3. return obj -> {
  4. // consume received object ...
  5. };
  6. }

Define channel id in the configuration file application.properties. Example for receiveMessage bean defined above connected to my-channel channel:

  1. spring.cloud.stream.bindings.receiveMessage-in-0.destination=my-channel

Publishing messages

Register the output binder (an event source) for publishing messages as follows:

  1. @Bean
  2. public Supplier<MyObject> feedSupplier() {
  3. return () -> {
  4. // ...
  5. return new MyObject();
  6. };
  7. }

Define channel id in the configuration file application.properties. Example for feedSupplier bean defined above connected to my-channel channel:

  1. spring.cloud.stream.bindings.feedSupplier-out-0.destination=my-channel
  2. spring.cloud.stream.bindings.feedSupplier-out-0.producer.useNativeEncoding=true

Spring Data Redis

Integrates Redisson with Spring Data Redis library. Implements Spring Data’s RedisConnectionFactory and ReactiveRedisConnectionFactory interfaces and allows to interact with Redis or Valkey through RedisTemplate, ReactiveRedisTemplate or ReactiveRedisOperations object.

Usage

  1. Add redisson-spring-data dependency into your project: Maven:
  1. <dependency>
  2. <groupId>org.redisson</groupId>
  3. <!-- for Spring Data Redis v.1.6.x -->
  4. <artifactId>redisson-spring-data-16</artifactId>
  5. <!-- for Spring Data Redis v.1.7.x -->
  6. <artifactId>redisson-spring-data-17</artifactId>
  7. <!-- for Spring Data Redis v.1.8.x -->
  8. <artifactId>redisson-spring-data-18</artifactId>
  9. <!-- for Spring Data Redis v.2.0.x -->
  10. <artifactId>redisson-spring-data-20</artifactId>
  11. <!-- for Spring Data Redis v.2.1.x -->
  12. <artifactId>redisson-spring-data-21</artifactId>
  13. <!-- for Spring Data Redis v.2.2.x -->
  14. <artifactId>redisson-spring-data-22</artifactId>
  15. <!-- for Spring Data Redis v.2.3.x -->
  16. <artifactId>redisson-spring-data-23</artifactId>
  17. <!-- for Spring Data Redis v.2.4.x -->
  18. <artifactId>redisson-spring-data-24</artifactId>
  19. <!-- for Spring Data Redis v.2.5.x -->
  20. <artifactId>redisson-spring-data-25</artifactId>
  21. <!-- for Spring Data Redis v.2.6.x -->
  22. <artifactId>redisson-spring-data-26</artifactId>
  23. <!-- for Spring Data Redis v.2.7.x -->
  24. <artifactId>redisson-spring-data-27</artifactId>
  25. <!-- for Spring Data Redis v.3.0.x -->
  26. <artifactId>redisson-spring-data-30</artifactId>
  27. <!-- for Spring Data Redis v.3.1.x -->
  28. <artifactId>redisson-spring-data-31</artifactId>
  29. <!-- for Spring Data Redis v.3.2.x -->
  30. <artifactId>redisson-spring-data-32</artifactId>
  31. <!-- for Spring Data Redis v.3.3.x -->
  32. <artifactId>redisson-spring-data-33</artifactId>
  33. <!-- for Spring Data Redis v.3.4.x -->
  34. <artifactId>redisson-spring-data-34</artifactId>
  35. <version>xVERSIONx</version>
  36. </dependency>

Gradle:

  1. // for Spring Data Redis v.1.6.x
  2. compile 'org.redisson:redisson-spring-data-16:xVERSIONx'
  3. // for Spring Data Redis v.1.7.x
  4. compile 'org.redisson:redisson-spring-data-17:xVERSIONx'
  5. // for Spring Data Redis v.1.8.x
  6. compile 'org.redisson:redisson-spring-data-18:xVERSIONx'
  7. // for Spring Data Redis v.2.0.x
  8. compile 'org.redisson:redisson-spring-data-20:xVERSIONx'
  9. // for Spring Data Redis v.2.1.x
  10. compile 'org.redisson:redisson-spring-data-21:xVERSIONx'
  11. // for Spring Data Redis v.2.2.x
  12. compile 'org.redisson:redisson-spring-data-22:xVERSIONx'
  13. // for Spring Data Redis v.2.3.x
  14. compile 'org.redisson:redisson-spring-data-23:xVERSIONx'
  15. // for Spring Data Redis v.2.4.x
  16. compile 'org.redisson:redisson-spring-data-24:xVERSIONx'
  17. // for Spring Data Redis v.2.5.x
  18. compile 'org.redisson:redisson-spring-data-25:xVERSIONx'
  19. // for Spring Data Redis v.2.6.x
  20. compile 'org.redisson:redisson-spring-data-26:xVERSIONx'
  21. // for Spring Data Redis v.2.7.x
  22. compile 'org.redisson:redisson-spring-data-27:xVERSIONx'
  23. // for Spring Data Redis v.3.0.x
  24. compile 'org.redisson:redisson-spring-data-30:xVERSIONx'
  25. // for Spring Data Redis v.3.1.x
  26. compile 'org.redisson:redisson-spring-data-31:xVERSIONx'
  27. // for Spring Data Redis v.3.2.x
  28. compile 'org.redisson:redisson-spring-data-32:xVERSIONx'
  29. // for Spring Data Redis v.3.3.x
  30. compile 'org.redisson:redisson-spring-data-33:xVERSIONx'
  31. // for Spring Data Redis v.3.4.x
  32. compile 'org.redisson:redisson-spring-data-34:xVERSIONx'
  1. Register RedissonConnectionFactory in Spring context:
  1. @Configuration
  2. public class RedissonSpringDataConfig {
  3. @Bean
  4. public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
  5. return new RedissonConnectionFactory(redisson);
  6. }
  7. @Bean(destroyMethod = "shutdown")
  8. public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  9. Config config = Config.fromYAML(configFile.getInputStream());
  10. return Redisson.create(config);
  11. }
  12. }

Upgrade to Redisson PRO with advanced features.

Spring Session

Please note that Redis or Valkey notify-keyspace-events setting should contain Exg letters to make Spring Session integration work.

Ensure you have Spring Session library in your classpath, add it if necessary:

Maven

  1. <dependency>
  2. <groupId>org.springframework.session</groupId>
  3. <artifactId>spring-session-core</artifactId>
  4. <version>3.4.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.redisson</groupId>
  8. <artifactId>redisson-spring-data-34</artifactId>
  9. <version>xVERSIONx</version>
  10. </dependency>

Gradle

  1. compile 'org.springframework.session:spring-session-core:3.4.1'
  2. compile 'org.redisson:redisson-spring-data-34:xVERSIONx'

Spring Http Session configuration

Add configuration class which extends AbstractHttpSessionApplicationInitializer class:

  1. @Configuration
  2. @EnableRedisHttpSession
  3. public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
  4. @Bean
  5. public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
  6. return new RedissonConnectionFactory(redisson);
  7. }
  8. @Bean(destroyMethod = "shutdown")
  9. public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  10. Config config = Config.fromYAML(configFile.getInputStream());
  11. return Redisson.create(config);
  12. }
  13. }

Spring WebFlux’s Session configuration

Add configuration class which extends AbstractReactiveWebInitializer class:

  1. @Configuration
  2. @EnableRedisWebSession
  3. public class SessionConfig extends AbstractReactiveWebInitializer {
  4. @Bean
  5. public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
  6. return new RedissonConnectionFactory(redisson);
  7. }
  8. @Bean(destroyMethod = "shutdown")
  9. public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  10. Config config = Config.fromYAML(configFile.getInputStream());
  11. return Redisson.create(config);
  12. }
  13. }

Spring Boot configuration

  1. Add Spring Session Data Redis library in classpath:
    Maven:
  1. <dependency>
  2. <groupId>org.springframework.session</groupId>
  3. <artifactId>spring-session-data-redis</artifactId>
  4. <version>3.2.1</version>
  5. </dependency>

Gradle:

  1. compile 'org.springframework.session:spring-session-data-redis:3.4.1'
  1. Add Redisson Spring Data Redis library in classpath:
    Maven:
  1. <dependency>
  2. <groupId>org.redisson</groupId>
  3. <artifactId>redisson-spring-data-34</artifactId>
  4. <version>xVERSIONx</version>
  5. </dependency>

Gradle:

  1. compile 'org.redisson:redisson-spring-data-34:xVERSIONx'
  1. Define follow properties in spring-boot settings
  1. spring.session.store-type=redis
  2. spring.redis.redisson.file=classpath:redisson.yaml
  3. spring.session.timeout.seconds=900

Upgrade to Redisson PRO with advanced features.

Spring Transaction Manager

Redisson provides implementation of both org.springframework.transaction.PlatformTransactionManager and org.springframework.transaction.ReactiveTransactionManager interfaces to participant in Spring transactions. See also Transactions section.

Spring Transaction Management

  1. @Configuration
  2. @EnableTransactionManagement
  3. public class RedissonTransactionContextConfig {
  4. @Bean
  5. public TransactionalBean transactionBean() {
  6. return new TransactionalBean();
  7. }
  8. @Bean
  9. public RedissonTransactionManager transactionManager(RedissonClient redisson) {
  10. return new RedissonTransactionManager(redisson);
  11. }
  12. @Bean(destroyMethod="shutdown")
  13. public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  14. Config config = Config.fromYAML(configFile.getInputStream());
  15. return Redisson.create(config);
  16. }
  17. }
  18. public class TransactionalBean {
  19. @Autowired
  20. private RedissonTransactionManager transactionManager;
  21. @Transactional
  22. public void commitData() {
  23. RTransaction transaction = transactionManager.getCurrentTransaction();
  24. RMap<String, String> map = transaction.getMap("test1");
  25. map.put("1", "2");
  26. }
  27. }

Reactive Spring Transaction Management

  1. @Configuration
  2. @EnableTransactionManagement
  3. public class RedissonReactiveTransactionContextConfig {
  4. @Bean
  5. public TransactionalBean transactionBean() {
  6. return new TransactionalBean();
  7. }
  8. @Bean
  9. public ReactiveRedissonTransactionManager transactionManager(RedissonReactiveClient redisson) {
  10. return new ReactiveRedissonTransactionManager(redisson);
  11. }
  12. @Bean(destroyMethod="shutdown")
  13. public RedissonReactiveClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  14. Config config = Config.fromYAML(configFile.getInputStream());
  15. return Redisson.createReactive(config);
  16. }
  17. }
  18. public class TransactionalBean {
  19. @Autowired
  20. private ReactiveRedissonTransactionManager transactionManager;
  21. @Transactional
  22. public Mono<Void> commitData() {
  23. Mono<RTransactionReactive> transaction = transactionManager.getCurrentTransaction();
  24. return transaction.flatMap(t -> {
  25. RMapReactive<String, String> map = t.getMap("test1");
  26. return map.put("1", "2");
  27. }).then();
  28. }
  29. }

Spring Cloud Stream

This feature is available only in Redisson PRO edition.

Redisson implements Spring Cloud Stream integration based on the reliable Stream structure for message delivery. To use Redis or Valkey binder with Redisson you need to add Spring Cloud Stream Binder library in classpath:

Maven:

  1. <dependency>
  2. <groupId>pro.redisson</groupId>
  3. <artifactId>spring-cloud-stream-binder-redisson</artifactId>
  4. <version>xVERSIONx</version>
  5. </dependency>

Gradle:

  1. compile 'pro.redisson:spring-cloud-stream-binder-redisson:xVERSIONx'

Compatible with Spring versions below.

Spring Cloud Stream Spring Cloud Spring Boot
4.2.x 2024.0.x 3.4.x
4.1.x 2023.0.x 3.0.x - 3.3.x
4.0.x 2022.0.x 3.0.x - 3.3.x
3.2.x 2021.0.x 2.6.x, 2.7.x (Starting with 2021.0.3 of Spring Cloud)
3.1.x 2020.0.x 2.4.x, 2.5.x (Starting with 2020.0.3 of Spring Cloud)

Receiving messages

Register the input binder (an event sink) for receiving messages as follows:

  1. @Bean
  2. public Consumer<MyObject> receiveMessage() {
  3. return obj -> {
  4. // consume received object ...
  5. };
  6. }

Define channel id in the configuration file application.properties. Example for receiveMessage bean defined above connected to my-channel channel:

  1. spring.cloud.stream.bindings.receiveMessage-in-0.destination=my-channel

Publishing messages

Register the output binder (an event source) for publishing messages as follows:

  1. @Bean
  2. public Supplier<MyObject> feedSupplier() {
  3. return () -> {
  4. // ...
  5. return new MyObject();
  6. };
  7. }

Define channel id in the configuration file application.properties. Example for feedSupplier bean defined above connected to my-channel channel:

  1. spring.cloud.stream.bindings.feedSupplier-out-0.destination=my-channel
  2. spring.cloud.stream.bindings.feedSupplier-out-0.producer.useNativeEncoding=true

Spring Data Redis

Integrates Redisson with Spring Data Redis library. Implements Spring Data’s RedisConnectionFactory and ReactiveRedisConnectionFactory interfaces and allows to interact with Redis or Valkey through RedisTemplate, ReactiveRedisTemplate or ReactiveRedisOperations object.

Usage

  1. Add redisson-spring-data dependency into your project:
    Maven:
  1. <dependency>
  2. <groupId>org.redisson</groupId>
  3. <!-- for Spring Data Redis v.1.6.x -->
  4. <artifactId>redisson-spring-data-16</artifactId>
  5. <!-- for Spring Data Redis v.1.7.x -->
  6. <artifactId>redisson-spring-data-17</artifactId>
  7. <!-- for Spring Data Redis v.1.8.x -->
  8. <artifactId>redisson-spring-data-18</artifactId>
  9. <!-- for Spring Data Redis v.2.0.x -->
  10. <artifactId>redisson-spring-data-20</artifactId>
  11. <!-- for Spring Data Redis v.2.1.x -->
  12. <artifactId>redisson-spring-data-21</artifactId>
  13. <!-- for Spring Data Redis v.2.2.x -->
  14. <artifactId>redisson-spring-data-22</artifactId>
  15. <!-- for Spring Data Redis v.2.3.x -->
  16. <artifactId>redisson-spring-data-23</artifactId>
  17. <!-- for Spring Data Redis v.2.4.x -->
  18. <artifactId>redisson-spring-data-24</artifactId>
  19. <!-- for Spring Data Redis v.2.5.x -->
  20. <artifactId>redisson-spring-data-25</artifactId>
  21. <!-- for Spring Data Redis v.2.6.x -->
  22. <artifactId>redisson-spring-data-26</artifactId>
  23. <!-- for Spring Data Redis v.2.7.x -->
  24. <artifactId>redisson-spring-data-27</artifactId>
  25. <!-- for Spring Data Redis v.3.0.x -->
  26. <artifactId>redisson-spring-data-30</artifactId>
  27. <!-- for Spring Data Redis v.3.1.x -->
  28. <artifactId>redisson-spring-data-31</artifactId>
  29. <!-- for Spring Data Redis v.3.2.x -->
  30. <artifactId>redisson-spring-data-32</artifactId>
  31. <!-- for Spring Data Redis v.3.3.x -->
  32. <artifactId>redisson-spring-data-33</artifactId>
  33. <!-- for Spring Data Redis v.3.4.x -->
  34. <artifactId>redisson-spring-data-34</artifactId>
  35. <version>xVERSIONx</version>
  36. </dependency>

Gradle:

  1. // for Spring Data Redis v.1.6.x
  2. compile 'org.redisson:redisson-spring-data-16:xVERSIONx'
  3. // for Spring Data Redis v.1.7.x
  4. compile 'org.redisson:redisson-spring-data-17:xVERSIONx'
  5. // for Spring Data Redis v.1.8.x
  6. compile 'org.redisson:redisson-spring-data-18:xVERSIONx'
  7. // for Spring Data Redis v.2.0.x
  8. compile 'org.redisson:redisson-spring-data-20:xVERSIONx'
  9. // for Spring Data Redis v.2.1.x
  10. compile 'org.redisson:redisson-spring-data-21:xVERSIONx'
  11. // for Spring Data Redis v.2.2.x
  12. compile 'org.redisson:redisson-spring-data-22:xVERSIONx'
  13. // for Spring Data Redis v.2.3.x
  14. compile 'org.redisson:redisson-spring-data-23:xVERSIONx'
  15. // for Spring Data Redis v.2.4.x
  16. compile 'org.redisson:redisson-spring-data-24:xVERSIONx'
  17. // for Spring Data Redis v.2.5.x
  18. compile 'org.redisson:redisson-spring-data-25:xVERSIONx'
  19. // for Spring Data Redis v.2.6.x
  20. compile 'org.redisson:redisson-spring-data-26:xVERSIONx'
  21. // for Spring Data Redis v.2.7.x
  22. compile 'org.redisson:redisson-spring-data-27:xVERSIONx'
  23. // for Spring Data Redis v.3.0.x
  24. compile 'org.redisson:redisson-spring-data-30:xVERSIONx'
  25. // for Spring Data Redis v.3.1.x
  26. compile 'org.redisson:redisson-spring-data-31:xVERSIONx'
  27. // for Spring Data Redis v.3.2.x
  28. compile 'org.redisson:redisson-spring-data-32:xVERSIONx'
  29. // for Spring Data Redis v.3.3.x
  30. compile 'org.redisson:redisson-spring-data-33:xVERSIONx'
  31. // for Spring Data Redis v.3.4.x
  32. compile 'org.redisson:redisson-spring-data-34:xVERSIONx'
  1. Register RedissonConnectionFactory in Spring context:
  1. @Configuration
  2. public class RedissonSpringDataConfig {
  3. @Bean
  4. public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
  5. return new RedissonConnectionFactory(redisson);
  6. }
  7. @Bean(destroyMethod = "shutdown")
  8. public RedissonClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
  9. Config config = Config.fromYAML(configFile.getInputStream());
  10. return Redisson.create(config);
  11. }
  12. }

Upgrade to Redisson PRO with advanced features.