Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicorn CPU usage spiking during load tests, ways to optimize

I am interested in ways to optimize my Unicorn setup for my Ruby on Rails 3.1.3 app. I'm currently spawning 14 worker processes on High-CPU Extra Large Instance since my application appears to be CPU bound during load tests. At about 20 requests per second replaying requests on a simulation load tests, all 8 cores on my instance get peaked out, and the box load spikes up to 7-8. Each unicorn instance is utilizing about 56-60% CPU.

I'm curious what are ways that I can optimize this? I'd like to be able to funnel more requests per second onto an instance of this size. Memory is completely fine as is all other I/O. CPU is getting tanked during my tests.

like image 793
randombits Avatar asked Jun 25 '12 02:06

randombits


People also ask

Why does my CPU load keep spiking?

Central processor unit (CPU) spikes in personal computers can be caused by a number of factors, such as excessive and continual usage, inadequate power supply, or improper cooling. Heavy software applications and running many programs at once can also cause spikes.

What is CPU load?

CPU load is the number of processes that are using, or want to use, CPU time, or queued up processes ready to use CPU. This can also be referred to as the run queue length.


2 Answers

If you are CPU bound you want to use no more unicorn processes than you have cores, otherwise you overload the system and slow down the scheduler. You can test this on a dev box using ab. You will notice that 2 unicorns will outperform 20 (number depends on cores, but the concept will hold true).

The exception to this rule is if your IO bound. In which case add as many unicorns as memory can hold.

A good performance trick is to route IO bound requests to a different app server hosting many unicorns. For example, if you have a request that uses a slow sql query, or your waiting on an external request, such as a credit card transaction. If using nginx, define an upstream server for the IO bound requests, forward those urls to a box with 40 unicorns. CPU bound or really fast requests, forward to a box with 8 unicorns (you stated you have 8 cores, but on aws you might want to try 4-6 as their schedulers are hypervised and already very busy).

Also, I'm not sure you can count on aws giving you reliable CPU usage, as your getting a percentage of an obscure percentage.

like image 164
Krut Avatar answered Sep 28 '22 08:09

Krut


First off, you probably don't want instances at 45-60% cpu. In that case, if you get a traffic spike, all of your instances will choke.

Next, 14 Unicorn instances seems large. Unicorn does not use threading. Rather, each process runs with a single thread. Unicorn's master process will only select a thread if it is able to handle it. Because of this, the number of cores isn't a metric you should use to measure performance with Unicorn.

A more conservative setup may use 4 or so Unicorn processes per instance, responding to maybe 5-8 requests per second. Then, adjust the number of instances until your CPU use is around 35%. This will ensure stability under the stressful '20 requests per second scenario.'

Lastly, you can get more gritty stats and details by using God.

like image 24
fdsaas Avatar answered Sep 28 '22 10:09

fdsaas