JezK
Edit File: page-TUNING.ri
U:RDoc::TopLevel[ i I"TUNING:ETcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[S:RDoc::Markup::Heading: leveli: textI"Tuning unicorn;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"Lunicorn performance is generally as good as a (mostly) Ruby web server ;TI"Gcan provide. Most often the performance bottleneck is in the web ;TI"?application running on Unicorn rather than Unicorn itself.;T@ S; ; i;I"unicorn Configuration;T@ o; ;[I"FSee Unicorn::Configurator for details on the config file format. ;TI"E+worker_processes+ is the most-commonly needed tuning parameter.;T@ S; ; i;I"+Unicorn::Configurator#worker_processes;T@ o:RDoc::Markup::List: @type:BULLET:@items[ o:RDoc::Markup::ListItem:@label0;[o; ;[ I"Gworker_processes should be scaled to the number of processes your ;TI"Fbackend system(s) can support. DO NOT scale it to the number of ;TI"Fexternal network clients your application expects to be serving. ;TI"Gunicorn is NOT for serving slow clients, that is the job of nginx.;T@ o;;0;[o; ;[ I"Hworker_processes should be *at* *least* the number of CPU cores on ;TI"@a dedicated server (unless you do not have enough memory). ;TI"HIf your application has occasionally slow responses that are /not/ ;TI"MCPU-intensive, you may increase this to workaround those inefficiencies.;T@ o;;0;[o; ;[I"GUnder Ruby 2.2 or later, Etc.nprocessors may be used to determine ;TI"%the number of CPU cores present.;T@ o;;0;[o; ;[I"Kworker_processes may be increased for Unicorn::OobGC users to provide ;TI"$more consistent response times.;T@ o;;0;[o; ;[I"JNever, ever, increase worker_processes to the point where the system ;TI"Kruns out of physical memory and hits swap. Production servers should ;TI"#never see heavy swap activity.;T@ S; ; i;I")Unicorn::Configurator#listen Options;T@ o;;;;[ o;;0;[o; ;[I"ESetting a very low value for the :backlog parameter in "listen" ;TI"Bdirectives can allow failover to happen more quickly if your ;TI""cluster is configured for it.;T@ o;;0;[o; ;[ I"HIf you're doing extremely simple benchmarks and getting connection ;TI"Ierrors under high request rates, increasing your :backlog parameter ;TI"Jabove the already-generous default of 1024 can help avoid connection ;TI"Gerrors. Keep in mind this is not recommended for real traffic if ;TI"9you have another machine to failover to (see above).;T@ o;;0;[o; ;[I"L:rcvbuf and :sndbuf parameters generally do not need to be set for TCP ;TI"Llisteners under Linux 2.6 because auto-tuning is enabled. UNIX domain ;TI"Lsockets do not have auto-tuning buffer sizes; so increasing those will ;TI"Fallow syscalls and task switches to be saved for larger requests ;TI"Kand responses. If your app only generates small responses or expects ;TI"Ismall requests, you may shrink the buffer sizes to save memory, too.;T@ o;;0;[o; ;[ I"EHaving socket buffers too large can also be detrimental or have ;TI"Ilittle effect. Huge buffers can put more pressure on the allocator ;TI"Fand may also thrash CPU caches, cancelling out performance gains ;TI"one would normally expect.;T@ o;;0;[o; ;[I"HUNIX domain sockets are slightly faster than TCP sockets, but only ;TI"*work if nginx is on the same machine.;T@ S; ; i;I"Other unicorn settings;T@ o;;;;[o;;0;[o; ;[I"GSetting "preload_app true" can allow copy-on-write-friendly GC to ;TI"Lbe used to save memory. It will probably not work out of the box with ;TI"Dapplications that open sockets or perform random I/O on files. ;TI"GDatabases like TokyoCabinet use concurrency-safe pread()/pwrite() ;TI"Dfunctions for safe sharing of database file descriptors across ;TI"processes.;T@ o;;0;[o; ;[ I"HOn POSIX-compliant filesystems, it is safe for multiple threads or ;TI"Jprocesses to append to one log file as long as all the processes are ;TI"9have them unbuffered (File#sync = true) or they are ;TI":record(line)-buffered in userspace before any writes.;T@ S; ; i;I"/Kernel Parameters (Linux sysctl and sysfs);T@ o; ;[I"PWARNING: Do not change system parameters unless you know what you're doing!;T@ o;;;;[o;;0;[o; ;[I"ETransparent hugepages (THP) improves performance in many cases, ;TI"8but can also increase memory use when relying on a ;TI"Icopy-on-write(CoW)-friendly GC (Ruby 2.0+) with "preload_app true". ;TI"ECoW operates at the page level, so writing to a huge page would ;TI"Btrigger a 2 MB copy (x86-64), as opposed to a 4 KB copy on a ;TI"regular (non-huge) page.;T@ o; ;[I"HConsider only allowing THP to be used when it is requested via the ;TI"madvise(2) syscall:;T@ o:RDoc::Markup::Verbatim;[I"?echo madvise >/sys/kernel/mm/transparent_hugepage/enabled ;T:@format0o; ;[I".Or disabling it system-wide, via "never".;T@ o; ;[I"@n.b. "page" in this context only applies to the OS kernel, ;TI"ERuby GC implementations also use this term for the same concept ;TI")in a way that is agnostic to the OS.;T@ o;;0;[o; ;[I"Fnet.core.rmem_max and net.core.wmem_max can increase the allowed ;TI"Jsize of :rcvbuf and :sndbuf respectively. This is mostly only useful ;TI"Hfor UNIX domain sockets which do not have auto-tuning buffer sizes.;T@ o;;0;[o; ;[ I"HFor load testing/benchmarking with UNIX domain sockets, you should ;TI"Econsider increasing net.core.somaxconn or else nginx will start ;TI"Ifailing to connect under heavy load. You may also consider setting ;TI"5a higher :backlog to listen on as noted earlier.;T@ o;;0;[o; ;[ I"=If you're running out of local ports, consider lowering ;TI"Dnet.ipv4.tcp_fin_timeout to 20-30 (default: 60 seconds). Also ;TI"9consider widening the usable port range by changing ;TI""net.ipv4.ip_local_port_range.;T@ o;;0;[o; ;[ I"?Setting net.ipv4.tcp_timestamps=1 will also allow setting ;TI"Hnet.ipv4.tcp_tw_reuse=1 and net.ipv4.tcp_tw_recycle=1, which along ;TI"Ewith the above settings can slow down port exhaustion. Not all ;TI"Knetworks are compatible with these settings, check with your friendly ;TI"1network administrator before changing these.;T@ o;;0;[o; ;[I"DIncreasing the MTU size can reduce framing overhead for larger ;TI"Btransfers. One often-overlooked detail is that the loopback ;TI";device (usually "lo") can have its MTU increased, too.;T: @file@:0@omit_headings_from_table_of_contents_below0