Reactive Spring Transactions in Redis
Learn how Redisson supports reactive Spring transactions.
Join the DZone community and get the full member experience.
Join For Free
Spring is an open-source, lightweight Java application framework that simplifies the process of developing enterprise software for the Java programming language. The Spring framework includes many core features that promote good software development practices, such as dependency injection and a model-view-controller (MVC) architectural pattern.
Reactive programming is a declarative programming paradigm that works with asynchronous data streams in order to respond to real-time events and build scalable, resilient, concurrent applications. Beginning with Spring Framework 5.2 M2, Spring includes support for reactive transaction management.
Spring includes a Spring Data Redis module for compatibility with Redis, an in-memory data structure store often used to build NoSQL databases. However, Spring doesn't provide support for Redis-based transactions out of the box. For this reason, many Java developers use a third-party Java Redis framework like Redisson to perform reactive Spring transactions in Redis.
Redisson: Reactive Spring Transactions in Redis
Redisson is a third-party open-source Redis Java client. By including distributed implementations of many common Java objects and collections, Redisson dramatically simplifies the process of using Java with Redis.
Redisson offers support for reactive Spring transactions in Redis via interfaces such as RedissonReactiveClient, RTransactionReactive, and ReactiveRedissonTransactionManager. To understand how these all work together, below is an example of how to do reactive Spring transactions in Redis using Redisson:
xxxxxxxxxx
public class TransactionalBean {
private ReactiveRedissonTransactionManager transactionManager;
public Mono<Void> commitData() {
Mono<RTransactionReactive> transaction = transactionManager.getCurrentTransaction();
return transaction.flatMap(t -> {
RMapReactive<String, String> map = t.getMap("myMap");
return map.put("1", "2");
}).then();
}
}
public class RedissonReactiveTransactionContextConfig {
public TransactionalBean transactionBean() {
return new TransactionalBean();
}
public ReactiveRedissonTransactionManager transactionManager(RedissonReactiveClient redisson) {
return new ReactiveRedissonTransactionManager(redisson);
}
destroyMethod="shutdown") (
public RedissonReactiveClient redisson( ("classpath:/redisson.yaml") Resource configFile) throws IOException {
Config config = Config.fromYAML(configFile.getInputStream());
return Redisson.createReactive(config);
}
}
Reactive Spring transactions in Redis are just one example of how developers can implement reactive programming, Java transactions, and the Spring framework using Redisson. The Spring framework is fully compatible with Redis, thanks to the Redisson Java client for Redis.
To get started using Spring with Redisson, check out the Configuration section in the Redisson project wiki. For more information about reactive programming and reactive Spring transactions in Redis, check out the Redisson website.
Further Reading
Using Spring Data Redis in Spring Boot 2.0 to Write a Custom Query
Implementation of Redis in a Microservice/Spring Boot Application
Opinions expressed by DZone contributors are their own.
Comments