Lighty+Mongrel: Limiting persistent db connections?


#1

I’m not sure if it’s due to setting up multiple mongrel servers and
proxying them with lighty, but something is keeping postgres connections
around. I only have a couple Rails apps running on a new server, with
not a whole lot of traffic, yet today I actually maxed out my connection
limit of 100 for postgres. How do I turn off persistent connections in
Mongrel?

Joe


#2

Joe,

Each mongrel you run takes a -n setting (-P on win32 though) that sets
the
number of processor threads. The way ActiveRecord works is that you get
one
DB connection for each thread using AR. Since the default is 20
processor
threads then you’ll get 20 DB connections per mongrel setup.

The next release will have an option to turn this off for AR so that
there’s
one DB connection for each Mongrel.

Still, it would better if AR just had a real LRU connection pool. :slight_smile:

Zed A. Shaw


#3

Ah, thanks. I decreased -n and that seems to have improved the
situation.

Is there any advantage in setting -n to greater than 1 when used with
Rails?

Joe


#4

Yes, even though Rails runs in a synchronized block, the entire rest of
Mongrel tries to be thread safe. So, if you want to be able to have
mongrel
deal with more than one connection at a time then keep it above 1.

The catch is that if you increase it too much then the threads fight
over
the socket queue and things start to get slower. 20 was about the right
setting, but YMMV.

Zed