How to Manage Transactions in Redis on Java
Learn more about managing transactions in Redis with Java.
Join the DZone community and get the full member experience.
Join For FreeTransactions in Redis allow the execution of a group of commands to take place in a single step. Managing Redis transactions on Java can be tricky for some users, but it is much easier when you have the right tools at your disposal. In this post, you will learn all you need to know about the execution of Redis transactions on Java, as well as getting a brief insight into Spring Transaction Manager and XA Transactions for Redis.
Overview of Redis Transactions on Java
Redis transactions are atomic, meaning that either all the commands in the transaction are processed or none of them are. The commands are sequentially executed as a single isolated operation, with no option for a request issued by another client to be served in the middle of the execution of the transaction.
Redis transactions are founded on four commands: MULTI, EXEC, DISCARD, and WATCH. The MULTI command allows a user to issue multiple commands, which are all executed when the user calls EXEC. Even if a command in the list of multiple commands fails, all the other commands in the queue will still be processed. If this occurs, the user will see an error message.
Redisson is a Redis Java client that allows us to execute Transactions on Java with isolation level READ_COMMITTED
. Some examples of objects that could participate in Java Transaction are RMap, RMapCache, RLocalCachedMap, RSet, RSetCache, and RBucket. Supported Redis modes are:
SINGLE
MASTER/SLAVE
SENTINEL
ELASTICACHE REPLICATED
AZURE CACHE
RLEC
There are several options that the user can supply during the creation of a Transaction. These include:
TransactionOptions options = TransactionOptions.defaults()
// Synchronization data timeout between Redis master participating in transaction and its slaves.
// Default is 5000 milliseconds
options.syncSlavesTimeout(5, TimeUnit.SECONDS);
// Response timeout
// Default is 3000 milliseconds
options.responseTimeout(3, TimeUnit.SECONDS);
// Defines time interval for each attempt to send transaction if it hasn't been sent already.
// Default is 1500 milliseconds
options.retryInterval(2, TimeUnit.SECONDS)
// Defines attempts amount to send transaction if it hasn't been sent already.
// Default is 3 attempts
options.retryAttempts(3)
// If transaction hasn't been committed within timeout it will be rolledback automatically.
// Default is 5000 milliseconds
options.timeout(5, TimeUnit.SECONDS);
Spring Transaction Manager for Redis
Redisson enables participation in Spring transactions through the implementation of org.springframework.transaction.PlatformTransactionManager
.
The first step is to configure Redisson to enable transaction management:
@Configuration
@EnableTransactionManagementpublic
public class RedissonTransactionContextConfig {
@Bean
public TransactionalBean transactionBean() {
return new TransactionalBean();
}
@Bean public RedissonTransactionManager transactionManager(RedissonClient redisson) {
return new RedissonTransactionManager(redisson);
}
@Bean
public RedissonClient redisson() {
return BaseTest.createInstance();
}
@PreDestroy
public void destroy() {
redisson().shutdown();
}
}
public class TransactionalBean {
@Autowired
private RedissonTransactionManager transactionManager;
@Transactional
public void commitData() {
RTransaction transaction = transactionManager.getCurrentTransaction();
RMap map = transaction.getMap("test1"); map.put("1", "2");
}
}
You can then use Spring Transaction Manager to manage transactions in Redis.
XA Transactions in Redis
Redisson also provides XAResource implementation. This allows the participation of JTA transactions to perform distributed Transaction processing. Here is an example of execution XA Transactions in Redisson:
// transaction obtained from JTA compatible transaction manager
Transaction globalTransaction = transactionManager.getTransaction();
RXAResource xaResource = redisson.getXAResource();
globalTransaction.enlistResource(xaResource);
RTransaction transaction = xaResource.getTransaction();
RBucket<String> bucket = transaction.getBucket("myBucket");
bucket.set("simple");
RMap<String, String> map = transaction.getMap("myMap");
map.put("myKey", "myValue");
transactionManager.commit();
Conclusion
Redisson provides several features that can make it easier for users to manage transactions in Redis. These include Spring Transaction Manager and XAResource implementation. These features are extremely useful for a large number of Redis users who are looking to make use of Java transactions on Redis.
Opinions expressed by DZone contributors are their own.
Comments