optimizing.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. .. _guide-optimizing:
  2. ============
  3. Optimizing
  4. ============
  5. Introduction
  6. ============
  7. The default configuration makes a lot of compromises. It's not optimal for
  8. any single case, but works well enough for most situations.
  9. There are optimizations that can be applied based on specific use cases.
  10. Optimizations can apply to different properties of the running environment,
  11. be it the time tasks take to execute, the amount of memory used, or
  12. responsiveness at times of high load.
  13. Ensuring Operations
  14. ===================
  15. In the book `Programming Pearls`_, Jon Bentley presents the concept of
  16. back-of-the-envelope calculations by asking the question;
  17. ❝ How much water flows out of the Mississippi River in a day? ❞
  18. The point of this exercise [*]_ is to show that there is a limit
  19. to how much data a system can process in a timely manner.
  20. Back of the envelope calculations can be used as a means to plan for this
  21. ahead of time.
  22. In Celery; If a task takes 10 minutes to complete,
  23. and there are 10 new tasks coming in every minute, the queue will never
  24. be empty. This is why it's very important
  25. that you monitor queue lengths!
  26. A way to do this is by :ref:`using Munin <monitoring-munin>`.
  27. You should set up alerts, that will notify you as soon as any queue has
  28. reached an unacceptable size. This way you can take appropriate action
  29. like adding new worker nodes, or revoking unnecessary tasks.
  30. .. [*] The chapter is available to read for free here:
  31. `The back of the envelope`_. The book is a classic text. Highly
  32. recommended.
  33. .. _`Programming Pearls`: http://www.cs.bell-labs.com/cm/cs/pearls/
  34. .. _`The back of the envelope`:
  35. http://books.google.com/books?id=kse_7qbWbjsC&pg=PA67
  36. .. _optimizing-general-settings:
  37. General Settings
  38. ================
  39. .. _optimizing-librabbitmq:
  40. librabbitmq
  41. -----------
  42. If you're using RabbitMQ (AMQP) as the broker then you can install the
  43. :mod:`librabbitmq` module to use an optimized client written in C:
  44. .. code-block:: bash
  45. $ pip install librabbitmq
  46. The 'amqp' transport will automatically use the librabbitmq module if it's
  47. installed, or you can also specify the transport you want directly by using
  48. the ``pyamqp://`` or ``librabbitmq://`` prefixes.
  49. .. _optimizing-connection-pools:
  50. Broker Connection Pools
  51. -----------------------
  52. The broker connection pool is enabled by default since version 2.5.
  53. You can tweak the :setting:`BROKER_POOL_LIMIT` setting to minimize
  54. contention, and the value should be based on the number of
  55. active threads/greenthreads using broker connections.
  56. .. _optimizing-transient-queues:
  57. Using Transient Queues
  58. ----------------------
  59. Queues created by Celery are persistent by default. This means that
  60. the broker will write messages to disk to ensure that the tasks will
  61. be executed even if the broker is restarted.
  62. But in some cases it's fine that the message is lost, so not all tasks
  63. require durability. You can create a *transient* queue for these tasks
  64. to improve performance:
  65. .. code-block:: python
  66. from kombu import Exchange, Queue
  67. CELERY_QUEUES = (
  68. Queue('celery', routing_key='celery'),
  69. Queue('transient', routing_key='transient',
  70. delivery_mode=1),
  71. )
  72. The ``delivery_mode`` changes how the messages to this queue are delivered.
  73. A value of 1 means that the message will not be written to disk, and a value
  74. of 2 (default) means that the message can be written to disk.
  75. To direct a task to your new transient queue you can specify the queue
  76. argument (or use the :setting:`CELERY_ROUTES` setting):
  77. .. code-block:: python
  78. task.apply_async(args, queue='transient')
  79. For more information see the :ref:`routing guide <guide-routing>`.
  80. .. _optimizing-worker-settings:
  81. Worker Settings
  82. ===============
  83. .. _optimizing-prefetch-limit:
  84. Prefetch Limits
  85. ---------------
  86. *Prefetch* is a term inherited from AMQP that is often misunderstood
  87. by users.
  88. The prefetch limit is a **limit** for the number of tasks (messages) a worker
  89. can reserve for itself. If it is zero, the worker will keep
  90. consuming messages, not respecting that there may be other
  91. available worker nodes that may be able to process them sooner [*]_,
  92. or that the messages may not even fit in memory.
  93. The workers' default prefetch count is the
  94. :setting:`CELERYD_PREFETCH_MULTIPLIER` setting multiplied by the number
  95. of concurrency slots[*]_ (processes/threads/greenthreads).
  96. If you have many tasks with a long duration you want
  97. the multiplier value to be 1, which means it will only reserve one
  98. task per worker process at a time.
  99. However -- If you have many short-running tasks, and throughput/round trip
  100. latency is important to you, this number should be large. The worker is
  101. able to process more tasks per second if the messages have already been
  102. prefetched, and is available in memory. You may have to experiment to find
  103. the best value that works for you. Values like 50 or 150 might make sense in
  104. these circumstances. Say 64, or 128.
  105. If you have a combination of long- and short-running tasks, the best option
  106. is to use two worker nodes that are configured separately, and route
  107. the tasks according to the run-time. (see :ref:`guide-routing`).
  108. .. [*] RabbitMQ and other brokers deliver messages round-robin,
  109. so this doesn't apply to an active system. If there is no prefetch
  110. limit and you restart the cluster, there will be timing delays between
  111. nodes starting. If there are 3 offline nodes and one active node,
  112. all messages will be delivered to the active node.
  113. .. [*] This is the concurrency setting; :setting:`CELERYD_CONCURRENCY` or the
  114. :option:`-c` option to the :program:`celery worker` program.
  115. Reserve one task at a time
  116. --------------------------
  117. When using early acknowledgement (default), a prefetch multiplier of 1
  118. means the worker will reserve at most one extra task for every active
  119. worker process.
  120. When users ask if it's possible to disable "prefetching of tasks", often
  121. what they really want is to have a worker only reserve as many tasks as there
  122. are child processes.
  123. But this is not possible without enabling late acknowledgements
  124. acknowledgements; A task that has been started, will be
  125. retried if the worker crashes mid execution so the task must be `idempotent`_
  126. (see also notes at :ref:`faq-acks_late-vs-retry`).
  127. .. _`idempotent`: http://en.wikipedia.org/wiki/Idempotent
  128. You can enable this behavior by using the following configuration options:
  129. .. code-block:: python
  130. CELERY_ACKS_LATE = True
  131. CELERYD_PREFETCH_MULTIPLIER = 1
  132. .. _prefork-pool-prefetch:
  133. Prefork pool prefetch settings
  134. ------------------------------
  135. The prefork pool will asynchronously send as many tasks to the processes
  136. as it can and this means that the processes are, in effect, prefetching
  137. tasks.
  138. This benefits performance but it also means that tasks may be stuck
  139. waiting for long running tasks to complete::
  140. -> send T1 to Process A
  141. # A executes T1
  142. -> send T2 to Process B
  143. # B executes T2
  144. <- T2 complete
  145. -> send T3 to Process A
  146. # A still executing T1, T3 stuck in local buffer and
  147. # will not start until T1 returns
  148. The worker will send tasks to the process as long as the pipe buffer is
  149. writable. The pipe buffer size varies based on the operating system: some may
  150. have a buffer as small as 64kb but on recent Linux versions the buffer
  151. size is 1MB (can only be changed system wide).
  152. You can disable this prefetching behavior by enabling the :option:`-Ofair`
  153. worker option:
  154. .. code-block:: bash
  155. $ celery -A proj worker -l info -Ofair
  156. With this option enabled the worker will only write to workers that are
  157. available for work, disabling the prefetch behavior.