Why a Single MySQL Connection Takes 200+ms and How Pooling Saves Hours
This article examines the detailed steps and timing of establishing a MySQL connection from Java, measuring over 200 ms per connection, and demonstrates how proper connection closing and connection pooling can dramatically reduce latency in high‑traffic web applications.
Background
The author, a software architect, wanted to understand the exact cost of creating a database connection in a web application, focusing on the network, CPU, and memory resources involved.
Analysis
MySQL was chosen because its open‑source protocol allows a full inspection of the handshake process. The test uses Java as the client language, but the methodology applies to any language.
Java Connection Code
Class.forName("com.mysql.jdbc.Driver");
String name = "shine_user";
String password = "123";
String url = "jdbc:mysql://172.16.100.131:3306/clever_mg_test";
Connection conn = DriverManager.getConnection(url, name, password);
// program ends, connection is forcibly closedWireshark was used to capture the entire TCP and MySQL protocol exchange.
The capture shows that MySQL uses a binary protocol over TCP. The connection establishment consists of the following steps:
Step 1: Establish a TCP connection via the three‑way handshake.
Step 2: Server sends a handshake packet; client replies.
Step 3: Client sends an authentication packet; server returns OK and the session begins.
After authentication, the client sets session variables (character set, autocommit, etc.) and exchanges several packets before any SQL statements are executed.
In this minimal example the program terminates without calling Connection.close(), causing the process to exit and the TCP connection to be reset (RST).
Measured time from the start of the TCP handshake to the forced termination (excluding the final RST) is: 10.416042 - 10.190799 = 0.225243s = 225.243ms This indicates that a single connection creation costs roughly 225 ms under ideal conditions.
Closing the Connection Properly
When the connection is closed explicitly with Connection.close(), the shutdown follows the MySQL protocol and then a TCP four‑way termination.
Class.forName("com.mysql.jdbc.Driver");
String name = "shine_user";
String password = "123";
String url = "jdbc:mysql://172.16.100.131:3306/clever_mg_test";
Connection conn = DriverManager.getConnection(url, name, password);
conn.close();The measured time for a complete open‑and‑close cycle is: 747.284311 - 747.100954 = 0.183357s = 183.357ms Even with a proper close, the latency remains close to 200 ms.
Impact on High‑Traffic Applications
Assuming a conservative 150 ms per connection, a site with 20 000 daily active users each making 5 requests would perform 100 000 connections per day, consuming:
100000 * 150ms = 15000000ms = 15000s = 250min = 4.17hThus, connection establishment alone can consume over four hours of CPU time per day, making connection pooling essential. Additional techniques such as caching, prepared statements, and load balancing are also recommended.
Conclusion
Database connections are expensive; avoid creating them for every request and use a connection pool to keep latency low and resource usage efficient.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.
