What is the optimal number of Unicorn workers?

k5trismegistus
3 min readAug 16, 2017

--

Introduction

When operating a web application made with Rails (or other Ruby based frameworks), it is common to take the following configuration.

Internet <-> nginx <-> Unicorn <-> Rails App

In this case, have you hesitated by how to set the number of workers of nginx / Unicorn?

Because nginx hasevent driven architecture, basically it does not make sense even if number of workers is greater than number of CPUs. In the automatic setting, it is the same as the number of CPUs, and there is no point in changing it.

Meanwhile Unicorn is a simple multi-process model, so configuration of the number of workers is important.

Configuration of the number of workers

If the application is purely CPU-intensive, it does not make sense to increase the number of workers beyond the number of CPUs. . However, the actual application should read the DB, make inquiries to the external API. There will be a lot of time waiting for I / O.

If the number of Unicorn workers is exactly the same as the number of CPUs, the CPU is not doing anything to wait for this I / O wait.

If you have a process waiting for I / O, you will be wasting valuable CPU resources unless there is a process that uses that CPU for that time. At this point I understand that it is better to have at least a larger number of workers than the number of CPUs. But how much should I do?

When I searched on Internet, I saw an opinion like CPU number + 1 is good, but this has no basis at all. What is +1? So I decided to think about it properly.

Calculation method

Model

Simplify the flow when an application processes an application by request, and make it as follows.

(I / O: r [msec]) -> (Computing: t [msec])

First, there is I / O bound process which takes r[msec] (during this phase CPU is not used at all). Next, CPU bound process that takes t[msec] follows.

A formula

When the number of workers is optimum, the following equation should hold.

(Number of finishing I / O bound process per unit time, ) = (Number of finishing CPU bound process per unit time)

Let N be the number of effective workers, n be the number of CPUs, r be the time taken for I / O processing, t be the time required for CPU processing, and T be the unit time.

(N-n) workers are performing I / O processing, and n workers are using CPU. The more number of workers, the more I / O processing requests per unit time will be requested, but the number of requests that CPU processing ends depends only on the number of CPUs.

Conclusion

The optimal number of unicorn worker is calculated by following equation.

First, you should measure time. In one request, how much time your application wait for I/O and spend CPU?

Next, substitute measured value and number of CPU which application canuse into the equation.

Obtained N is the optimal number of Unicorn workers.

Caution

Of course, all we have mentioned so far is based on the assumption that all processes using CPU are Unicorn’s worker processes. Actually, although the OS and the daemon process use the CPU, we cut off that they are negligible and their influence is minor.

Afterword

The original is written in Japanese. (http://qiita.com/k5trismegistus/items/d63b453f27981e7864e7)

I tried to translate into English with Google translate, but I am not used to writing post in English, so if you have improvement idea, please comment.

--

--

k5trismegistus
k5trismegistus

Written by k5trismegistus

Web develop engineer, in Japan

No responses yet