Question

I have Unicorn, Sidekiq and Postgres setup.

I am trying to understand the right configuration to set up so that I don't hit the maximum db connection limit. In Opsworks, the m1.small Postgres RDS instance can have a max of 121 connections.

I have a db pool size of 5.

Consider this. Sidekiq and Unicorn are its own process. So the db pool size for each process is 5. Correct me if my understanding here is wrong.

If I have 5 unicorn process', that means 5*5=25 database connections

Now this is the part where I am slightly confused, since Sidekiq is multithreaded. If Sidekiq has a concurrency of 5. and the db pool size is also set to 5. Does that mean 25 potential db connections at a given time too?

This means, for one instance, I could have 50 db connections?

Was it helpful?

Solution

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.

In Sidekiq, the connections in the pool are shared across threads, so you need to have at least one connection available per worker. If you have a concurrency of 5, then your pool needs to be at least 5.

Having a pool size greater than 1 means each Unicorn worker has access to connections it can't use, but it won't actually open the connections, so that doesn't matter.

The total number of actual connections your app requires, unless you're using threads in your application code (and they don't share a db connection), is one per Sidekiq worker plus one per Unicorn worker.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top