DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel

Trending

  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Building Production-Grade GenAI on GCP with Vertex AI Agent Builder
  • Product-Led Software Delivery: Intelligent Platforms for DevOps at Scale
  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  1. DZone
  2. Coding
  3. Java
  4. How to Manage Transactions in Redis on Java

How to Manage Transactions in Redis on Java

Learn more about managing transactions in Redis with Java.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Mar. 04, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
27.8K Views

Join the DZone community and get the full member experience.

Join For Free

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

Redis (company) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook