Why a Single MySQL Connection Takes Over 200 ms – A Deep Dive into Connection Overhead
The article measures the exact time cost of establishing and closing a MySQL connection from Java, breaks down each network and protocol step with Wireshark captures, and shows how these latencies add up to hundreds of milliseconds, making connection pooling essential for high‑traffic web services.
Background
Developers often wonder why creating a database connection feels slow, especially in web applications where connection pooling is recommended. This article investigates the detailed cost of establishing a MySQL connection using Java and Wireshark packet analysis.
Analysis Setup
MySQL is chosen because it is open‑source and its binary protocol is publicly documented, allowing a full trace of the handshake. The test program is written in Java, which may influence timing but the focus is on network, memory, and CPU resources.
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 closedThe above five‑line snippet opens a connection and then terminates the JVM without calling Connection.close(), which results in a TCP RST packet.
The capture shows that MySQL uses a binary protocol over TCP. The connection establishment consists of the following steps:
Step 1: TCP three‑way handshake.
Step 2: Server sends handshake packet; client responds.
Step 3: Client sends authentication packet; on success the server replies with OK and the session begins.
After authentication, the client and server exchange several configuration packets (character set, autocommit flag, etc.) before any SQL statements are executed.
Because the program ends abruptly, the connection is closed by a TCP RST, which is not counted in the measured latency. 10.416042 - 10.190799 = 0.225243s = 225.243ms The measured time from the start of the TCP handshake to the point where the connection is forcibly terminated (excluding the RST) is about 225 ms. The analysis shows that at least seven round‑trips between client and server are required for a full connection.
Proper Connection Closure
When the connection is closed gracefully with conn.close(), a new capture shows the following steps:
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();Step 1: Client sends a MySQL protocol close request; no server response is awaited.
Step 2: TCP four‑way termination completes the socket shutdown. 747.284311 - 747.100954 = 0.183357s = 183.357ms The full connect‑and‑close cycle therefore takes about 183 ms, still close to 200 ms, confirming that the overhead is dominated by network round‑trips and protocol handshakes.
Impact on High‑Traffic Applications
Assume a site with 20 000 daily active users, each issuing five requests, resulting in 100 000 requests per day. If each request creates a new connection costing a conservative 150 ms, the total time spent only on establishing connections is:
100000 * 150ms = 15000000ms = 15000s = 250min = 4.17hThus, roughly four hours per day are wasted on connection setup, making a connection pool indispensable.
When traffic grows further, a pool alone may not suffice; additional techniques such as caching, prepared‑statement reuse, and load balancing become necessary.
Conclusion
Establishing a MySQL connection from Java routinely consumes around 200 ms, primarily due to multiple network round‑trips and protocol negotiations. Frequent creation and destruction of connections is therefore inefficient, and developers should employ connection pooling and related performance‑optimisation strategies.
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
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
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.
