matsko
matsko

Reputation: 22183

Heroku and Rails file uploads

I've read a lot of areas online that talk about file uploads in Rails cause the web application to hang and I was wondering if this is the case with a Heroku Dyno? If a user was to upload a 1 gig file, then would this cause the rest of the application to hang if it were hosted on a single dyno?

Upvotes: 1

Views: 483

Answers (1)

agronemann
agronemann

Reputation: 687

By default Rails is not thread safe. That means each Rails instance running can only handle a single request at a time.

On Heroku you can run 1 process on a single dyno. That could be Thin, Unicorn, a delayed_job worker, etc.

Since Rails is not thread safe by default, Thin can only handle 1 request at a time (which means your dyno can only handle 1 request at a time). In that case, when a user uploads 1 GB file, then Thin can only handle that request = causes next requests to hang. To solve this you would have to scale the amount of dynos. Forexample running 4 dynos (running Thin), and you can handle 4 requests at a time.

However if you use Unicorn on your Heroku dyno, and set the Unicorn worker amount to 4, then Rails can handle 4 requests at a time. Unicorn is smart. It spawns 4 processes inside that single Heroku dyno. That means when you upload a 1 GB file, then Unicorn continue and handle 3 requests at that time. And this is all running on a single dyno.

Check out this article for more info on Heroku and Unicorn: http://michaelvanrooijen.com/articles/2011/06/01-more-concurrency-on-a-single-heroku-dyno-with-the-new-celadon-cedar-stack/

Hope this helps.

UPDATE

If you upload files to S3, you can actually skip your dynos completely and upload the files directly to S3. Just a tip. ;-)

Upvotes: 2

Related Questions