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