You really know how to set the size of the database connection pool yet

Some time ago in an old project experienced a problem: a Dubbo service, starting when slow death, look after the entire process log to check the cause has been connected to initialize the database. See a database connection parameters, connection pool size: 1024.

Many late entry students have not experienced life handwritten JDBC connection. At that time no concept of database connection pool, are native code meal out, and later with the degree of complexity in Java after iBATIS gradually reduced, also derived from the database connection pool C3P0 thing that basis. Rome was not built in a day, but the Internet is developing too fast, under a variety of middleware technology pressure forced forced to research and development, we work overtime to get out on a variety of tall scaffolding, but also the achievements of many great men .

Database connections using TCP way, 3-way handshake to establish a connection needs to release the connection requires 4 times waving today at this frequency of Internet use, if every time you access the database connections are re-established, I guess you are not enough companies closed down 800 times.

1. The process is like a database connection

Java originator Sun Microsystems is a set of API wants to unify the world, but since each database server vendors not too much to force reunification. Upset is the creation of a unified interface, unified access of a proposed steps, each vendor implements the interface, follow the steps to load your own database. So now the program is 4 tricks:

  1. Registration drive, Class.forName()known: ;
  2. Gets Connection, namely the establishment of a successful connection to the database;
  3. Get Statement object, CRUD operations for the database;
  4. Get the database to return results ResultSet.

We should all know that the database itself is a client program only started to connect. Take MYSQL For example, we installed and started on the service machines, the command line input: mysql -uroot -pto connect the current database.

MYSQL connection there are many ways to distinguish between Unix and Windows systems and common connections, said here only two ways: one is unix domain socket, the other is based on tcp/ipthe agreement, we generally if remote access to the database must be based on tcp/ip, but If we will use the socket or into tcp / ip the machine to log in.

socket:mysql -uroot -p
tcp/ip:mysql -h127.0.0.1 -uroot -p

When the database server and application server are on different hosts must establish a connection using tcp / ip manner. Each connection takes up one thread in the operating system to maintain. To establish a connection it is also divided into two categories: short and long connection connection.

Short connection

Refers to the so-called short connecting connection is closed after completion of the communication and database applications. This connection is for every operation:

发出请求--->建立连接--->操作数据--->释放连接

The problem with this is:

  1. Frequent establishment / release connection to the database is increased burden on the system;
  2. Each operation process database application will become very slow;
  3. Every application to establish a connection must occupy a port, frequent establishment / release, each connection is not released immediately executed after issuing a release request must go through a FIN wait until the confirmation stage. So when thousands of requests per second database, application server port is likely to be consumed.

Long connection

That connection has been open long after the connection is established, until the application is closed before the release. The benefits of using long connection is to reduce the overhead of the connection each time you create.

For application servers to maintain the benefits of long connection speaks for itself, but for the database server, the connection is too long disaster.

MYSQL TCP connection supports long connection, so every database operation is complete, you can not turn off the direct connection, but while waiting for the next use in this connection multiplexing. All long-Socket connections are maintained by the built-in TCP ping heartbeat (TCP keep alive), so as to stay connected, and we are familiar with websocket, it is also used to maintain the TCP connection is not interrupted by a heartbeat.

connection pool

The benefits of a long connection so big, naturally we all use long connection. Slowly come up with a set of tools to maintain a long connection - a database connection pool.

Nor how complex design connection pool, general step is to:

  1. Initiates the connection;
  2. Remove the service connection;
  3. Traffic transmission request;
  4. Replace the connection.

In addition to the basic functions of the above, but also to deal with concurrency issues, multi-server and multi-user database, transaction processing, connection configuration and maintenance of the pool. Probably these functions. Once you have a pool, and establish business connection with the release of no relation to the transfer pool to maintain.

2. MYSQL can support the number of connections

MYSQL default maximum number of connections is in version 5.7 151, can be up to 16,384 (2 ^ 14). How to set the maximum number of connections that your server performance, see the MYSQL connection number information command is as follows:

mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 5050  |
+-----------------+-------+
1 row in set (0.00 sec)

Our production environment MYSQL maximum number of connections is set to 5050, attention can not be set too small, the consequences are too small to cause connection failure: "query failed Error 1040: Too many connections" error. Too large and when the machine is connected to the database will be more of an impact on the current MYSQL performance.

MYSQL official website shows the proportion of a recommendation to set the maximum number of connections:

Max_used_connections / max_connections * 100% ≈ 85%

The total number of connections already use about 85 per cent cap, too large if the number of connections and the maximum number of connections currently using the ratio is less than 10% of it is very clear that setting.

Query the current database connections established:

mysql> show status like 'Threads_connected';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_connected | 89    |
+-------------------+-------+
1 row in set (0.00 sec)

Mysql query and configuration can be set in a global variable, mainly related configuration can query these below:

Configuration meaning
Connections Mysql number of connection attempts, whether or not the connection is successful, the value will be +1
Threads_connected The number of connections, a single node has established the connection pool is generally less than the maximum number of connections Maximum
max_connections The maximum number of connections can be limited Mysql
wait_timeout That maximum duration of long life MYSQL connection (non-interactive), the default is 8 hours
interactive_timeout Long duration connection (interactive) of maximum health, the default is 8 hours

3. how many connections the connection pool setting is appropriate

Set the connection pool size is certainly not the bigger the better, you need to consider is the current performance of the machine's service, network conditions, the performance of the database machine, database features and so on. But also do not waste system resources, memory, ports, and so the amount of synchronization signal.

For example, the maximum thread pool Default application server Tomcat setting the value of 200, the maximum is assumed that each thread will use a database connection, the thread pool size should be less than 200.

Another consideration is that each application for a long connection will be built on a physical network connection for the maintenance of the long process, and the number of CPU cores to perform with a physical machine processes relevant. Theoretically a server connected to the core 8 is set to 8 best pool, each of the cores simultaneously processing one thread, there are more than eight concurrent thread context switching overhead.

Here's a short video released by Oracle performance team, connection pooling test was divided into two parts: test video 1 , the test video 2 . Video adjust the thread pool size of the database performance of 2048, when a sharp decline, adjusted back to 144 was restored. PostgreSQL provides a thread pool to set the expected size of the formula:

connections = ((core_count * 2) + effective_spindle_count)

The formula comes from: https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing .

Among them, core_countthe CPU core, effective_spindle_countthe meaning of the effective number of spindles, if your server uses RAID 16 with a disk, then valid_spindle_count=16. It is essentially a measure of the number of parallel I / O request server can manage. Rotating a hard disk (usually) only handle one I / O request, if you have a 16, the system 16 can handle I / O requests simultaneously.

I would like to Hikari as the most outstanding one database connection pool, proposed this formula is amenable. In a production environment you may wish to try (I do not find a problem).

Guess you like

Origin www.cnblogs.com/rickiyang/p/12239907.html