On Thu, Sep 24, 2015 at 10:42:39PM -0700, Michael Vigilante wrote:
> Hi, just a small question on the way connections are managed in
> ActiveRecord.
>
> From the best I can gather reading the code (and based on behaviour),
> ActiveRecord will check a connection out of the pool the first time a
> thread asks for one, then that thread will continue to hang on to the
> connection until it is closed. What is the reasoning behind this approach
> as opposed to checking out a connection for each DB operation and letting
> go of it once the operation is completed?
Constantly checking out a connection then checking it back in seems like
it would cause a performance bottleneck. The assumption is that lock
contention will become the bottleneck if we have to constantly check in
/ check out connections. The other side of that is it will cause more
complexity throughout AR as we'll have to change every place that
touches the connection.
Doing it once per thread, and giving the user control over checking in
connections (see `release_connection` on the connection pool) seemed
like a good compromise for performance and code complexity.
> My team has had a couple of issues with this (leading to us needing to use
> a connection pool equal to the number of threads being used by our server,
> which is not really a big problem, but feels a bit counter-intuitive).
Is it counter-intuitive? If you're doing database intensive work, then
you'd probably want the same number of connections as you have request
threads in order to maximize throughput, otherwise you'll still be
blocking other requests at some point (see Amdahl's law).
> This pos
> <
https://groups.google.com/d/msg/rubyonrails-core/Z4eiyPnGjwk/21a5KSL_YlIJ>t
> is on much the same subject. I'm really just curious about the way this
> works.
If we can prove that the concerns above are unwarranted, then it would
be fine to change.
--
Aaron Patterson
http://tenderlovemaking.com/