| 1 |
.. _guide-optimizing: |
| 2 |
|
| 3 |
============ |
| 4 |
Optimizing |
| 5 |
============ |
| 6 |
|
| 7 |
Introduction |
| 8 |
============ |
| 9 |
The default configuration makes a lot of compromises. It's not optimal for |
| 10 |
any single case, but works well enough for most situations. |
| 11 |
|
| 12 |
There are optimizations that can be applied based on specific use cases. |
| 13 |
|
| 14 |
Optimizations can apply to different properties of the running environment, |
| 15 |
be it the time tasks take to execute, the amount of memory used, or |
| 16 |
responsiveness at times of high load. |
| 17 |
|
| 18 |
Ensuring Operations |
| 19 |
=================== |
| 20 |
|
| 21 |
In the book Programming Pearls, Jon Bentley presents the concept of |
| 22 |
back-of-the-envelope calculations by asking the question; |
| 23 |
|
| 24 |
❝ How much water flows out of the Mississippi River in a day? ❞ |
| 25 |
|
| 26 |
The point of this exercise [*]_ is to show that there's a limit |
| 27 |
to how much data a system can process in a timely manner. |
| 28 |
Back of the envelope calculations can be used as a means to plan for this |
| 29 |
ahead of time. |
| 30 |
|
| 31 |
In Celery; If a task takes 10 minutes to complete, |
| 32 |
and there are 10 new tasks coming in every minute, the queue will never |
| 33 |
be empty. This is why it's very important |
| 34 |
that you monitor queue lengths! |
| 35 |
|
| 36 |
A way to do this is by :ref:`using Munin <monitoring-munin>`. |
| 37 |
You should set up alerts, that'll notify you as soon as any queue has |
| 38 |
reached an unacceptable size. This way you can take appropriate action |
| 39 |
like adding new worker nodes, or revoking unnecessary tasks. |
| 40 |
|
| 41 |
.. _`The back of the envelope`: |
| 42 |
http://books.google.com/books?id=kse_7qbWbjsC&pg=PA67 |
| 43 |
|
| 44 |
.. _optimizing-general-settings: |
| 45 |
|
| 46 |
General Settings |
| 47 |
================ |
| 48 |
|
| 49 |
.. _optimizing-connection-pools: |
| 50 |
|
| 51 |
Broker Connection Pools |
| 52 |
----------------------- |
| 53 |
|
| 54 |
The broker connection pool is enabled by default since version 2.5. |
| 55 |
|
| 56 |
You can tweak the :setting:`broker_pool_limit` setting to minimize |
| 57 |
contention, and the value should be based on the number of |
| 58 |
active threads/green-threads using broker connections. |
| 59 |
|
| 60 |
.. _optimizing-transient-queues: |
| 61 |
|
| 62 |
Using Transient Queues |
| 63 |
---------------------- |
| 64 |
|
| 65 |
Queues created by Celery are persistent by default. This means that |
| 66 |
the broker will write messages to disk to ensure that the tasks will |
| 67 |
be executed even if the broker is restarted. |
| 68 |
|
| 69 |
But in some cases it's fine that the message is lost, so not all tasks |
| 70 |
require durability. You can create a *transient* queue for these tasks |
| 71 |
to improve performance: |
| 72 |
|
| 73 |
.. code-block:: python |
| 74 |
|
| 75 |
from kombu import Exchange, Queue |
| 76 |
|
| 77 |
task_queues = ( |
| 78 |
Queue('celery', routing_key='celery'), |
| 79 |
Queue('transient', Exchange('transient', delivery_mode=1), |
| 80 |
routing_key='transient', durable=False), |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
or by using :setting:`task_routes`: |
| 85 |
|
| 86 |
.. code-block:: python |
| 87 |
|
| 88 |
task_routes = { |
| 89 |
'proj.tasks.add': {'queue': 'celery', 'delivery_mode': 'transient'} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
The ``delivery_mode`` changes how the messages to this queue are delivered. |
| 94 |
A value of one means that the message won't be written to disk, and a value |
| 95 |
of two (default) means that the message can be written to disk. |
| 96 |
|
| 97 |
To direct a task to your new transient queue you can specify the queue |
| 98 |
argument (or use the :setting:`task_routes` setting): |
| 99 |
|
| 100 |
.. code-block:: python |
| 101 |
|
| 102 |
task.apply_async(args, queue='transient') |
| 103 |
|
| 104 |
For more information see the :ref:`routing guide <guide-routing>`. |
| 105 |
|
| 106 |
.. _optimizing-worker-settings: |
| 107 |
|
| 108 |
Worker Settings |
| 109 |
=============== |
| 110 |
|
| 111 |
.. _optimizing-prefetch-limit: |
| 112 |
|
| 113 |
Prefetch Limits |
| 114 |
--------------- |
| 115 |
|
| 116 |
*Prefetch* is a term inherited from AMQP that's often misunderstood |
| 117 |
by users. |
| 118 |
|
| 119 |
The prefetch limit is a **limit** for the number of tasks (messages) a worker |
| 120 |
can reserve for itself. If it is zero, the worker will keep |
| 121 |
consuming messages, not respecting that there may be other |
| 122 |
available worker nodes that may be able to process them sooner [*]_, |
| 123 |
or that the messages may not even fit in memory. |
| 124 |
|
| 125 |
The workers' default prefetch count is the |
| 126 |
:setting:`worker_prefetch_multiplier` setting multiplied by the number |
| 127 |
of concurrency slots [*]_ (processes/threads/green-threads). |
| 128 |
|
| 129 |
If you have many tasks with a long duration you want |
| 130 |
the multiplier value to be *one*: meaning it'll only reserve one |
| 131 |
task per worker process at a time. |
| 132 |
|
| 133 |
However -- If you have many short-running tasks, and throughput/round trip |
| 134 |
latency is important to you, this number should be large. The worker is |
| 135 |
able to process more tasks per second if the messages have already been |
| 136 |
prefetched, and is available in memory. You may have to experiment to find |
| 137 |
the best value that works for you. Values like 50 or 150 might make sense in |
| 138 |
these circumstances. Say 64, or 128. |
| 139 |
|
| 140 |
If you have a combination of long- and short-running tasks, the best option |
| 141 |
is to use two worker nodes that are configured separately, and route |
| 142 |
the tasks according to the run-time (see :ref:`guide-routing`). |
| 143 |
|
| 144 |
Reserve one task at a time |
| 145 |
-------------------------- |
| 146 |
|
| 147 |
The task message is only deleted from the queue after the task is |
| 148 |
:term:`acknowledged`, so if the worker crashes before acknowledging the task, |
| 149 |
it can be redelivered to another worker (or the same after recovery). |
| 150 |
|
| 151 |
Note that an exception is considered normal operation in Celery and it will be acknowledged. |
| 152 |
Acknowledgments are really used to safeguard against failures that can not be normally |
| 153 |
handled by the Python exception system (i.e. power failure, memory corruption, hardware failure, fatal signal, etc.). |
| 154 |
For normal exceptions you should use task.retry() to retry the task. |
| 155 |
|
| 156 |
.. seealso:: |
| 157 |
|
| 158 |
Notes at :ref:`faq-acks_late-vs-retry`. |
| 159 |
|
| 160 |
When using the default of early acknowledgment, having a prefetch multiplier setting |
| 161 |
of *one*, means the worker will reserve at most one extra task for every |
| 162 |
worker process: or in other words, if the worker is started with |
| 163 |
:option:`-c 10 <celery worker -c>`, the worker may reserve at most 20 |
| 164 |
tasks (10 acknowledged tasks executing, and 10 unacknowledged reserved |
| 165 |
tasks) at any time. |
| 166 |
|
| 167 |
Often users ask if disabling "prefetching of tasks" is possible, and it is |
| 168 |
possible with a catch. You can have a worker only reserve as many tasks as |
| 169 |
there are worker processes, with the condition that they are acknowledged |
| 170 |
late (10 unacknowledged tasks executing for :option:`-c 10 <celery worker -c>`) |
| 171 |
|
| 172 |
For that, you need to enable :term:`late acknowledgment`. Using this option over the |
| 173 |
default behavior means a task that's already started executing will be |
| 174 |
retried in the event of a power failure or the worker instance being killed |
| 175 |
abruptly, so this also means the task must be :term:`idempotent` |
| 176 |
|
| 177 |
You can enable this behavior by using the following configuration options: |
| 178 |
|
| 179 |
.. code-block:: python |
| 180 |
|
| 181 |
task_acks_late = True |
| 182 |
worker_prefetch_multiplier = 1 |
| 183 |
|
| 184 |
If your tasks cannot be acknowledged late you can disable broker |
| 185 |
prefetching by enabling :setting:`worker_disable_prefetch`. With this |
| 186 |
setting the worker fetches a new task only when an execution slot is |
| 187 |
free, preventing tasks from waiting behind long running ones on busy |
| 188 |
workers. This can also be set from the command line using |
| 189 |
:option:`--disable-prefetch <celery worker --disable-prefetch>`. This feature |
| 190 |
is currently only supported when using Redis as the broker. |
| 191 |
|
| 192 |
Memory Usage |
| 193 |
------------ |
| 194 |
|
| 195 |
If you are experiencing high memory usage on a prefork worker, first you need |
| 196 |
to determine whether the issue is also happening on the Celery master |
| 197 |
process. The Celery master process's memory usage should not continue to |
| 198 |
increase drastically after start-up. If you see this happening, it may indicate |
| 199 |
a memory leak bug which should be reported to the Celery issue tracker. |
| 200 |
|
| 201 |
If only your child processes have high memory usage, this indicates an issue |
| 202 |
with your task. |
| 203 |
|
| 204 |
Keep in mind, Python process memory usage has a "high watermark" and will not |
| 205 |
return memory to the operating system until the child process has stopped. This |
| 206 |
means a single high memory usage task could permanently increase the memory |
| 207 |
usage of a child process until it's restarted. Fixing this may require adding |
| 208 |
chunking logic to your task to reduce peak memory usage. |
| 209 |
|
| 210 |
Celery workers have two main ways to help reduce memory usage due to the "high |
| 211 |
watermark" and/or memory leaks in child processes: the |
| 212 |
:setting:`worker_max_tasks_per_child` and :setting:`worker_max_memory_per_child` |
| 213 |
settings. |
| 214 |
|
| 215 |
You must be careful not to set these settings too low, or else your workers |
| 216 |
will spend most of their time restarting child processes instead of processing |
| 217 |
tasks. For example, if you use a :setting:`worker_max_tasks_per_child` of 1 |
| 218 |
and your child process takes 1 second to start, then that child process would |
| 219 |
only be able to process a maximum of 60 tasks per minute (assuming the task ran |
| 220 |
instantly). A similar issue can occur when your tasks always exceed |
| 221 |
:setting:`worker_max_memory_per_child`. |
| 222 |
|
| 223 |
|
| 224 |
.. rubric:: Footnotes |
| 225 |
|
| 226 |
.. [*] The chapter is available to read for free here: |
| 227 |
`The back of the envelope`_. The book is a classic text. Highly |
| 228 |
recommended. |
| 229 |
|
| 230 |
.. [*] RabbitMQ and other brokers deliver messages round-robin, |
| 231 |
so this doesn't apply to an active system. If there's no prefetch |
| 232 |
limit and you restart the cluster, there will be timing delays between |
| 233 |
nodes starting. If there are 3 offline nodes and one active node, |
| 234 |
all messages will be delivered to the active node. |
| 235 |
|
| 236 |
.. [*] This is the concurrency setting; :setting:`worker_concurrency` or the |
| 237 |
:option:`celery worker -c` option. |