Production-level NODEJS development practices-using connection pooling

Source: Internet
Author: User
Tags mysql connection pool

Introduction

Doing back-end development inevitably has to 存储服务器 deal with some, and 消息服务器 so on.

Cause (traditional mode, read database)

Everyone knows that passing data to a service that uses a TCP connection must open a连接-connection

For example, we open a database and execute a SQL, which is usually

  1. connection = open "mysql://127.0.0.1:3306/db"(Open the database and get a handle to the connection)
  2. data = connection.exec "select * from table1"(Execute SQL and get data)
  3. connection.close()Explicitly close the connection

There seems to be nothing wrong with the way it looks.

However, for sites with a slightly larger number of concurrency. The operation of a unit batch needs to be opened and closed once a connection ...
This is obviously unacceptable.

Is it unacceptable to do so?

It takes three handshake to establish a TCP connection. You also need to assign system resources and memory space to objects. So creating a TCP connection can be said to be expensive.

Go back to the topic just now. This trivial turn-off connection not only increases the pressure on the client Io, most importantly increases the pressure on the TCP server (MySQL, Redis).

So let's try another way, that is, not closing the connection, and always using the connection. The answer is: yes. This way is called 长连接 .

That, too, has a problem. Because a connection can only do one thing over a period of time. This obviously blocks the entire system in the case of concurrency.

So can we try to create multiple connections, and then, when called, use a connection that is not in use, and when it is finished, put the connection back in a way that other callers can use ?

Connecting the big Butler (connection pool)

The answer is: That's what we're going to say 连接池 . Of course a sound connection pool does not just complete the functions I have described above.

  • Connection preheating (automatic opening of n connections for use at startup)
  • Use for example to 轮转法 distribute connection requests evenly
  • Dynamically generate new connections when a connection in the pool is about to run out
  • Automatically frees connections when a connection in the pool has not been called for a period of time
  • Automatically discard the connection that has broken down
  • Automatically release all connections when the system shuts down
    ........

These are the connection pooling features. 连接池we are管理连接的管家

Connection pooling in node. js

There seems to be no point in speaking of this. What should we do in node?

We can use node-pool this module GitHub

1. 安装     npm install generic-pool
2. Create a connection poolCreate a MySQL connection poolvar poolmodule =Require' Generic-pool ');var pool = Poolmodule.pool ({Name:' MySQL ',A connected handler create will be built:functionCallback) {var Client =Require' MySQL '). Client;var C = new Client (); c.user =  Scott ‘; C.password =  ' mydb '; C.connect (); Callback ( null, c); }, //release a connected handler destroy:  function (client) {client.end ();}, //connection pool maximum number of connections Max: 10, //the minimum number of connections in the connection pool min: 2, //If a thread has not been used in 3 seconds. Then release Idletimeoutmillis: 30000, //if set to true, use Console.log print into the job, of course you can pass a function most as logging handler log: true});     
3. Get the link from the connection pool and useThe default is no task priority, but as with high priority, in the forefront of the race queue Pool.acquire (function (err, client) { Pool.release (client); }); //high priority to get links in the forefront of the competition queue Pool.acquire ( function (err, client) {pool.release (client);}, 0); //Medium priority Get link pool.acquire ( Function1); //Pool.acquire (Handler, priority) method accepts a parameter //handler: Gets the connection's back function //priority: Gets the competitive precedence of the link //pool.release (client) method will put the link back into the connection pool, When the obtained link is not release. Will cause the link to be occupied                

More related configurations I will not start the discussion. You can go to GitHub to see the readme.md of the project.

"Production level NODEJS development practice-strong node process container PM2"-Stay tuned

Note:
存储服务器: (Database MySQL ..., cache memcached, Redis ...)
消息服务器: (Rabbitmq,activemq ...)

    • Published on December 18, 2014
    • More

Production-level NODEJS development practices-using connection pooling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.