What’s this “Puma” thing that starts running when you do rails server?
It’s an app server!
Let me explain what an application server is & why do we need them with an example.
Understanding App Servers
Let’s say you start building your new shiny web application in Ruby.
And before you write any code…
You want to see it load in your browser, even if just to see the default “welcome” page.
So you open your browser, you point it at localhost:3000, or maybe localhost:4567 if you’re using Sinatra.
What happens then?
Your browser connects to localhost, on port3000, where a Ruby application server like Puma is waiting.
localhost points to a special IP address, 127.0.0.1, which always refers to your computer
Now:
The job of Puma, or any other rack-based server (like Thin / Unicorn), is to handle the browser’s request & pass it along to your application through a common interface.
The main reason they mention is, that if you use Unicorn – an alternative app server – you are susceptible to slow client attacks.
Also:
Puma is multi-threaded, which usually results in less memory usage.
“The operating system performs less work on behalf of a multithreaded program than it does for a multiprocess program. This translates into a performance gain for the multithreaded program.” PThreads Programming
Which Server Is Faster?
Running a simple Rack app with this command:
rackup -s thin rack-simple.ru 2>1 &>/dev/null
Tested with this:
wrk -d 20 http://localhost:9292
I got these benchmark results:
Webrick 229.97 request/second
Thin 773.20 request/second
Puma 2035.34 request/second
Out of these three, it looks like Puma gets you the best performance.
But that won’t matter if you have a slow SQL query, or if you’re looping through arrays with millions of elements on every request.
Summary
You learned about application servers in Ruby, like Puma & Thin, so you now have a better understanding of why we use them.
Which one are you going to use for your next project?
Nice article but performance wise why no Unicorn? I’d say it still deserves a mention even if you feel you need to add a little * to reference the slow client attack previously.