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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Best Practices for Creating Highly Reliable Applications in Mule 4
  • Java and Low Latency
  • Strategies for Securing E-Commerce Applications
  • How to Create a Successful API Ecosystem

Trending

  • Assessing Bias in AI Chatbot Responses
  • Mastering Deployment Strategies: Navigating the Path to Seamless Software Releases
  • How Can Developers Drive Innovation by Combining IoT and AI?
  • Navigating Double and Triple Extortion Tactics
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Solving Parallel Writing Issues in MuleSoft With Distributed Locking

Solving Parallel Writing Issues in MuleSoft With Distributed Locking

Learn how to solve parallel writing issues in MuleSoft applications running in multi-threaded or multi-worker environments using distributed locking.

By 
Nahid E Mahbub user avatar
Nahid E Mahbub
·
Dec. 12, 24 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
4.6K Views

Join the DZone community and get the full member experience.

Join For Free

In MuleSoft applications running in multi-threaded or multi-worker environments, the risk of parallel writing issues arises when multiple threads or workers attempt to access or update the same shared resource simultaneously. These issues can lead to data corruption, inconsistencies, or unexpected behavior, especially in distributed systems where Mule applications are deployed across multiple workers in a cluster.

The Problem: Parallel Writing

Let me illustrate the problem first. I created a simple counter application that supports /inc and /get operations. The /inc operation increases the counter by 1, and the /get operation returns the current value.

Now, if I hit /inc parallelly from JMeter 300 times, the /get should return exactly 300 if all writes are successful and no overwrite happens. However, in Mule4, we will not get 300.

Issue: Lower Count Due to Overwriting

The screenshot shows we are getting a much lower number here because of parallel threads, which results in overwriting.

Lower count because of overwriting

Lower count because of overwriting

The Solution: Distributed Locking in Mule 4

To solve this issue, Mule 3 provided the "retrieve-with-lock" object store operation (<objectstore:retrieve-with-lock>). However, Mule 4 does not offer this out-of-the-box solution. Instead, Mule 4 requires distributed locking via LockFactory.createLock().

The counter application version 2 uses a Groovy script and Java bean to implement distributed locking. The Groovy script is very small, and it performs only the bean lookup and method invocation:

XML
 
<scripting:execute engine="Groovy">
  <scripting:code><![CDATA[registry.lookupByName("beanName").get().retrieveValue(key)]]></scripting:code>
</scripting:execute>


And the Java part is doing the actual locking:

Java
 
Lock lock = muleContext.getLockFactory().createLock(objectStoreName + "Lock");


ObjectStore<CustomObject> objectStore = muleContext.getObjectStoreManager()
.getObjectStore(objectStoreName);

    lock.lock();

    try {

        List<String> keys = objectStore.allKeys();

        for(String key : keys) {
            CustomObject value = objectStore.retrieve(key);

            if (value.getClientId().equals(clientId)) {
                objectStore.remove(key);
                break;
            }
        }

        CustomObject newObject = new CustomObject(token);
        if (objectStore.contains(token)) {
            objectStore.remove(token);
        }

        objectStore.store(token, newObject);
    } finally {
    lock.unlock();
}


Now, the operation is slightly slower due to the locking mechanism, but the overwriting issue is resolved. The counter value now returns exactly 300, as expected:

Exact count because of no overwriting

Exact count because of no overwriting

Consideration for CloudHub Deployments

However, there is just one issue: if you have multiple workers in a Cloudhub deployment, you need to implement a cluster-wide distributed lock. Go to CloudHub settings and enable cluster mode to ensure the lock is applied across the entire cluster:

Cloudhub config

Cloudhub config

Conclusion

Distributed locking is a powerful technique to handle parallel writing issues in MuleSoft applications deployed in multi-threaded or multi-worker environments. By leveraging Object Store or similar mechanisms, you can synchronize resource access and maintain data integrity.

When implemented correctly, distributed locking ensures that your applications remain reliable and consistent, even in the most demanding distributed systems.


MuleSoft applications Lock (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Best Practices for Creating Highly Reliable Applications in Mule 4
  • Java and Low Latency
  • Strategies for Securing E-Commerce Applications
  • How to Create a Successful API Ecosystem

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!