How rails database connection pool works

前端 未结 2 1669
无人及你
无人及你 2021-02-07 05:04

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.

2条回答
  •  独厮守ぢ
    2021-02-07 05:33

    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

提交回复
热议问题