redis.rst 4.4 KB

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