Why rails 5 using puma instead of webrick for development purpose?
Asked Answered
C

3

10

I tried to find out the difference between the Puma and Webrick, but didn't get it or satisfied with it.

So could any one please share information regarding it.

Castanon answered 5/4, 2018 at 14:10 Comment(2)
"why" - puma is a better/faster server. That's a good enough reason. (also you have a production-grade server from the start and don't need to use another server for production env)Species
@SergioTulentsev Do you have any benchmarks backing your claim that Puma is faster? puma.io only compares it to other (non WEBrick) servers.Terminate
C
11

By default WEBrick is single threaded, single process. This means that if two requests come in at the same time, the second must wait for the first to finish.

The most efficient way to tackle slow I/O is multithreading. A worker process spawns several worker threads inside of it. Each request is handled by one of those threads, but when it pauses for I/O - like waiting on a db query - another thread starts its work. This rapid back & forth makes best use of your RAM limitations, and keeps your CPU busy.

So, multithreading is achieved using Puma and that is why it is used as a default App Server in Rails App.

Cattegat answered 5/4, 2018 at 14:17 Comment(6)
The most efficient way to do slow I/O is evented I/O. Evented servers can handle tens of thousands of connections (unlike threaded servers)Species
@Cattegat is it also the default server for Rails in production?Highborn
Yes..from rails 5 onwards it is the default server for development and production but you can use other servers for production as wellCattegat
Webrick has always been a multi-threaded server, you can check in server.rb codebase and in git history (create_thread method call). I filed an answer correcting this.Fanelli
Webrick is not multithreaded as it runs in a single ruby process and ruby itself is not multithreaded. So many pretty bad comments here, scary. Evented I/O has nothing to do with this problem of "slowness" at all.Achorn
Words. Webrick IS multi threaded, it DOES use multiple UNIX pthreads. The global lock does not allow parallelism but it indeed DOES allow execution of other threads while the others are waiting for I/O. Multi-threaded Ruby web server WILL perform better than a single-threaded server. These are all FACTS.Fanelli
F
6

This is a question for Ruby on Rails developers rather than broad audience, because I don't understand reasons any other that putting development environment closer to production where Puma is a solid choice.

To correct the current answer however, I must say that Webrick is, and always has been, a multi-threaded web server. It now ships with Ruby language (and also a rubygem is available). And it is definitely good enough to serve Rails applications for development or for lower-scale production environments.

On the other hand it is not as configurable as other web servers like Puma. Also it is based on the old-school new thread per request design. This can be a problem under heavy load which can lead to too many threads created. Modern web servers solve this by using thread pools, worker processes or combination of the two or other techniques. This includes Puma, however for development spawning a new thread per request is totally fine.

I have no hard feelings for any of the two, both are great Ruby web servers and in our project we actually use them both in production. Anyway, if you like using Webrick for RoR development, you indeed can still use it:

rails server webrick
Fanelli answered 1/10, 2020 at 6:34 Comment(3)
You suffer from the ruby terminology here. Ruby threads are not CPU threads. They are fibers. They do not allow parallelism.Achorn
I guess your comment was an April joke. Threads in Ruby are indeed normal threads: ruby-doc.org/core-2.5.0/Thread.html implemented as UNIX pthreads: github.com/ruby/ruby/blob/master/include/ruby/thread_native.h and it is worth noting that Fibers are also available: ruby-doc.org/core-2.5.0/Fiber.htmlFanelli
There is still the GIL/GVL problem, however in this context (development box) threads will provide good enough environment. Whole point of my OP is that webrick is often incorrectly marked as single-threaded. That is not the case.Fanelli
C
0

Rails 6.1 Minor update:

rails server -u webrick [-p NNNN]
Chare answered 12/1, 2021 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.