redis.rst 5.0 KB

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