[Mongrel] sendfile and mongrel

Kirk Haines wyhaines at gmail.com
Wed Dec 12 12:11:47 EST 2007

On Dec 12, 2007 8:27 AM, Nathan Vack <njvack at wisc.edu> wrote:
> Before I discovered x-sendfile, I used to handle file uploads with
> PHP -- talking to the same database as my main Rails app. PHP doesn't
> bloat when dumping files across the wire like Ruby does.

_Ruby_ doesn't.

swiftiply      1510  0.7  0.2 53300 21136 ?       Ss   Dec09  27:43
ruby swiftiply -c /etc/swiftiply.cnf

Next I retrieve a 470mb file, twice.  Then look at ps again

swiftiply      1510  0.7  0.2 53300 21136 ?       Ss   Dec09  27:56
ruby swiftiply -c /etc/swiftiply.cnf

No change in the memory footprint, despite running almost a gigabyte
of file data through it.

One should see essentially the same thing with Mongrel's send_file()
method, because it reads the file in Const::CHUNK_SIZE pieces (16k).
However...if you are using send_file() from within Rails, then Rails
has to read the entire file into the response before it ever goes back
to Mongrel, and THAT is what is causing the memory bloat.  Not Ruby.
Not Mongrel.  Rails.

I agree with the other pieces of advice that have been given, though.
Leverage Apache's x-sendfile since you are on Apache.  Alternately,
write a dedicated mongrel handler that will deal with authentication &
file delivery outside of Rails (which also means outside of the Rails
mutex lock, which is a BIG win with regard to scalability of
downloads, at least compared to doing it with send_file from inside of

Kirk Haines

More information about the Mongrel-users mailing list