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

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

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

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

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

Related

  • How to Set up OAuth JWT Flow and mTLS in the Salesforce Connector for a MuleSoft App
  • Trigger Salesforce Assignment Rules and Send Notifications From MuleSoft
  • How To Plan a (Successful) MuleSoft VPN Migration (Part I)
  • On-Demand-Schedulers With MuleSoft CloudHub APIs

Trending

  • Navigating Change Management: A Guide for Engineers
  • Building an AI/ML Data Lake With Apache Iceberg
  • Advancing Robot Vision and Control
  • Simplifying Multi-LLM Integration With KubeMQ
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Optimizing MuleSoft Performance With HikariCP: A Complete Guide

Optimizing MuleSoft Performance With HikariCP: A Complete Guide

Learn how to integrate HikariCP with MuleSoft for optimized database connections, improved performance, and best configuration practices for efficiency.

By 
Kamal Adivi user avatar
Kamal Adivi
·
Dec. 11, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
6.8K Views

Join the DZone community and get the full member experience.

Join For Free

MuleSoft is a powerful integration platform that often deals with high-throughput workloads that require robust database connection management. One solution that stands out in optimizing database interactions is HikariCP, a high-performance JDBC connection pool known for its speed and reliability. HikariCP is widely used in applications that require efficient connection management.

In this article, we'll discuss the integration of HikariCP with MuleSoft, its benefits, and best practices for configuring it to maximize performance.

What Is HikariCP?

HikariCP is a lightweight, high-performance JDBC connection pool known for its minimal overhead and advanced optimization features. It provides fast connection acquisition, efficient connection pool management, and built-in tools to reduce latency and improve database interaction reliability.

Key Features

  • High Performance: Low-latency operations and efficient resource utilization make it one of the fastest connection pools available.
  • Reliability: Features like connection validation and leak detection enhance stability.
  • Scalability: Supports high-concurrency applications with minimal thread contention.
  • Lightweight: Minimal footprint in terms of memory and CPU usage.

Why Use HikariCP in MuleSoft?

MuleSoft applications often interact with databases to process real-time and batch data. Efficient management of database connections is critical to meeting high transaction per minute (TPM) requirements and ensuring system reliability. HikariCP offers:

  1. Faster Response Times: Reduced connection acquisition time leads to lower latency in API responses.
  2. Enhanced Throughput: Optimized connection pooling ensures better handling of concurrent requests.
  3. Thread Management: Prevents thread saturation and reduces CPU overhead.
  4. Error Handling: Automatically detects and manages problematic connections, reducing application downtime.

Integrating HikariCP With MuleSoft

To integrate HikariCP in MuleSoft, follow these steps:

Step 1: Configure the HikariCP Module

 MuleSoft does not natively include HikariCP (it comes with C3P0 by default), it has to be added as a custom dependency. Update your project's pom.xml to include the HikariCP library:

XML
 
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>5.0.1</version> <!-- Use the latest stable version -->
</dependency>


Step 2: Define HikariCP Configuration

Add Spring dependencies in pom.xml. The easiest way to add Spring jars is via "Spring Authorization Filter" from the Mule palette:

XML
 
<dependency>
    <groupId>org.mule.modules</groupId>
    <artifactId>mule-spring-module</artifactId>
    <version>1.3.6</version>
    <classifier>mule-plugin</classifier>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>5.4.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.3.2</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>5.4.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.2</version>
</dependency>


Step 3: Create Spring Configuration

Define a Spring configuration XML file (spring-config.xml) to initialize the HikariCP DataSource and expose it as a Spring Bean. Add this XML config file to src/main/resources.

XML
 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- HikariCP DataSource Configuration -->
    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydb"/>
        <property name="username" value="dbuser"/>
        <property name="password" value="dbpassword"/>
        <property name="maximumPoolSize" value="20"/>
        <property name="minimumIdle" value="5"/>
        <property name="idleTimeout" value="30000"/>
        <property name="connectionTimeout" value="30000"/>
        <property name="maxLifetime" value="1800000"/>
        <property name="poolName" value="MuleHikariCP"/>
    </bean>
</beans>


HikariCP Pool Size (Maximum Pool Size)

Here’s a detailed guide on why and how to avoid setting a higher connection pool size:

  • For example, with 2400 TPM (which is 40 TPS) and each query taking about 1 second, you may need around 40 connections, with an additional buffer of 10-20% to handle spikes, giving a total pool size of around 44-48 connections.

Caution: Avoiding an excessively high connection pool size in HikariCP (or any connection pooling mechanism) is critical for optimizing resource usage and ensuring stable application performance. A higher-than-necessary pool size can lead to resource contention, database overload, and system instability.

1. Right-Size the Pool

Use the formula below to calculate the optimal pool size:

CSS
 
Optimal Pool Size = (Core Threads) × (1 + (Wait Time / Service Time))

Optimal Pool Size = (Core Threads) × (1 + (Service Time / Wait Time))


  • Core Threads: Number of threads available for executing queries.
  • Wait Time: Time the application can afford to wait for a connection.
  • Service Time: Average time for a query to execute.

2. Connection Timeout

Set the connectionTimeout to a value less than your SLA, such as 500-700 milliseconds, to ensure that connections are not held up for too long if they are not available.

3. Idle Timeout

Configure idleTimeout to a lower value, like 30,000 ms (30 seconds), so that idle connections are quickly released, avoiding resource waste.

4. Max Lifetime

Set the maxLifetime slightly shorter than the database’s connection timeout (e.g., 30 minutes) to avoid connections being closed abruptly by the database.

5. Connection Validation

Use validationQuery or enable validationTimeout to ensure connections are always valid, keeping the latency minimal.

6. Database Connection Utilization

  • Ensure that queries are optimized for performance on the database side.
  • Monitor database resources (CPU, memory, and indexes) to see if adjustments can be made for better utilization.

These settings should improve how connections are used and help achieve your target response time of under 1 second. If you’re still experiencing issues, consider analyzing query performance and optimizing database operations.

Step 4. Configure Spring in Mule Application Global Elements

Add this Spring configuration in global.xml and refer to the config-ref:

XML
 
<mule xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:db="http://www.mulesoft.org/schema/mule/db"
      xmlns:spring="http://www.mulesoft.org/schema/mule/spring"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd 
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/spring http://www.mulesoft.org/schema/mule/spring/current/mule-spring.xsd">
	
  <!--Spring beans-->
  <spring:config name="Spring_Config" doc:name="Spring Config" doc:id="5550c804-20cf-40c0-9331-d3ee45d7444f" files="spring-beans.xml" />
	
    <!--DB Config.. Reference the datasource created in Spring beans-->

  <db:config name="Database_Config" doc:name="Database Config" doc:id="f18a2e4d-0f43-4adc-86f3-f76a42ecc3c9" >
		<db:data-source-connection dataSourceRef="dataSource" />
	</db:config>
	    

     <!-- Database Configuration -->
    
    
</mule>


Step 5: Use the Spring Bean in Mule Flows

The datasource bean is now available in the MuleSoft application and can be referenced in the db module configuration.

Step 6: Verify Configuration

  1. Run the Mule application to ensure the Spring context is loaded correctly.
  2. Test database interactions to validate the connection pooling behavior.

Step 7: Simulate Load Tests for HikariCP Pool Size Configuration

Test Scenario

  • API Use Case: Mule 4 API handling database queries with HikariCP connection pooling.
  • Transaction Load: 3000 Transactions Per Minute (TPM).
  • Concurrency: 50 concurrent users.
  • Query Execution Time: 200ms per query.
  • Connection Pool Configuration:
    • Max Pool Size: 240
    • Min Pool Size: 20
    • Idle Timeout: 30 seconds
    • Max Lifetime: 30 minutes
    • Leak Detection Threshold: 2 seconds

Conclusion

Integrating HikariCP into your MuleSoft applications unlocks a new level of performance, reliability, and scalability for database interactions. By utilizing HikariCP’s efficient connection pooling and combining it with MuleSoft’s robust integration capabilities, you can confidently handle high-traffic workloads and demanding SLAs.

Whether you're building APIs, processing real-time data, or managing complex integrations, HikariCP ensures optimal use of resources, reduced latency, and seamless scalability. With proper configuration and thoughtful integration, you can transform your MuleSoft applications into high-performance engines ready for modern enterprise challenges.

Connection pool MULE MuleSoft optimization

Opinions expressed by DZone contributors are their own.

Related

  • How to Set up OAuth JWT Flow and mTLS in the Salesforce Connector for a MuleSoft App
  • Trigger Salesforce Assignment Rules and Send Notifications From MuleSoft
  • How To Plan a (Successful) MuleSoft VPN Migration (Part I)
  • On-Demand-Schedulers With MuleSoft CloudHub APIs

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!