Question

I recently discovered this line in my Gemfile:

# Use unicorn as the app server
# gem 'unicorn'

I have 2 questions.

  1. Why would I want to use unicorn over the default WEBrick?

  2. How do I get it to work? I uncommented that line, ran bundle install and then rails server and it still booted up WEBrick

Was it helpful?

Solution

Reasons why you would use Unicorn instead of WEBrick?

  1. Unicorn is supposed to be faster than WEBrick
  2. You can spawn multiple processes
  3. If you are using unicorn in production. You would want your development env to be as close as production.

How to run unicorn locally?

  1. Uncomment gem 'unicorn' in Gemfile
  2. Create unicorn.rb file in config/ and add the following line. You can increase the number of processes if you want to

    worker_processes 1

  3. start unicorn using the following command

    unicorn -c config/unicorn.rb

OTHER TIPS

  1. While this is mostly an opinion answer, Unicorn supports multiple "worker" processes to handle concurrent web requests by executing one instance of Unicorn. The number of worker processes you can run depends on the specs of the hardware, but generally 3-4 workers is safe for small servers and even development machines. You'd need multiple WEBrick processes for concurrent requests. I've also found Unicorn to be faster than WEBrick, especially in production applications and apps running on Heroku. Heroku actually has some really good documentation on this that is applicable outside of Heroku as well.

  2. Take a look at the Unicorn gem documentation as well as the Heroku docs above. TL;DR - you'll use the command unicorn instead of rails server to run your app using Unicorn.

You can also use the unicorn_rails gem which will override default webrick and unicorn instead

https://github.com/samuelkadolph/unicorn-rails

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top