Why Database Connections Are Costly: MySQL Handshake Takes 200ms and 7 Round-Trips
The article uses Wireshark to analyze MySQL connection establishment, revealing it requires 7 network round-trips and ~200ms per connection, demonstrating why connection pooling is essential for performance.
Reading font

MySQL Connection Establishment Analysis
The article investigates the resource cost of establishing a database connection by capturing and analyzing the network traffic between a Java application and a MySQL server using Wireshark. The test uses a minimal Java snippet that loads the MySQL JDBC driver and opens a connection via DriverManager.getConnection().
Class.forName("com.mysql.jdbc.Driver");
String name = "xttblog2";
String password = "123456";
String url = "jdbc:mysql://172.16.100.131:3306/xttblog2";
Connection conn = DriverManager.getConnection(url, name, password);
// program terminates, connection closed abruptlyConnection Steps Observed in Wireshark
The capture shows the MySQL protocol runs over TCP and is a binary protocol (not text-based like HTTP). The connection sequence consists of three main phases:
Step 1: TCP three-way handshake to establish the transport connection.
Step 2: Server sends a handshake packet (initial handshake information); client responds.
Step 3: Client sends an authentication packet (username/password); server replies with OK.
After successful authentication, additional round-trips occur to set session variables such as character set and autocommit mode. Even with no explicit configuration, the driver still performs these steps.
Timing Measurements
Two scenarios were measured:
Abrupt close (process termination): The connection is not closed via Connection.close(); the TCP connection ends with an RST packet. Excluding the final RST, the client and server exchange packets 7 round-trips . Total elapsed time: 225.243 ms (10.416042 - 10.190799 seconds).
Graceful close ( Connection.close() ): The client sends a MySQL-level quit command (no response waited), followed by a TCP four-way handshake (FIN/ACK). Total elapsed time: 183.357 ms (747.284311 - 747.100954 seconds).
Both measurements are on a local network; actual latency varies with network conditions, server load, and driver implementation.
Impact Calculation
Assuming a modest site with 20,000 daily active users, each making 5 requests (100,000 requests/day), and a conservative 150 ms per connection establishment:
100,000 * 150 ms = 15,000,000 ms = 15,000 s = 250 min = 4.17 hoursOver 4 hours per day are spent solely on opening database connections, not including query execution time.
Conclusion
The core takeaway: database connections are genuinely expensive in time because each requires multiple network round-trips and protocol negotiations. Therefore, connection pooling is mandatory to reuse connections and avoid repeated handshakes. For higher scale, additional techniques such as caching, SQL prepared statements, and load balancing are also needed.
About this article
For attribution, corrections or content rights, contact