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.
Join the DZone community and get the full member experience.
Join For FreeMuleSoft 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:
- Faster Response Times: Reduced connection acquisition time leads to lower latency in API responses.
- Enhanced Throughput: Optimized connection pooling ensures better handling of concurrent requests.
- Thread Management: Prevents thread saturation and reduces CPU overhead.
- 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:
<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:
<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 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:
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:
<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
- Run the Mule application to ensure the Spring context is loaded correctly.
- 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.
Opinions expressed by DZone contributors are their own.
Comments