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
Newsletter
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
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop

Trending

  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems
  • Why AI Agents Need a Shared Enterprise Ontology
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • The SDLC After AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Mar. 04, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
28.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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