Skip to main content

Celery broker and results backend implementation for Redis Sentinel

Project description

https://badge.fury.io/py/celery-redis-sentinel.svg https://travis-ci.org/dealertrack/celery-redis-sentinel.svg?branch=master https://coveralls.io/repos/dealertrack/celery-redis-sentinel/badge.svg?branch=master&service=github

Celery broker and results backend implementation for Redis Sentinel

Why?

Redis is a pretty awesome in-memory key-value data-store. Being in-memory makes it wickedly fast however at a cost of no-persistence. In business-critical applications (you know, which make company money) that makes stand-alone redis deployment non-practical. This is where Redis Sentinel comes in. It provides scalability and high availability to Redis 2.X (Redis 3.X comes with native-clustering which is different from Sentinel). As a result, Redis becomes a viable solution for solving some of business needs. As you can imagine from the project title, one use-case is using Redis Sentinel with celery. Unfortunately celery does not support Redis Sentinel by default hence this library which aims to provide non-official Redis Sentinel support as both celery broker and results backend.

Installing

Installation is super easy with pip:

$ pip install celery-redis-sentinel

Usage

Using this library is pretty simple. All you need to do is configure celery to use Redis Sentinel for broker and/or results backend. That is done with a couple of settings:

# celeryconfig.py
BROKER_URL = 'redis-sentinel://redis-sentinel:26379/0'
BROKER_TRANSPORT_OPTIONS = {
    'sentinels': [('192.168.1.1', 26379),
                  ('192.168.1.2', 26379),
                  ('192.168.1.3', 26379)],
    'service_name': 'master',
    'socket_timeout': 0.1,
}

CELERY_RESULT_BACKEND = 'redis-sentinel://redis-sentinel:26379/1'
CELERY_RESULT_BACKEND_TRANSPORT_OPTIONS = BROKER_TRANSPORT_OPTIONS

Some notes about the configuration:

  • note the use of redis-sentinel schema within the URL for broker and results backend. In order to use that schema, which is not shipped with celery, where you create your celery application you must first need to register sentinel support:

    # tasks.py
    from celery_redis_sentinel import register
    
    # has to be called before creating celery app
    register()
    
    app = Celery('tasks')
  • hostname and port are ignored within the actual URL. Sentinel uses transport options sentinels setting to create a Sentinel() instead of configuration URL.

Scheduling During Failover

Some considerations while using Redis Sentinel as a celery broker. While the failover is in progress, no tasks can be scheduled. Trying to schedule a task will either raise Timeout or ConnectionError. That is because other sentinel nodes within the cluster, depending on the configuration, have a timeout until they elect a new master. During that time, trying to schedule a task will attempt to store it in now-invalid master node hence the exception. If that is unacceptable within your application, this library comes with a small wrapper which allows to trigger tasks which will block the scheduling until new master will be elected:

from celery_redis_sentinel.redis_sentinel import ensure_redis_call
from tasks import add

# this will blow up during failover
add.delay(1, 2)
# this will keep retrying until it succeeds
ensure_redis_call(add.delay, 1, 2)

Alternatively you can use a supplied abstract celery task subclass which provides same retrying behavior in the task definition itself:

# tasks.py
from celery_redis_sentinel.task import EnsuredRedisTask

@app.task(base=EnsuredRedisTask)
def add(a, b):
    return a + b

History

0.3 (2016-05-03)

  • New: Addition of ShortLivedStrictRedis and ShortLivedSentinel. Both of them use short-lived connections which disconnect from redis as soon as the query to redis is complete.

  • Fixed: All sentinel connections are now created via ShortLivedSentinel. This fixes an issue when sentinel would reach its max connections limit since all celery workers would always be connected to sentinel. That is not necessary since sentinel is queried very rarely for the current master connection details. In addition this is useful when Redis Sentinel is used behind a firewall since sentinel would not notice when firewall would close the connections and so would not release them.

0.2 (2016-01-14)

  • New: Added EnsuredRedisTask which allows to ensure tasks are scheduled via an abstract base task class in task definition rather then explicitly using ensure_redis_call while calling the task:

    @app.task(base=EnsuredRedisTask)
    def foo(): pass

0.1 (2016-01-13)

  • First release

Credits

This utility was created at dealertrack technologies (dealertrack GitHub) for our internal use so thank you dealertrack for allowing to contribute the utility to the open-source community.

Development Lead

Contributors

None yet. Why not be the first?

License

The MIT License (MIT)

Copyright © 2015-2016, dealertrack technologies

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

celery-redis-sentinel-0.3.0.tar.gz (21.1 kB view details)

Uploaded Source

File details

Details for the file celery-redis-sentinel-0.3.0.tar.gz.

File metadata

File hashes

Hashes for celery-redis-sentinel-0.3.0.tar.gz
Algorithm Hash digest
SHA256 0527d74ccfc6b0183565ac54b08f0e51a9ebc135fc67cb35a6f99ffa19b392a9
MD5 698816160d869eacabfe9e21b019208c
BLAKE2b-256 22ec53826b4b631c535d47716123ad9bfe949d699f0a6d0d6fd7928d95ae7219

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page