Search code examples
ruby-on-railsrubymemorynginxrbenv

The memory optimal setting of rails app (on Phusion passenger/nginx)


I have a rails app using renv/Phusion Passenger/nginx on Ubuntu. My server has a small memory size, and the rails app is rarely invoked, so I need to find a memory optimal configuration on Phusion passenger/nginx.

Setup nginx.conf

I tried to minimize the memory usage, so I keep only 1 pool, and tried to minimize the idle time. I got information from this site: https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_pool_idle_time

user www-data;
worker_processes 1;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    ...
    ##
    # Phusion Passenger config
    ##
    # Uncomment it if you installed passenger or passenger-enterprise
    ##

    passenger_max_pool_size 1;
    passenger_pool_idle_time 1;
    passenger_root /home/ubuntu/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.0.21;
    passenger_ruby /home/ubuntu/.rbenv/shims/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Setup rails.conf

I have this configuration to rails.example.com is connected to a rails app.

server {
    listen 80;
    server_name rails.example.com;
    passenger_enabled on;
    passenger_app_env development;
    root /home/ubuntu/webapp/rails/passenger-ruby-rails-demo/public;
}

Checking the memory usage with htop, I have this map.

enter image description here

However, the information shown is not exactly the same as the one from sudo passenger-memory-status command.

enter image description here

The htop report has four Passenger RubuApps (with 3317, 3319, 3314, 3320 pids), but passenger-memory-status only shows 3314 pid. The same is true with other processes (core, watchdog, ust-router). I think htop shows the correct memory usage.

What makes the differences in memory usage information? I'm also curious about

  1. How to minimize the memory usage of Phusion passenger with nginx?
  2. Is there a way to kill passenger processes after a certain amount of time when there is no rails app invocation?
  3. Other than Phusion passenger, what might be the way to use least amount of memory?

EDIT

Modifying the setup of htop to show in tree format and show threads in different color results in showing one process.

enter image description here

EDIT2

Passenger AppPreloader consumes the memory as much as RubyApp does. enter image description here

After killing the AppPreloader, the memory consumption is smaller, and the app seems to be working fine.

enter image description here

I'm not sure if AppPreloader is absolutely necessary (for optimal memory usage) and is there an option not to use it.


Solution

  • Passenger author here. It is in fact passenger-memory-stats that shows the most correct measurement, not htop. See https://www.phusionpassenger.com/library/indepth/accurately_measuring_memory_usage.html for more information.

    Furthermore, htop not only displays processes, but also threads. That is the reason why you see 4 entries in htop and 1 in passenger-memory-stats is because. Thus, passenger-memory-stats really is the most accurate.

    As for your how to shutdown an app after a certain amount of time: Passenger already does that by default, but you mean want to tweak things a little. Have a look at these two config options:

    Unicorn does not use less memory than Passenger. They are about the same because most memory is occupied by Rails and by the app, so it does not really matter. Passenger has a ton more features, tooling and documentation than Unicorn too.