redis.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. the ``celery[redis]`` :ref:`bundle <bundles>`:
  11. .. code-block:: bash
  12. $ pip install -U celery[redis]
  13. .. _broker-redis-configuration:
  14. Configuration
  15. =============
  16. Configuration is easy, just configure the location of
  17. your Redis database::
  18. BROKER_URL = 'redis://localhost:6379/0'
  19. Where the URL is in the format of::
  20. redis://:password@hostname:port/db_number
  21. all fields after the scheme are optional, and will default to localhost on port 6379,
  22. using database 0.
  23. .. _redis-visibility_timeout:
  24. Visibility Timeout
  25. ------------------
  26. The visibility timeout defines the number of seconds to wait
  27. for the worker to acknowledge the task before the message is redelivered
  28. to another worker. Be sure to see :ref:`redis-caveats` below.
  29. This option is set via the :setting:`BROKER_TRANSPORT_OPTIONS` setting::
  30. BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600} # 1 hour.
  31. The default visibility timeout for Redis is 1 hour.
  32. .. _redis-results-configuration:
  33. Results
  34. -------
  35. If you also want to store the state and return values of tasks in Redis,
  36. you should configure these settings::
  37. CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
  38. For a complete list of options supported by the Redis result backend, see
  39. :ref:`conf-redis-result-backend`
  40. .. _redis-caveats:
  41. Caveats
  42. =======
  43. .. _redis-caveat-fanout-prefix:
  44. - Broadcast messages will be seen by all virtual hosts by default.
  45. You have to set a transport option to prefix the messages so that
  46. they will only be received by the active virtual host::
  47. BROKER_TRANSPORT_OPTIONS = {'fanout_prefix': True}
  48. Note that you will not be able to communicate with workers running older
  49. versions or workers that does not have this setting enabled.
  50. This setting will be the default in the future, so better to migrate
  51. sooner rather than later.
  52. .. _redis-caveat-fanout-patterns:
  53. - Workers will receive all task related events by default.
  54. To avoid this you must set the ``fanout_patterns`` fanout option so that
  55. the workers may only subscribe to worker related events::
  56. BROKER_TRANSPORT_OPTIONS = {'fanout_patterns': True}
  57. Note that this change is backward incompatible so all workers in the
  58. cluster must have this option enabled, or else they will not be able to
  59. communicate.
  60. This option will be enabled by default in the future.
  61. - If a task is not acknowledged within the :ref:`redis-visibility_timeout`
  62. the task will be redelivered to another worker and executed.
  63. This causes problems with ETA/countdown/retry tasks where the
  64. time to execute exceeds the visibility timeout; in fact if that
  65. happens it will be executed again, and again in a loop.
  66. So you have to increase the visibility timeout to match
  67. the time of the longest ETA you are planning to use.
  68. Note that Celery will redeliver messages at worker shutdown,
  69. so having a long visibility timeout will only delay the redelivery
  70. of 'lost' tasks in the event of a power failure or forcefully terminated
  71. workers.
  72. Periodic tasks will not be affected by the visibility timeout,
  73. as this is a concept separate from ETA/countdown.
  74. You can increase this timeout by configuring a transport option
  75. with the same name::
  76. BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 43200}
  77. The value must be an int describing the number of seconds.
  78. - Monitoring events (as used by flower and other tools) are global
  79. and is not affected by the virtual host setting.
  80. This is caused by a limitation in Redis. The Redis PUB/SUB channels
  81. are global and not affected by the database number.
  82. - Redis may evict keys from the database in some situations
  83. If you experience an error like::
  84. InconsistencyError, Probably the key ('_kombu.binding.celery') has been
  85. removed from the Redis database.
  86. you may want to configure the redis-server to not evict keys by setting
  87. the ``timeout`` parameter to 0.