redis.rst 4.5 KB

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