redis.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. .. _broker-redis:
  2. =============
  3. Using Redis
  4. =============
  5. .. _broker-redis-installation:
  6. Installation
  7. ============
  8. For the Redis support you have to install additional dependencies.
  9. You can install both Celery and these dependencies in one go using
  10. either the `celery-with-redis`_, or the `django-celery-with-redis` bundles:
  11. .. code-block:: bash
  12. $ pip install -U celery-with-redis
  13. .. _`celery-with-redis`:
  14. http://pypi.python.org/pypi/celery-with-redis
  15. .. _`django-celery-with-redis`:
  16. http://pypi.python.org/pypi/django-celery-with-redis
  17. .. _broker-redis-configuration:
  18. Configuration
  19. =============
  20. Configuration is easy, just configure the location of
  21. your Redis database::
  22. BROKER_URL = 'redis://localhost:6379/0'
  23. Where the URL is in the format of::
  24. redis://:password@hostname:port/db_number
  25. all fields after the scheme are optional, and will default to localhost on port 6379,
  26. using database 0.
  27. .. _redis-visibility_timeout:
  28. Visibility Timeout
  29. ------------------
  30. The visibility timeout defines the number of seconds to wait
  31. for the worker to acknowledge the task before the message is redelivered
  32. to another worker. Be sure to see :ref:`redis-caveats` below.
  33. This option is set via the :setting:`BROKER_TRANSPORT_OPTIONS` setting::
  34. BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600} # 1 hour.
  35. The default visibility timeout for Redis is 1 hour.
  36. .. _redis-results-configuration:
  37. Results
  38. -------
  39. If you also want to store the state and return values of tasks in Redis,
  40. you should configure these settings::
  41. CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
  42. For a complete list of options supported by the Redis result backend, see
  43. :ref:`conf-redis-result-backend`
  44. .. _redis-caveats:
  45. Caveats
  46. =======
  47. - If a task is not acknowledged within the :ref:`redis-visibility_timeout`
  48. the task will be redelivered to another worker and executed.
  49. This causes problems with ETA/countdown/retry tasks where the
  50. time to execute exceeds the visibility timeout; in fact if that
  51. happens it will be executed again, and again in a loop.
  52. So you have to increase the visibility timeout to match
  53. the time of the longest ETA you are planning to use.
  54. Note that Celery will redeliver messages at worker shutdown,
  55. so having a long visibility timeout will only delay the redelivery
  56. of 'lost' tasks in the event of a power failure or forcefully terminated
  57. workers.
  58. Periodic tasks will not be affected by the visibility timeout,
  59. as this is a concept separate from ETA/countdown.
  60. You can increase this timeout by configuring a transport option
  61. with the same name:
  62. BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 43200}
  63. The value must be an int describing the number of seconds.
  64. - Monitoring events (as used by flower and other tools) are global
  65. and is not affected by the virtual host setting.
  66. This is caused by a limitation in Redis. The Redis PUB/SUB channels
  67. are global and not affected by the database number.
  68. - Redis may evict keys from the database in some situations
  69. If you experience an error like::
  70. InconsistencyError, Probably the key ('_kombu.binding.celery') has been
  71. removed from the Redis database.
  72. you may want to configure the redis-server to not evict keys by setting
  73. the ``timeout`` parameter to 0.