38

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'
});
1

3 Answers 3

64

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Nice answer! With this idle configuration, if my query does more than 10 seconds to execute, is the connection closed without me getting a response?
no, idle is literally how long the connection will sit there doing nothing. what you're talking about is the pool.timeout setting. which i think defaults to 30s. after that the query times out and you'll get an error.
Nice answer sir
@steev the current docs as well as those of v4 don't talk about pool.timeout. I also can't find pool.timeout in the source code. See stackoverflow.com/a/62697070/4417769 for something like that on postgres
@sezanzeb If I recall correctly I think that option is a default of "generic-pool", the package that sequelize4 uses to do its pooling. if you can't find mention of it in sequelize code then it might never actually reset it? latest sequelize, btw, uses sequelize-pool instead, it seems, and i have no idea about that. these are guesses, tho, it's been a long time since i looked at this stuff...
|
7

pool is draining error

I found this thread in my search for a Sequalize error was giving my node.js app: pool is draining. I could not for the life of me figure it out. So for those who follow in my footsteps:

The issue was that I was closing the database earlier than I thought I was, with the command sequelize.closeConnections(). For some reason, instead of an error like 'the database has been closed`, it was instead giving the obscure error 'pool is draining'.

1 Comment

Thank you for taking the time to post this. In the end, this was my problem, too. Coupled with tricky forEach
0

Seems that you can try to put pool to false to avoid having the pool bing created. Here is the API details table :http://sequelize.readthedocs.org/en/latest/api/sequelize/

[options.pool={}] Object Should sequelize use a connection pool. Default is true

3 Comments

can you go into more detail about the pool and the pool object
wouldn't recommend as it can heavily affect performance of your applicaton
Error: Support for pool:false was removed in v4.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.