Druid vs HikariCP: Which Connection Pool Wins?
This article compares Druid and HikariCP, the two most popular Java database connection pools, by explaining how connection pools work, presenting benchmark results, dissecting HikariCP's lock‑free design and bytecode optimizations, detailing Druid's rich monitoring and security features, and offering a practical decision framework for different scenarios.
Introduction
When choosing a database connection pool, many developers are torn between two excellent open‑source options: Druid, Alibaba’s feature‑rich pool, and HikariCP, the high‑performance default in Spring Boot.
1. What a Connection Pool Does
Creating a new JDBC connection for every request is wasteful: each connection requires a TCP handshake, authentication, and session setup, which consumes CPU and can quickly saturate the database server under high concurrency. A connection pool pre‑creates a set of connections at startup, allowing requests to borrow a connection from the pool and return it after use, eliminating the overhead of repeated creation and destruction.
The two most widely used pools in the Java ecosystem are HikariCP and Druid.
2. How Fast Is HikariCP?
2.1 Simple Performance Test
The following JUnit test creates a HikariCP datasource with a maximum pool size of 20 and measures the time to acquire and release 1,000 connections using 100 concurrent threads.
public class ConnectionPoolBenchmark {
@Test
public void testHikari() throws Exception {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/test");
config.setUsername("root");
config.setPassword("123456");
config.setMaximumPoolSize(20);
// Enable leak detection
config.setLeakDetectionThreshold(5000);
HikariDataSource ds = new HikariDataSource(config);
ExecutorService executor = Executors.newFixedThreadPool(100);
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
executor.submit(() -> {
try (Connection conn = ds.getConnection()) {
conn.isValid(1);
} catch (SQLException e) {
e.printStackTrace();
}
});
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.MINUTES);
long end = System.currentTimeMillis();
System.out.println("Total time: " + (end - start) + "ms");
}
}Results show that under 100 concurrent threads, HikariCP’s connection‑acquisition latency is consistently below 5 ms, and memory usage stays low (core code ~130 KB).
In the TechEmpower benchmark, HikariCP sustains over 150 k TPS, while Druid reaches roughly 80 k–120 k TPS. HikariCP’s latency is typically <5 ms versus Druid’s 10‑25 ms.
2.2 The Secret Behind HikariCP’s Speed
Two key factors make HikariCP fast:
1) ConcurrentBag – a lock‑free design
HikariCP uses a class called ConcurrentBag<T> to manage pooled connections. It relies on CopyOnWriteArrayList, ThreadLocal, and AtomicInteger to achieve lock‑free access.
public class ConcurrentBag<T> {
// sharedList holds all connections
private final CopyOnWriteArrayList<T> sharedList;
// threadList caches connections used by the current thread
private final ThreadLocal<List<Object>> threadList;
// Number of threads waiting for a connection
private final AtomicInteger waiters;
/**
* Core method to borrow a connection
*/
public T borrow(long timeout, final TimeUnit timeUnit) throws InterruptedException {
// ① Try ThreadLocal first – completely lock‑free!
List<Object> list = threadList.get();
for (int i = list.size() - 1; i >= 0; i--) {
final T bagEntry = (T) list.remove(i);
// CAS operation, lock‑free state update
if (bagEntry != null && bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) {
return bagEntry;
}
}
// ② If ThreadLocal is empty, fall back to sharedList with timeout handling
// ...
}
}The clever part is that each thread first checks its own ThreadLocal cache; if a connection is found, no lock is needed. Only when the cache is empty does the pool fall back to the shared list, which may involve synchronization.
In contrast, Druid acquires a lock for every borrow and return operation, leading to higher contention under load.
2) Bytecode‑level Optimizations
HikariCP performs compile‑time bytecode optimizations, such as replacing ArrayList with a custom FastList to reduce boundary checks, further reducing overhead.
2.3 Pool Size Best Practices
Setting maximumPoolSize too high is a common mistake. With a 4‑core database server, the optimal pool size is roughly (CPU cores × 2) + number of disks, which yields about 9 connections. Over‑provisioning creates a queue of waiting threads and unnecessary context switches.
Optimal connections = (CPU cores × 2) + effective disks
For a 4‑core server, 9‑10 connections are usually sufficient to handle thousands of concurrent requests.
It is also important to keep minimumIdle equal to maximumPoolSize to avoid frequent expansion and contraction of the pool.
# Recommended HikariCP configuration (production)
spring:
datasource:
hikari:
maximum-pool-size: 20 # set according to the formula above
minimum-idle: 20 # keep fixed size
connection-timeout: 3000 # 3 seconds
idle-timeout: 600000 # 10 minutes
max-lifetime: 1800000 # must be less than DB wait_timeout
leak-detection-threshold: 60000 # log stack trace after 60 s3. Why Druid Still Holds Half the Market
Druid’s biggest advantage is its built‑in monitoring capabilities.
3.1 Monitoring Core
Druid provides real‑time dashboards showing active/idle connections, SQL execution time, slow queries, and even correlates web requests with database interactions.
Enabling Druid’s monitoring is straightforward:
# application.yml
spring:
datasource:
druid:
stat-view-servlet:
enabled: true
url-pattern: /druid/*
login-username: admin
login-password: 123456
reset-enable: false
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: "*.js,*.css,*.jpg,/druid/*"
session-stat-enable: true
filter:
stat:
enabled: true
wall:
enabled: true
# SQL monitoring
filter:
stat:
enabled: true
log-slow-sql: true
slow-sql-millis: 1000
# Security wall
wall:
enabled: trueAfter configuration, visiting http://localhost:8080/druid displays a visual monitoring page with SQL statistics, URL stats, and session info.
3.2 “Killer Feature” – Leak Detection
Druid can automatically reclaim leaked connections and log the stack trace of the code that failed to close the connection, which is invaluable during production incidents.
@Configuration
public class DruidConfig {
@Bean
public DataSource druidDataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setUrl("jdbc:mysql://localhost:3306/test");
ds.setUsername("root");
ds.setPassword("123456");
ds.setInitialSize(10);
ds.setMaxActive(100);
ds.setMinIdle(10);
// Leak detection
ds.setRemoveAbandoned(true);
ds.setRemoveAbandonedTimeout(180); // 180 s
ds.setLogAbandoned(true);
ds.setAbandonWhenOverflow(true);
// Validation
ds.setValidationQuery("SELECT 1");
ds.setTestWhileIdle(true);
ds.setTimeBetweenEvictionRunsMillis(60000);
// Filters: monitoring + SQL firewall
ds.setFilters("stat,wall");
return ds;
}
}When a leak is detected, Druid logs the stack trace, pinpointing the exact line where getConnection() was called without a corresponding close.
3.3 SQL Firewall – Guard Against Injection
Druid includes a WallFilter that can block dangerous statements such as DROP TABLE or DELETE without a WHERE clause. This is essential for compliance‑heavy domains like finance and healthcare.
// Enable WallFilter with custom rules
Map<String, String> wallConfig = new HashMap<>();
wallConfig.put("deleteAllow", "false"); // forbid DELETE
wallConfig.put("dropTableAllow", "false"); // forbid DROP TABLE
wallConfig.put("createTableAllow", "false"); // forbid CREATE TABLE
WallConfig config = new WallConfig(wallConfig);
WallFilter wallFilter = new WallFilter();
wallFilter.setConfig(config);
ds.setProxyFilters(Arrays.asList(wallFilter));4. Side‑by‑Side Comparison
Key differences can be summarised as follows:
Design philosophy : HikariCP – minimalist, high‑performance “sports car”; Druid – feature‑rich, monitor‑centric “SUV”.
Performance : HikariCP ★★★★★ (latency <5 ms); Druid ★★★ (latency 10‑25 ms).
Memory footprint : HikariCP ~130 KB; Druid ~2 MB.
TPS ceiling : HikariCP >150 k/s; Druid 80‑120 k/s.
Monitoring : HikariCP basic stats; Druid visual dashboard + SQL‑level tracing.
SQL firewall : HikariCP none; Druid supports WallFilter.
Leak detection : HikariCP logs stack trace; Druid auto‑reclaims and logs.
5. Practical Selection Guide
There is no absolute “better” pool; the choice depends on the scenario.
5.1 When to Choose HikariCP
Microservice or cloud‑native architectures where startup speed and low memory are critical.
High‑concurrency transaction systems (e.g., flash sales, payment gateways) where every millisecond counts.
New Spring Boot projects – it’s the default and requires no extra dependency.
Resource‑constrained environments such as small Docker containers.
5.2 When to Choose Druid
Enterprise back‑office systems that need deep SQL auditing and performance analysis.
Financial or medical applications with strict compliance requiring SQL firewall and audit trails.
Legacy systems with frequent production incidents where Druid’s monitoring speeds up diagnosis.
Large teams that benefit from a shared visual monitoring dashboard.
5.3 Decision Matrix
Microservice / Cloud‑native – HikariCP (lightweight, fast start).
High‑concurrency / flash‑sale – HikariCP (nanosecond‑level latency).
Large e‑commerce platform – Druid (deep monitoring, diagnostics).
Finance / Healthcare – Druid (SQL audit + security).
Legacy system migration – Druid (visual monitoring).
Rapid prototype / startup project – HikariCP (simple, sufficient).
Conclusion
If you need raw performance, pick HikariCP – the “sports car” that accelerates straight down the highway. If you require comprehensive monitoring, security, and diagnostic capabilities, pick Druid – the “SUV” that handles rugged terrain with confidence.
Choose the pool that best matches your business requirements rather than chasing hype.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
