optimizing.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. .. _optimizing:
  2. ============
  3. Optimizing
  4. ============
  5. Introduction
  6. ============
  7. The default configuration, like any good default, is full of compromises.
  8. It is not tweaked to be optimal for any single use case, but tries to
  9. find middle ground that works *well enough* for most situations.
  10. There are key optimizations to be done if your application is mainly
  11. processing lots of short tasks, and also if you have fewer but very
  12. long tasks.
  13. Optimization here does not necessarily mean optimizing for runtime, but also
  14. optimizing resource usage and ensuring responsiveness at times of high load.
  15. Ensuring Operations
  16. ===================
  17. In the book `Programming Pearls`_, Jon Bentley presents the concept of
  18. back-of-the-envelope calculations by asking the question;
  19. ❝ How much water flows out of the Mississippi River in a day? ❞
  20. The point of this exercise[*] is to demonstrate that there is a limit
  21. to how much data a system can process in a timely manner, and teaches
  22. back of the envelope calculations as a means to plan for this ahead of time.
  23. This is very relevant to Celery; If a task takes 10 minutes to complete,
  24. and there are 10 new tasks coming in every minute, then this means
  25. the queue will *never be processed*. This is why it's very important
  26. that you monitor queue lengths!
  27. One way to do this is by :ref:`using Munin <monitoring-munin>`.
  28. You should set up alerts, so you are notified as soon as any queue has
  29. reached an unacceptable size, this way you can take appropriate action like
  30. adding new worker nodes, or revoking unnecessary tasks.
  31. .. [*] The chapter is available to read for free here:
  32. `The back of the envelope`_. This book is a classic text, highly
  33. recommended.
  34. .. _`Programming Pearls`: http://www.cs.bell-labs.com/cm/cs/pearls/
  35. .. _`The back of the envelope`:
  36. http://books.google.com/books?id=kse_7qbWbjsC&pg=PA67
  37. .. _optimizing-worker-settings:
  38. Worker Settings
  39. ===============
  40. .. _optimizing-prefetch-limit:
  41. Prefetch Limits
  42. ---------------
  43. *Prefetch* is a term inherited from AMQP that is often misunderstood
  44. by users.
  45. The prefetch limit is a **limit** for the number of messages (tasks) a worker
  46. can reserve in advance. If this is set to zero, the worker will keep
  47. consuming messages *ad infinitum*, not respecting that there may be other
  48. available worker nodes that may be able to process them sooner[#],
  49. or that the messages may not even fit in memory.
  50. The workers initial prefetch count is set by multiplying
  51. the :setting:`CELERYD_PREFETCH_MULTIPLIER` setting by the number
  52. of child worker processes[#]. The default is 4 messages per child process.
  53. If you have many expensive tasks with a long duration you would want
  54. the multiplier value to be 1, which means it will only reserve one
  55. unacknowledged task per worker process at a time.
  56. However -- If you have lots of short tasks, and throughput/roundtrip latency
  57. is important to you, then you want this number to be large. Say 64, or 128
  58. for example, as the worker is able to process a lot more *tasks/s* if the
  59. messages have already been prefetched in memory. You may have to experiment
  60. to find the best value that works for you.
  61. If you have a combination of both very long and short tasks, then the best
  62. option is to use two worker nodes that is configured individually, and route
  63. the tasks accordingly (see :ref:`guide-routing`).
  64. .. [*] RabbitMQ and other brokers will deliver the messages in round-robin,
  65. so this doesn't apply to an active system. But if there is no prefetch
  66. limit and you restart the cluster, there will be timing delays between
  67. nodes starting, so if there are 3 offline nodes and one active node,
  68. then all messages will be delivered to the active node while the others
  69. are offline.
  70. .. [*] This is the concurrency setting; :setting:`CELERYD_CONCURRENCY` or the
  71. :option:`-c` option to :program:`celeryd`.
  72. Reserve one task at a time
  73. --------------------------
  74. When using early acknowledgement (default), a prefetch multiplier of 1
  75. means the worker will reserve at most one extra task for every active
  76. worker process.
  77. Often when users ask if it's possible to disable "prefetching of tasks",
  78. what they really want is to have a worker only reserve as many tasks
  79. as there are child processes at a time.
  80. Sadly, this requirement is not possible without enabling late
  81. acknowledgements; A task that has been started, will be
  82. retried if the worker crashes mid execution so the task must be `reentrant`_
  83. (see also notes at :ref:`faq-acks_late-vs-retry`).
  84. .. _`reentrant`: http://en.wikipedia.org/wiki/Reentrant_(subroutine)
  85. You can enable this behavior by using the following configuration:
  86. .. code-block:: python
  87. CELERY_ACKS_LATE = True
  88. CELERYD_PREFETCH_MULTIPLIER = 1
  89. .. optimizing-rate-limits:
  90. Rate Limits
  91. -----------
  92. The subsystem responsible for enforcing rate limits introduces extra
  93. complexity, so if you're not using rate limits it may be a good idea to
  94. disable them completely.
  95. Set the :setting:`CELERY_DISABLE_RATE_LIMITS` setting to disable
  96. the rate limit subsystem:
  97. .. code-block:: python
  98. CELERY_DISABLE_RATE_LIMITS = True