socketry / falcon-rails-example

25 stars 1 forks source link

Isolation level and Active Record Connection Pool #2

Closed trevorturk closed 7 months ago

trevorturk commented 11 months ago

I haven't tested in a while, and please feel free to ask me to do my own research, but I'm curious if there's anything else that likely needs configuring after setting config.active_support.isolation_level = :fiber in terms of Active Record's connection pool.

In my case when I tested last I was connecting to Postgres but didn't have pgBouncer enabled and I was overwhelmed with connections once I flipped it on.

Currently, with isolation_level left on the default :thread I have this config/database.yml:

default: &default
  adapter: postgresql
  pool: <%= ENV.fetch("DATABASE_POOL_SIZE", 5) %>

...but since I may have many more fibers running than threads, I'm concerned that the pool is effectively uncapped in real world usage. Is there some way to cap the pool size to the number of Falcon processes running or something like that? IIRC last time I inquired about this, we suspected that we might need to use an Async::Semaphore to limit connections, but I never did try to figure that out.

I suspect this might impact other Falcon + Rails users, so perhaps it's a good thing for us to try to sort out and document? Thank you!

j-manu commented 8 months ago

It's also very easy to run out of connections if you have long running requests as Rails doesn't checkin a connection until a request completes. You will have to wrap all your code which accesses DB with with_connection to avoid this.

trevorturk commented 8 months ago

Ah, good to know!

trevorturk commented 7 months ago

Closing since we have better documentation in https://github.com/socketry/falcon/pull/220