32

I am learning rails database connection pool concept. In rails application I have defined pool size of 5.

my understanding about connection pool size is as below.

  1. When server start rails automatically creates n number of connection defined in the database.yml file. In my case it will create 5 connection since pool size is 5.

  2. On every http request if there is need to access database then rails will use available connection from the connection pool to serve the request.

But my question is if I hit 1000 request at a time then most of the request will not get access to database connection because my connection pool size is only 5.

Is my above understanding about rails connection pool is right??

Thanks,

Yuki Inoue
  • 3,569
  • 5
  • 34
  • 53
user2274074
  • 991
  • 2
  • 9
  • 25

2 Answers2

34

Purpose:
Database connections are not thread safe; so ActiveRecord uses separate database connection for each thread.

Limiting factor:
Total database connections is limited by the database server you use (e.g Posgres: default is typically 100 or lesser), by your app server's configuration (number of processes/threads available) and Active Record's configuration : Connection Pool defaults to 5 .

Pool size:
Active Record's pool size is for a single process. A thread uses a connection from this pool and releases it automatically afterwards. (unless you spawn a thread yourself, then you'll have to manually release it). If your application is running on multiple processes, you will have 5 database connections for each of them. If your server is hit by 1000 requests concurrently, it will distribute the requests among these connections, when it gets full, rest of the requests wait for their turn.

Read more at:
https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html

Raza
  • 2,320
  • 2
  • 22
  • 32
  • 1
    so if my web server is single process then there will be only one connection pool and if it is multiple process then total number of connection pool will be equal to the number of process. right?? – user2274074 Dec 18 '15 at 08:35
  • 1
    yes, right. But normally, application should be based on multiple processes/threads. – Raza Dec 18 '15 at 08:37
  • if two process are running simultaneously say Process 'A' and Process 'B' then there will be two separate connection pool . if suppose process 'A' creates two threads and if those threads needs to access the database then which connection pool those threads will use. connection pool created by Process 'A' or Process 'B'. – user2274074 Dec 18 '15 at 08:46
  • ofcourse process A, actually by pool 5, it means.. each connection is available on different thread of the same process. – Raza Dec 18 '15 at 08:49
  • what if my server is multi threaded??. i guess in this case i will have only one connection pool irrespective of number simultaneous HTTP connection. – user2274074 Dec 18 '15 at 08:57
  • Does every http request creates new process?? – user2274074 Dec 18 '15 at 10:01
  • @user2274074 share any good documentation related to processes & connection pool. Add these to your answer. it will help a lot (Y) – Waleed Arshad Jul 26 '20 at 18:40
14

Yes, from the docs:

A connection pool synchronizes thread access to a limited number of database connections. The basic idea is that each thread checks out a database connection from the pool, uses that connection, and checks the connection back in. ConnectionPool is completely thread-safe, and will ensure that a connection cannot be used by two threads at the same time, as long as ConnectionPool's contract is correctly followed. It will also handle cases in which there are more threads than connections: if all connections have been checked out, and a thread tries to checkout a connection anyway, then ConnectionPool will wait until some other thread has checked in a connection.

source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html

If you use something like unicorn as http server:

In Unicorn each process establishes its own connection pool, so you if your db pool setting is 5 and you have 5 Unicorn workers then you can have up to 25 connections. However, since each unicorn worker can handle only one connection at a time, then unless your app uses threading internally each worker will only actually use one db connection.

Laurens
  • 2,420
  • 22
  • 39
  • http://stackoverflow.com/questions/34251081/rails-does-every-http-request-creates-a-new-connection-pool see this link it states that every worker process creates creates a new connection pool. – user2274074 Dec 18 '15 at 08:27
  • 1
    Well, if you use something like unicorn: In Unicorn each process establishes its own connection pool, so you if your db pool setting is 5 and you have 5 Unicorn workers then you can have up to 25 connections. However, since each unicorn worker can handle only one connection at a time, then unless your app uses threading internally each worker will only actually use one db connection. – Laurens Dec 18 '15 at 08:34
  • what about passenger. i am using passenger with ngnix. – user2274074 Dec 18 '15 at 08:38
  • i guess passenger is similar to unicorn. passenger creates multiple worker process(app instances). – user2274074 Dec 18 '15 at 08:40
  • 1
    You can check this with 'passenger-status', for more info read: https://www.phusionpassenger.com/library/walkthroughs/basics/ruby/process_management.html – Laurens Dec 18 '15 at 08:45
  • Thanks, will check and get back to you. – user2274074 Dec 18 '15 at 09:00