Databases 8 min read

Why a Single MySQL Connection Takes 200+ms and How Pooling Saves Hours

This article examines the detailed steps and timing of establishing a MySQL connection from Java, measuring over 200 ms per connection, and demonstrates how proper connection closing and connection pooling can dramatically reduce latency in high‑traffic web applications.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Why a Single MySQL Connection Takes 200+ms and How Pooling Saves Hours

Background

The author, a software architect, wanted to understand the exact cost of creating a database connection in a web application, focusing on the network, CPU, and memory resources involved.

Analysis

MySQL was chosen because its open‑source protocol allows a full inspection of the handshake process. The test uses Java as the client language, but the methodology applies to any language.

Java Connection Code

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 closed

Wireshark was used to capture the entire TCP and MySQL protocol exchange.

The capture shows that MySQL uses a binary protocol over TCP. The connection establishment consists of the following steps:

Step 1: Establish a TCP connection via the 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 begins.

After authentication, the client sets session variables (character set, autocommit, etc.) and exchanges several packets before any SQL statements are executed.

In this minimal example the program terminates without calling Connection.close(), causing the process to exit and the TCP connection to be reset (RST).

Measured time from the start of the TCP handshake to the forced termination (excluding the final RST) is: 10.416042 - 10.190799 = 0.225243s = 225.243ms This indicates that a single connection creation costs roughly 225 ms under ideal conditions.

Closing the Connection Properly

When the connection is closed explicitly with Connection.close(), the shutdown follows the MySQL protocol and then 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();

The measured time for a complete open‑and‑close cycle is: 747.284311 - 747.100954 = 0.183357s = 183.357ms Even with a proper close, the latency remains 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 perform 100 000 connections per day, consuming:

100000 * 150ms = 15000000ms = 15000s = 250min = 4.17h

Thus, connection establishment alone can consume over four hours of CPU time per day, making connection pooling essential. Additional techniques such as caching, prepared statements, and load balancing are also recommended.

Conclusion

Database connections are expensive; avoid creating them for every request and use a connection pool to keep latency low and resource usage efficient.

JavaPerformanceMySQLWiresharkConnection Poolingdatabase-connection
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.