Really need a db connection pool for unicorn rails

2020-06-16 04:07发布

问题:

I can't find any document describing database connection pooling effect for unicorn.

Unicorn forks several worker processes. I configured prefork and it's critical not to share database connections between workers, so I reset db connections after fork.

My rails application has 8 workers per server, and the pool size in database.yml is 5, then I saw 45 connections to mysql.

Each worker is single-threaded that handles 1 request at a time. SQL queries should be blocking. Seems the other 4 connections are useless? Can I set the pool size to 1 for better performance?

回答1:

Since each worker can only serve 1 request at a time, each worker can also use only one connection at a time, and nothing is gained from having more connections. If you set the pool size to 1, each Unicorn worker should open one connection. You will likely not get a noticeable performance increase but you will save resources by having fewer open connections.