Puma Stops Running for Rails App on EC2 Instance with Nginx (using Capistrano/Capistrano Puma)
<p>My top-level question is, how can I get Puma to stop failing.  But that is really made up of lots of smaller questions.  I will number and bold each of them, to try to make this question answerable.</p>

<p>I am hosting a Rails application on an EC2 instance that is a t2.nano.  This is admittedly, a very small box--but I don't expect my website to receive any traffic.  I configured everything successfully with Nginx and Puma using Capistrano and Capistrano Puma.  Everything was great, until one day I went to my website and saw the Nginx 504 message.</p>

<p>I opened the Nginx error log and saw that it could not connect to Puma: </p>

<p><code>connect() to unix:/home/deploy/myapp/shared/tmp/sockets/puma.sock failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: localhost, request: "GET / HTTP/1.0", upstream: "http://unix:/home/deploy/myapp/shared/tmp/sockets/puma.sock:/500.html", host: "myapp.com"</code></p>

<p>Debugging this, I learned that Puma had stopped running.  That is why Nginx could not connect to it.  I think there are two problems here: the first, is that Puma should not stop running.  The server is tiny, but there is no traffic.  the second, is that when Puma does fail, it should restart gracefully.  However, I am just focusing on the first issue for now.  Because if Puma is constantly restarting, it seems reasonable that sometimes it kills the process in a harsh way.</p>

<p>To debug this, I opened htop.  Sure enough, the machine was running without any memory to spare.  This makes sense--I am running a database, rails app, webserver, and memcache on one tiny machine.  It keeps running out of memory and killing Puma.</p>

<p>I looked into the Puma configuration I had set up with Capistrano.  In config/deploy.rb I had these lines--</p>

<pre><code>set :puma_threads, [0, 8]
set :puma_workers, 0
</code></pre>

<p>I read all about puma_workers and puma_threads.  I also learned that Nginx has its own workers.  Puma processes are very expensive.  What makes Puma cool is that it is properly muli-threaded--so the independent processes are awesome.  It sounds like each worker has its own set of threads--so if there are 4 workers with 8 threads, there will be 32 processes.  But in my case, I want to use very little memory.  2 processes sound good to me.  <b>1. Is my understanding of workers and threads correct?</b></p>

<p>I updated my config/deploy.rb file and deployed, with 0 puma_workers and min=0, max=2 threads.</p>

<p>It appears the configuration for Nginx lives here: /etc/nginx/nginx.conf.  And the configuration for Puma lives here: /home/deploy/myapp/shared/puma.rb.  I would have expected my updates in config/deploy.rb to have had Capistano edit the config files.  No luck--my min, max threads was still set to 0,8.  <b>2. Is it correct to try and update these values through config/deploy.rb when using Capistano?</b></p>

<p>Also--I opened the nginx.conf and saw <code>worker_processes 4;</code>.  <b>3. Was this set to four when I installed Nginx or did Capistano set this default?</b></p>

<p>I opened htop and sure enough I had lots of Puma processes.  Therefore, I edited my config files manually and restarted Puma and Nginx.</p>

<p>I changed the number of Nginx workers from 4 to 1.  Looking in htop, this worked.  I now only had 1 Nginx worker.  However, the Nginx workers were never very expensive (compared to the Puma threads).  So I don't think this matters much.</p>

<p>However, there were still more than 2 Puma threads--there were 6.  On a lark, I changed the minimum number of threads from 0 to 1--thinking 0 isn't a possible number so maybe it's setting a default.  This increased the number of Puma processes to 9.  I also tried changing the number of puma_workers to 1, for the same reason, and the number of processes increased.  <b>4. What does it mean to have 0 threads and/or workers?</b></p>

<p>I then tried to kill one of the puma processes manually (sudo kill xxxxx), and then <b>all</b> of the Puma processes died.</p>

<p><b>5.  What do I have to do to have just 2 puma processes?</b></p>

<p>As you can see, my understanding of Puma is not great and the lines between what Puma vs Nginx vs Capistano touches is not clear.  Any help is greatly appreciated.  I haven't been able to find great resources regarding this issue.</p>