Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Databases
3 min read

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.

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 abruptly

Connection 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.

Wireshark capture showing MySQL connection handshake packets
Wireshark capture showing MySQL connection handshake packets

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).

Wireshark capture showing graceful MySQL connection close
Wireshark capture showing graceful MySQL connection close

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 hours

Over 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.

For attribution, corrections or content rights, contactadmin@besthub.dev

Report an issue with this article

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion