<p>I was wondering what the correct approach to deploying a containerized Django app using gunicorn &amp; celery was. </p>

<p>Specifically, each of these processes has a built-in way of scaling vertically, using <code>workers</code> for gunicorn and <code>concurrency</code> for celery. And then there is the Kubernetes approach to scaling using <code>replicas</code></p>

<p>There is also this notion of setting workers equal to some function of the CPUs. Gunicorn recommends </p>

<blockquote>
  <p>2-4 workers per core</p>
</blockquote>

<p>However, I am confused what this translates to on K8s where CPU is a divisible shared resource - unless I use resoureceQuotas.</p>

<p>I want to understand what the Best Practice is. There are three options I can think of:</p>

<ul>
<li>Have single workers for gunicorn and a concurrency of 1 for celery, and scale them using the replicas? (horizontal scaling)</li>
<li>Have gunicorn &amp; celery run in a single replica deployment with internal scaling (vertical scaling). This would mean setting fairly high values of workers &amp; concurrency respectively. </li>
<li>A mixed approach between 1 and 2, where we run gunicorn and celery with a small value for workers &amp; concurrency, (say 2), and then use K8s Deployment replicas to scale horizontally.</li>
</ul>

<p>There are some questions on SO around this, but none offer an in-depth/thoughtful answer. Would appreciate if someone can share their experience.</p>

<p>Note: We use the default worker_class <code>sync</code> for Gunicorn</p>

rtindru
<p>These technologies aren't as similar as they initially seem.
They address different portions of the application stack and are actually complementary.</p>
<p>Gunicorn is for scaling web request concurrency, while celery should be thought of as a worker queue. We'll get to kubernetes soon.</p>
<hr />
<h3>Gunicorn</h3>
<p>Web request concurrency is primarily limited by network I/O or &quot;I/O bound&quot;. These types of tasks can be scaled using cooperative scheduling provided by threads. If you find request concurrency is limiting your application, increasing gunicorn worker threads may well be the place to start.</p>
<hr />
<h3>Celery</h3>
<p>Heavy lifting tasks e.g. compress an image, run some ML algo, are &quot;CPU bound&quot; tasks. They can't benefit from threading as much as more CPUs. These tasks should be offloaded and parallelized by celery workers.</p>
<hr />
<h3>Kubernetes</h3>
<p>Where Kubernetes comes in handy is by providing out-of-the-box horizontal scalability and fault tolerance.</p>
<p>Architecturally, I'd use two separate k8s deployments to represent the different scalablity concerns of your application.
One deployment for the Django app and another for the celery workers.
This allows you to independently scale request throughput vs. processing power.</p>
<p>I run celery workers pinned to a single core per container (<code>-c 1</code>) this vastly simplifies debugging and adheres to Docker's &quot;one process per container&quot; mantra. It also gives you the added benefit of predictability, as you can scale the processing power on a per-core basis by incrementing the replica count.</p>
<p>Scaling the Django app deployment is where you'll need to DYOR to find the best settings for your particular application.
Again stick to using <code>--workers 1</code> so there is a single process per container but you should experiment with <code>--threads</code> to find the best solution. Again leave horizontal scaling to Kubernetes by simply changing the replica count.</p>
<p>HTH
It's definitely something I had to wrap my head around when working on similar projects.</p>
