Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use preload_app with Puma on Heroku?

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:

  • When do my workers die? Will they be reaped and re-forked after a certain number of requests? How can I monitor this?
  • How do I know if my workers need fast starts?

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.)

like image 978
Max Wallace Avatar asked Dec 03 '15 12:12

Max Wallace


People also ask

Does heroku use Puma?

Heroku recommends Puma for use in scenarios where you expect slow clients.

How many requests can Puma handle?

The select system call is limited on most systems (usually up to 1024 clients / maxfd ).

How many threads does a Puma have?

In this example, you see that process puma with PID 2160 runs with 4 threads (NLWP) having the ID's 2160--2163.

Is Puma a web server or application server?

Nginx is a web server and puma is an application server. Both have their advantages, and you need both.


1 Answers

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.

like image 136
dlackty Avatar answered Oct 07 '22 15:10

dlackty