Aaron Gibralter
Aaron Gibralter

Reputation: 4853

Unicorn workers' first responses are slower by few seconds

I can't figure out how I can get my unicorn workers to only accept connections when they're really "ready" to process a request. I find that the first few requests are slow, then they speed up dramatically (from a few seconds down to a hundred or so ms). This problem seems to be compounded by the fact that unicorn seems to kill workers after a certain amount of time, meaning that I'm constantly facing the performance hit of the slow first request. Has anyone else seen this or have an idea of what I can do?

Upvotes: 4

Views: 2309

Answers (1)

Aaron Gibralter
Aaron Gibralter

Reputation: 4853

It turns out the our i18n yml files being lazily loaded in the views on the first request was causing the performance issues. Simply adding the following to my config/unicorn.rb seems to have solved the issue:

before_fork do |server, worker|
  # The following is highly recomended for Rails + "preload_app true" as
  # there's no need for the master process to hold a connection.
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

  # No need to disconnect from Redis servers--they are connected to lazily.

  # Force translations to be loaded into memory.
  I18n.t('activerecord')
end

Upvotes: 6

Related Questions