Why a Single MySQL Connection Can Take 200 ms – A Deep Dive into Connection Latency
This article analyzes the full lifecycle of a MySQL connection from Java code to TCP handshake using Wireshark, measures each step’s latency, calculates the total cost per request, and demonstrates why connection pooling is essential for high‑traffic web applications.
Background
Developers often wonder how much time is actually spent establishing a database connection, especially in web applications where each request may need a fresh connection. The article uses a minimal Java example that loads the MySQL driver, defines credentials, and opens a connection with DriverManager.getConnection. No queries are executed, and the program terminates, causing the connection to close abruptly.
Analysis
The Java snippet used for the experiment is:
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 forced closedWireshark was employed to capture the entire handshake and authentication process. The capture shows that MySQL uses a binary protocol over TCP, not a text‑based protocol like HTTP.
The connection establishment consists of three main steps:
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 flag, etc.) before any SQL statements can be issued.
Because the program ends without calling Connection.close(), the TCP stack sends a RST packet, which is not counted in the measured latency.
The measured time from the start of the TCP handshake to the last packet before the RST is: 10.416042 - 10.190799 = 0.225243 s = 225.243 ms This 225 ms represents the minimal cost of establishing a MySQL connection in this environment. Network conditions, server performance, and driver implementation can affect the exact number.
A second experiment added an explicit conn.close() call, capturing the graceful shutdown sequence. The total time from handshake start to connection close was: 747.284311 - 747.100954 = 0.183357 s = 183.357 ms Even with a proper close, the latency remains around 180–225 ms per connection.
Scaling this to a realistic workload: assuming a site with 20 000 daily active users, each making five requests, yields 100 000 requests per day. If each request incurs a 150 ms connection cost, the daily time spent only on establishing connections is:
100000 * 150 ms = 15 000 000 ms = 15 000 s = 250 min ≈ 4.17 hThus, connection establishment alone can consume several hours of CPU time per day, underscoring the necessity of a connection pool.
Conclusion
The core takeaway is that creating a new MySQL connection is surprisingly expensive—on the order of 200 ms—so applications should avoid opening connections per request. Using a connection pool, caching, prepared statements, load balancing, and other optimizations are essential to keep latency low and resource usage reasonable.
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.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
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.
