How a MySQL Connection Takes 200ms+ and Why You Need a Pool
This article dissects the MySQL connection process using Java and Wireshark, revealing that establishing a single connection can consume over 200 ms, which scales to hours of latency for high‑traffic sites, highlighting the necessity of connection pooling and related optimizations.
Using MySQL as an example—because its open‑source nature makes its communication protocol publicly available—we can thoroughly analyze the entire process of establishing a connection.
The resource consumption in this analysis is primarily network‑related, though memory and CPU are also involved; the implementation language is Java.
Java code to open a MySQL 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 terminates, connection is forcibly closedWireshark captures of the connection establishment are shown below:
The capture reveals that MySQL uses a binary protocol over TCP. The connection steps are:
Step 1: Establish a TCP connection via the three‑way handshake.
Step 2: Server sends a handshake packet; client responds.
Step 3: Client sends an authentication packet; upon successful verification the server returns an OK response and command execution can begin.
After successful authentication, the server sets various connection variables (e.g., character set, autocommit). Only after these steps are actual queries executed.
In our test, only the five lines of code above are run, without any queries. The program ends without calling Connection.close(), causing the connection to be terminated abruptly and a TCP RST packet to appear. The measured round‑trip time (excluding the final RST) is:
10.416042 − 10.190799 = 0.225243 s ≈ 225 ms.
This demonstrates that establishing a single database connection can take roughly 225 ms, even in an optimal scenario. For a site with 100 000 daily requests (assuming 5 requests per user) and a conservative 150 ms per connection, the total time spent merely establishing connections would be:
100 000 × 150 ms = 15 000 s ≈ 4.17 hours per day.
Therefore, connection pooling is essential, and additional optimizations such as caching, SQL prepared statements, and load balancing should be considered.
Properly closing the 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);
conn.close();Wireshark capture of the graceful shutdown shows the client sending a close request without waiting for a server response, followed by a four‑way TCP termination. The total time for this complete cycle is:
747.284311 − 747.100954 = 0.183357 s ≈ 183 ms.
The core takeaway is that database connections are costly; frequent creation of connections should be avoided.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
