Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How rails database connection pool works

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,

like image 451
user2274074 Avatar asked Dec 18 '15 07:12

user2274074


People also ask

How does database connection pool work?

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.

What is a pooled database connection?

What is database connection pooling? Database connection pooling is a way to reduce the cost of opening and closing connections by maintaining a “pool” of open connections that can be passed from database operation to database operation as needed.

What is pool in database Yml rails?

It sets the amount of possible connections per ruby process. So in case you are threading your rails app, or you use transactions excessively.


2 Answers

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

like image 194
Raza Avatar answered Sep 28 '22 07:09

Raza


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.

like image 45
Laurens Avatar answered Sep 28 '22 06:09

Laurens