Web server and Application server
Tips about servers for Ruby on Rails development
Recent web applications are composed of 3 layers:
- Web server
- Application server
- Database
Long time ago web server and application server are played by the same server ( thus they call it web 2 layers ).
Web server
Web server refers to server software, or hardware dedicated to running said software, that can serve contents to the World Wide Web.
A web server processes incoming network requests over HTTP and several other related protocols. The primary function of a web server is to store, process and deliver web pages to clients. The communication between client and server takes place using the Hypertext Transfer Protocol (HTTP). Pages delivered are most frequently HTML documents, which may include images, style sheets and scripts in addition to the text content.(Wikipedia)
You can see which web server is used by inspecting your web page( Ctrl + option + I
)and see your network tab.
Application server
An application server is a software framework that provides both facilities to create web applications and a server environment to run them.
[…]their main job is to support the construction of dynamic pages. However, many application servers target much more than just Web page generation: they implement services like clustering, fail-over, and load-balancing, so developers can focus on implementing the business logic.[…]
Application servers consist of web server connectors, computer programming languages, runtime libraries, database connectors, and the administration code needed to deploy, configure, manage, and connect these components on a web host.(Wikipedia)
How they work together
Basically, a web request will be sent to your web server first. There the web server passes it off to the app server after some processes. The app server uses Rack to fire your Rails app. Then your app returns the response back through the app server and the web server to the browser of the user.
For example, Apache passes a HTTP request to Puma, then Puma put some process on it before sending it to Rails App(or Sinatra app). If you use Rails app router receive it and call appropriate method of a specific controller.
How to see what is used in my Ruby on Rails app?
It might be too straight forward to answer this question, but just run rails server
and see what happens.
When you’re using Puma, it looks like:
$ rails s
=> Booting Puma
=> Rails 5.2.1 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.4.4-p296), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
And when you use WEBrick, here:
$ rails s
=> Booting WEBrick
=> Rails 5.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
[2019-01-27 11:38:57] INFO WEBrick 1.3.1
[2019-01-27 11:38:57] INFO ruby 2.4.4 (2018-03-28) [x86_64-darwin17]
[2019-01-27 11:38:57] INFO WEBrick::HTTPServer#start: pid=87024 port=3000
For unicorn users, it will return
$ rails s
=> Booting Unicorn
=> Rails 5.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
I, [2019-01-28T01:30:41.038456 #93979] INFO -- : listening on addr=[::1]:3000 fd=24
I, [2019-01-28T01:30:41.038648 #93979] INFO -- : worker=0 spawning...
I, [2019-01-28T01:30:41.039986 #93979] INFO -- : master process ready
I, [2019-01-28T01:30:41.041601 #94006] INFO -- : worker=0 spawned pid=94006
I, [2019-01-28T01:30:41.042255 #94006] INFO -- : worker=0 ready
Note that when you use unicorn
gem instead of unicorn-rails
, rails s
will just return WEBrick. (Run unicorn
in your terminal when you use unicorn
gem)
Rails says rails server
command by default run Puma(in their Github source ).
### `rails server`The `rails server` command launches a web server named Puma which comes bundled with Rails. You'll use this any time you want to access your application through a web browser.With no further work, `rails server` will run our new shiny Rails app:```bash
$ cd commandsapp
$ rails server
=> Booting Puma
=> Rails 5.1.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.0.2 (ruby 2.3.0-p0), codename: Plethora of Penguin Pinatas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
```
What to do to deploy your app to Heroku
Configuration is always painstaking, but about servers too, we are recommended to declare a specific web server in our app. Here I write some sample case of deploying Heroku.
Ruby includes by default a web server called WEBrick. So when you deploy a Ruby application without Procfile
, WEBrick will be used. It is useful in a sense that it doesn’t cost you much for configuration in dev/test mode.
But you shouldn’t use WEBrick as a web server in production, because it does not support concurrent thread ( It works single thread/process only).
This is why we are recommended to set some other web server explicitly in Procfile
(maybe you need to create it first at the root of your app)like:
# Procfile# use Puma
web: bundle exec puma -C config/puma.rb# use Unicorn
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
Of course, you need other configuration when you deploy other platforms than Heroku.