Tuesday, 27 August 2013

Reduce the unicorn app server response time

Reduce the unicorn app server response time

I recently migrated to Unicorn from passenger to run my e-commerce
application which is based on Ruby 2.0.0-p0 on Rails 3.2.13. The average
response time of the app server thats unicorn is quite high according to
newrelic metrics. How to tweak unicorn to reduce the app response time to
less 500ms, presently I am experiencing more then 1200ms. I have attached
the snap shot of the unicorn app server response time and also I have
attached the unicorn.rb and nginx.conf One thing we can notice that, Ruby
itself is consuming more then 800ms. How can I reduce that? I am running
AWS ubuntu ec2 instance. I am running on large instances.
require 'unicorn/oob_gc'
# this should probably be between CPU threads and CPU threads * 2
worker_processes 2
# this is your current deployed code symlink
root = "/path/to/app"
working_directory root
# don't use TCP to talk to Nginx
listen "/tmp/unicorn.sock"
# how long is it ok for your workers to hang
timeout 30
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn_stderr.log"
stdout_path "#{root}/log/unicorn_stdout.log"
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
check_client_connection false
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{root}/Gemfile"
end
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for
master to sent QUIT'
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
here is my nginx.conf
worker_processes 2;
user ubuntu ubuntu; # for systems with "nobody" as a group instead
# Feel free to change all paths to suite your needs here, of course
pid /etc/nginx/nginx.pid;
error_log /var/log/nginx/nginx.error.log;
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # "on" if nginx worker_processes > 1
# use epoll; # enable for Linux 2.6+
# use kqueue; # enable for FreeBSD, OSX
}
http {
# nginx will find this file in the config directory set at nginx build time
include mime.types;
# fallback in case we can't determine a type
default_type application/octet-stream;
# click tracking!
# access_log /var/log/nginx/nginx.access.log combined;
# you generally want to serve static files with nginx since neither
# Unicorn nor Rainbows! is optimized for it at the moment
sendfile on;
tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
tcp_nodelay off; # on may be better for some Comet/long-poll stuff
client_max_body_size 2M;
client_body_buffer_size 64k;
#file_cache
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
# gzip_types text/plain text/html text/xml text/css
# text/comma-separated-values
# text/javascript application/x-javascript
# application/atom+xml;
# this can be any application server, not just Unicorn/Rainbows!
upstream app_server {
# for UNIX domain socket setups:
#server unix:/path/to/.unicorn.sock fail_timeout=0;
;
# for TCP setups, point these to your backend servers
# server 192.168.0.9:8080 fail_timeout=0;
}
server {
keepalive_timeout 5;
# path for static files
root path/to/app;
try_files $uri/index.html $uri.html $uri @app;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
# Rails error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root path/to/app;
}
}
}
Here is the attachment of the newrelic

No comments:

Post a Comment