On Sat, 25 Aug 2007, dtuttle1 / gmail.com wrote:

> I'd like to learn more in this area too, but here are my thoughts:
> The web servers, at least mongrel, are single-threaded. Mongrel queues
> requests and feeds them to the app sequentially. To get concurrency
> you have to run multiple instances of mongrel. In this situation there
> are no thread safety issues because there's only one thread per
> process.

This is untrue.

The standard Mongrel is threaded.  It creates a new thread of execution 
for each connection that it receives, and those execute in parallel with 
each other and the main Mongrel thread, which is essentially just an 
accept() loop that receives the requests and spawns handler threads for 
them.

The Rails mongrel handler has a mutex that locks the action within it to a 
single thread of execution at a time.  So, if 10 requests come in at the 
same time, Mongrel will create 10 threads of execution for those 10 
requests, but when execution flow reaches the Rails handler, each thread 
will stand in line at the mutex gate and proceed through it in single 
file.

In a standard Mongrel handler, which does not have a mutex at the front of 
it, the requests are processed concurrently.  This is the normal 
situation.

Remember that Ruby threads, being green threads, are all in the same 
process, so there is no actual concurrency of execution between them.  In 
most cases these threads will not increase your throughput.


Kirk Haines