Why a Single MySQL Connection Can Cost Over 200 ms – A Deep Dive
This article examines the detailed steps and timing of establishing a MySQL connection from a Java web application, measuring network round‑trips and total latency, and demonstrates how even a minimal connection can consume hundreds of milliseconds, making connection pooling essential for high‑traffic services.
Background
Connecting to a database in a web application is often taken for granted, yet the actual cost of establishing a connection can be substantial. This article investigates the low‑level details of creating a MySQL connection, using Java code and Wireshark packet captures to quantify the latency.
Sample Java Code for a Simple 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);
// after program terminates, connection is forcibly closedWireshark Capture of the Connection Process
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 a handshake packet; client replies.
Step 3: Client sends an authentication packet; server returns OK and the session is ready.
Timing of the Simple Connection
The measured time from the start of the TCP handshake to the forced termination of the process (excluding the final RST packet) is: 10.416042 - 10.190799 = 0.225243s = 225.243ms This 225 ms represents the minimal latency for establishing a MySQL connection in this environment.
Closing the Connection Properly
When the connection is closed explicitly via Connection.close(), the process includes an additional protocol phase and 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();Timing of Connection with Proper Close
747.284311 - 747.100954 = 0.183357s = 183.357msThe full cycle (connect + close) takes about 183 ms, still close to 200 ms.
Impact on High‑Traffic Applications
Assuming a conservative 150 ms per connection, a site with 20 000 daily active users each making 5 requests would generate 100 000 connections per day. The total time spent only on establishing connections would be:
100000 * 150ms = 15000000ms = 15000s = 250min = 4.17hThus, connection establishment alone can consume over four hours of CPU time per day, highlighting the necessity of a connection pool.
Conclusion
Database connections are expensive; repeatedly opening and closing them dramatically degrades performance. Using a connection pool is essential, and additional optimizations such as caching, prepared statements, and load balancing should be considered for larger scales.
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.
