Databases 8 min read

Why a Single MySQL Connection Can Take 200 ms – A Wireshark Deep Dive

This article measures the exact time cost of establishing a MySQL connection from Java, breaks down the TCP handshake, authentication, and protocol steps with Wireshark captures, and shows why connection pooling is essential for high‑traffic web applications.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why a Single MySQL Connection Can Take 200 ms – A Wireshark Deep Dive

Background

Developers often wonder how much time is really spent creating a database connection in a web application. While it is well known that opening a connection is expensive, the exact latency and its components are rarely quantified.

Analysis

The article uses a simple Java program that loads the MySQL driver and opens a connection with five lines of code. Wireshark is then used to capture the entire handshake and authentication process.

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 closed by process termination

The capture shows that MySQL uses a binary protocol over TCP. The connection establishment consists of:

Step 1: TCP three‑way handshake.

Step 2: Server sends handshake packet, client replies.

Step 3: Client sends authentication packet; on success the server returns an OK packet and the session is ready for commands.

After authentication the client and server exchange several configuration packets (character set, autocommit, etc.). Because the test program does not execute any queries, the only network traffic is the connection setup and the final TCP reset caused by the abrupt process termination.

The measured time from the first SYN to the final RST (excluding the RST) is:

10.416042 - 10.190799 = 0.225243 s = 225.243 ms

This indicates that even the minimal connection path takes about 225 ms, with at least seven round‑trips between client and server.

Proper Connection Closure

When the connection is closed gracefully with Connection.close(), the shutdown sequence is different. The capture shows the client sending a MySQL protocol close request followed by 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 total time for a full open‑and‑close cycle in this test is:

747.284311 - 747.100954 = 0.183357 s = 183.357 ms

Implications for High‑Traffic Sites

Assuming a conservative 150 ms per connection, a site with 20 000 daily active users each making five requests would spend:

100 000 requests × 150 ms = 15 000 000 ms = 4.17 hours per day

Thus, connection establishment alone can consume several hours of CPU time, making a connection pool indispensable. When traffic grows further, additional techniques such as caching, SQL statement pre‑compilation, and load balancing become necessary.

Conclusion

The core takeaway is that establishing a database connection is surprisingly costly; avoiding frequent connections by using a pool dramatically improves performance and resource utilization.

Wireshark capture of connection setup
Wireshark capture of connection setup
Wireshark capture of graceful close
Wireshark capture of graceful close
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.

JavamysqlWiresharkConnection PoolingDatabase Connection
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.