Why a Single MySQL Connection Takes 225 ms – A Deep Dive into Connection Latency
This article analyzes the detailed steps and timing of establishing and closing a MySQL connection from Java, using Wireshark traces to show that even a minimal connection can consume over 200 ms, highlighting the need for connection pooling in high‑traffic web applications.
Developers often wonder how much time a database connection really consumes; this article investigates the exact latency of creating and closing a MySQL connection in a Java web application, using Wireshark packet captures to break down each network and protocol step.
Test Setup and Java Code
The experiment uses a simple Java snippet that loads the MySQL driver, defines credentials, and opens a connection without any additional properties:
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 captures of this run reveal the full TCP handshake and MySQL binary protocol exchange.
Connection Establishment Process
Step 1: TCP three‑way handshake establishes the transport layer.
Step 2: Server sends its handshake packet; client replies.
Step 3: Client sends an authentication packet; on success the server returns an OK packet and the session is ready for commands.
After authentication, the server and client exchange configuration settings such as character set and autocommit. Even with the minimal code, the driver still sets these defaults.
Measured Timing
The capture shows that, excluding the final TCP RST caused by abrupt program termination, the round‑trip count between client and server is seven. The total time from the start of the TCP handshake to the forced disconnection is:
10.416042 s – 10.190799 s = 0.225243 s (≈ 225 ms)
This demonstrates that a single connection can easily consume over 200 ms, even in an optimal scenario.
Proper Connection Closure
When the connection is closed gracefully with conn.close();, the shutdown sequence differs:
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 now shows the client sending a close request without waiting for a server response, followed by a TCP four‑way handshake to terminate the connection.
The measured time for the full open‑and‑close cycle with proper shutdown is:
747.284311 s – 747.100954 s = 0.183357 s (≈ 183 ms)
Impact on High‑Traffic Applications
Assuming a site with 20 000 daily active users, each generating five requests, the system would handle 100 000 requests per day. If each request incurs a conservative 150 ms connection overhead, the total time spent merely establishing connections would be:
100 000 × 150 ms = 15 000 000 ms = 15 000 s ≈ 250 minutes ≈ 4.2 hours per day.
Thus, connection pooling is essential; otherwise, a substantial portion of CPU time is wasted on connection setup.
Additional Optimizations
Caching query results.
Using prepared statements to avoid repeated parsing.
Load balancing across multiple database instances.
Other techniques such as read‑replicas, sharding, etc.
The core takeaway is clear: establishing a database connection is expensive, and applications should avoid doing it per request.
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.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
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.
