What's the performance impact of disabling eager_load in production.rb?

1.9k views Asked by At

My rails 4.1 app connects to a second, non-primary server via SSH for a backend jobs. Consequently, when the rails app restarts daily, the SSH connection needs to be live/up (rather the second, non-primary server needs to live/up), otherwise the the app crashes. This is due to eager loading by default being set to true in production.rb (config.eager_load = true).

I'm tempted to break this dependency by disabling eager loads, but I'm not able to find much information on the performance impact. So, my questions are...

1) if eager_load is set to false, will that simple slow down the app's startup time, or will the app eagerly load resources the first time they are hit?

3) If eager_load is simply turned off, to what degree will this impact the performance off the app (more subjective question)?

2) The model that performs the SSH connection are under folder app\models\legacy. Instead of changing eager_load to false, can that folder be excluded from eager loaded resources? If so, how? I believe I would need to edit config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**/}')] but not entirely sure.

production.rb:

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both thread web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true
2

There are 2 answers

0
Matt Brictson On BEST ANSWER

Setting eager_load=false will probably speed up your app's startup, because loading will be deferred until necessary.

However, the penalty is that your app will likely use more memory (which is usually the most scarce server resource). I suspect that you may also run into threading bugs if you use a multithreaded server (e.g. puma) with eager_load=false.

Since Rails automatically includes all app/* directories in its eager load paths, I can't think of an easy way to exclude app/models/legacy while eager-loading everything else.

Instead, you could move the contents of app/models/legacy to e.g. legacy/ at the root of your project and add that to the autoload_paths:

config.autoload_paths += %W( #{config.root}/legacy )

Now Rails will still be able to find those files, but they won't be eagerly loaded in production.

0
Chitra On

disable_dependency_loading Disables the automatic dependency loading if the config.eager_load is set to true.

for detailed info please have a look on this blog - http://blog.arkency.com/2014/11/dont-forget-about-eager-load-when-extending-autoload/