Why a Single MySQL Connection Takes Over 200 ms – A Wireshark Deep Dive
This article dissects the MySQL connection process in Java, measuring each handshake and authentication step with Wireshark to reveal why establishing a single database connection can consume around 200 ms, and why connection pooling is essential for high‑traffic applications.
When developing web applications, understanding the low‑level details of database connections is crucial, especially the cost of establishing a new connection for each request.
Analysis
We use MySQL as the example because its protocol is open and well documented. The analysis focuses mainly on network latency, though memory and CPU usage also matter, and the code is written in Java.
Below is the minimal Java code used to open a connection:
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 closedUsing Wireshark we captured the entire handshake process:
The MySQL protocol runs over TCP and uses a binary format. The connection establishment consists of:
Step 1: TCP three‑way handshake.
Step 2: Server sends handshake packet; client responds.
Step 3: Client sends authentication packet; server replies with OK.
After successful authentication the client sets session variables (charset, autocommit, etc.) through several additional exchanges before any query can be executed.
Because the program terminates without calling Connection.close(), the TCP connection is closed abruptly, generating a RST packet.
Even with this minimal code (no extra connection properties), the measured time from the start of the TCP handshake to the forced termination (excluding the final RST) is: 10.416042 - 10.190799 = 0.225243 s = 225.243 ms This shows that a single MySQL connection can take roughly 225 ms, a figure that can vary with network conditions, server performance, and code efficiency.
In a normal application the connection is closed gracefully with conn.close():
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 termination sequence captured shows:
Step 1: Client sends a MySQL protocol close request without waiting for a server response.
Step 2: TCP four‑way handshake completes the disconnection.
The measured time for the graceful close is:
747.284311 - 747.100954 = 0.183357 s = 183.357 msAssuming a conservative 150 ms per connection, a site with 20 000 daily active users each making 5 requests would spend:
100000 * 150 ms = 15,000,000 ms = 15,000 s = 250 min ≈ 4.17 hThus, establishing connections alone can consume several hours of CPU time per day, making connection pooling indispensable. As traffic grows, additional strategies such as caching, SQL statement pre‑compilation, and load balancing become necessary.
Conclusion
The core takeaway is that creating a database connection is expensive; avoid doing it repeatedly by using a connection pool and other performance‑optimizing techniques.
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.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
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.
