Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use database connections pool in Sequelize.js

I need some clarification about what the pool is and what it does. The docs say Sequelize will setup a connection pool on initialization so you should ideally only ever create one instance per database.

var sequelize = new Sequelize('database', 'username', 'password', {   host: 'localhost',   dialect: 'mysql'|'mariadb'|'sqlite'|'postgres'|'mssql',    pool: {     max: 5,     min: 0,     idle: 10000   },  // SQLite only    storage: 'path/to/database.sqlite' }); 
like image 362
Mark A Avatar asked Feb 20 '16 16:02

Mark A


People also ask

How does Sequelize connection pool work?

Configuring the Connection Pool Size in Sequelize This means that your application will use no more than five connections at a time, no matter how many requests it gets. As explained before, if six users concurrently hit any of your APIs that need to connect to the database, one of them will be queued and have to wait.

How do I connect multiple databases in Sequelize?

Sequelize will setup a connection pool on initialization so you should ideally only ever create one instance per database if you're connecting to the DB from a single process. This means, that we will need to run a new Sequelize instance for every database we want to connect to our server.

What is connection pooling and why it is used?

Connection pooling means that connections are reused rather than created each time a connection is requested. To facilitate connection reuse, a memory cache of database connections, called a connection pool, is maintained by a connection pooling module as a layer on top of any standard JDBC driver product.


1 Answers

When your application needs to retrieve data from the database, it creates a database connection. Creating this connection involves some overhead of time and machine resources for both your application and the database. Many database libraries and ORM's will try to reuse connections when possible, so that they do not incur the overhead of establishing that DB connection over and over again. The pool is the collection of these saved, reusable connections that, in your case, Sequelize pulls from. Your configuration of

pool: {     max: 5,     min: 0,     idle: 10000   } 

reflects that your pool should:

  1. Never have more than five open connections (max: 5)
  2. At a minimum, have zero open connections/maintain no minimum number of connections (min: 0)
  3. Remove a connection from the pool after the connection has been idle (not been used) for 10 seconds (idle: 10000)

tl;dr: Pools are a good thing that help with database and overall application performance, but if you are too aggressive with your pool configuration you may impact that overall performance negatively.

like image 79
P Ackerman Avatar answered Oct 12 '22 20:10

P Ackerman