I am running a small Rails app on Heroku with one dyno and several Puma workers.
The Puma docs say:
General rule is to use preload_app when your workers die often and need fast starts. If you don’t have many workers, you probably should not use preload_app.
This suggests that I should not use preload_app!
in my config/puma.rb
. However, I have a few unanswered questions:
It's clear that preload_app!
should save resources when using many workers, but I don't see the disadvantage of using it even with a small number of workers.
Heroku's recommended config for "a simple Rails application" includes preload_app!
but they don't offer any guidance on when not to use it.
When should preload_app!
not be used, and why? (Ignoring the phased restart issue.)
Heroku recommends Puma for use in scenarios where you expect slow clients.
The select system call is limited on most systems (usually up to 1024 clients / maxfd ).
In this example, you see that process puma with PID 2160 runs with 4 threads (NLWP) having the ID's 2160--2163.
Nginx is a web server and puma is an application server. Both have their advantages, and you need both.
Besides starting worker fast, preload_app!
also saves memory thanks to Copy on Write feature introduced in Ruby 2.0.
You can read this great write-up from Heroku introducing the idea of copy-on-write in Ruby:
Copy on Write or COW is an optimization that can reduce the memory footprint of a Ruby process when it is copied. Instead of allocating duplicate memory when a process is forked, COW allows multiple processes to share the same memory until one of the processes needs to modify a piece of information.
Thus no matter how many works you have, it's always recommended to enable preload_app!
if possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With