Databases 8 min read

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.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Why a Single MySQL Connection Takes Over 200 ms – A Wireshark Deep Dive

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 closed

Using Wireshark we captured the entire handshake process:

Wireshark capture of MySQL connection
Wireshark capture of MySQL connection

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();
Wireshark capture of connection termination
Wireshark capture of connection termination

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 ms

Assuming 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 h

Thus, 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaWiresharkConnection Poolingdatabase-connection
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.