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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Trending

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Ethical AI in Agile
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  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.3K 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
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!