{
  "schema_version": 2,
  "id": "integrate/celery",
  "title": "Redis with Celery",
  "url": "https://redis.io/docs/latest/integrate/celery/",
  "summary": "Use Redis as the message broker and result backend for Celery task queues.",
  "tags": [
    "docs",
    "integrate",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-11-17T15:15:32Z",
  "children": [],
  "page_type": "content",
  "content_hash": "46fb34a385ee0314d9ca86b132f3997baa11cec4332bbd534c615f23e8fcb28d",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "[Celery](https://docs.celeryq.dev/) is a distributed task queue for Python. Your\napplication sends tasks to a *broker*, and a pool of *workers* runs them. Redis can act as\nthe broker, which holds the queued tasks, and as the *result backend*, which stores\ntask results. See the [job queue use case](https://redis.io/docs/latest/develop/use-cases/job-queue)\nfor when to choose this pattern."
    },
    {
      "id": "requirements",
      "title": "Requirements",
      "role": "content",
      "text": "The examples on this page were run with Celery 5.7 and Kombu 5.7 (the messaging library\nthat Celery's Redis transport uses) against Redis 7.2. Install Celery with its Redis\ndependencies:\n\n[code example]\n\nYou also need a running Redis server. You can run one locally with\n[Redis Open Source](https://redis.io/docs/latest/operate/oss_and_stack/install/archive/install-redis),\nuse [Docker](https://hub.docker.com/_/redis), or connect to\n[Redis Cloud](https://redis.io/cloud/)."
    },
    {
      "id": "configure-redis-as-the-broker-and-result-backend",
      "title": "Configure Redis as the broker and result backend",
      "role": "configuration",
      "text": "Create a Celery application and point `broker` and `backend` at your Redis server. Use\ndifferent [logical databases](https://redis.io/docs/latest/commands/select) if you want to keep queued\ntasks and results apart:\n\n[code example]"
    },
    {
      "id": "define-and-call-tasks",
      "title": "Define and call tasks",
      "role": "content",
      "text": "Decorate a function with `@app.task` to make it a task. Call `delay()` to queue it,\nor `apply_async()` to set options such as a delay. The returned result object lets you\nwait for the value:\n\n[code example]\n\n[code example]\n\nStart a worker in a separate terminal:\n\n[code example]\n\nIn this example, calling `flaky.delay()` fails twice and succeeds on the third attempt, so\nthe result is `ok after 2 retries`."
    },
    {
      "id": "what-celery-stores-in-redis",
      "title": "What Celery stores in Redis",
      "role": "content",
      "text": "With the configuration above, Redis holds the following:\n\n| Key | Type | Contents |\n|-----|------|----------|\n| `celery` | list | Messages for tasks that are waiting for a worker (the default queue). The key exists only while the queue isn't empty. |\n| `_kombu.binding.celery` and similar | set | Routing information that Kombu keeps for each queue. |\n| `celery-task-meta-<task-id>` | string | The task result as JSON, with a time to live (TTL) equal to `result_expires`. |\n\nWith `result_expires = 3600`, a result key has a TTL of about 3600 seconds."
    },
    {
      "id": "keep-in-flight-tasks-safe",
      "title": "Keep in-flight tasks safe",
      "role": "content",
      "text": "By default, a worker acknowledges a task before it runs it, so a worker that crashes\nmid-task loses that task. Set `task_acks_late` so the worker acknowledges the task after\nit finishes:\n\n[code example]\n\nWith late acknowledgment, Kombu holds each running task in a hash named `unacked`, with a\nsorted set named `unacked_index` that tracks when to give up on it. Both are empty again\nwhen the task completes. Your tasks must be\n[idempotent](https://redis.io/docs/latest/develop/data-types/streams/idempotency), because a task can run more\nthan once if a worker dies before acknowledging it.\n\nFor Redis, Celery redelivers a task that stays unacknowledged for longer than the\n*visibility timeout*. Set it with `broker_transport_options`, and make it longer than your\nlongest task or the longest `countdown` or `eta` you use. See the\n[Celery Redis documentation](https://docs.celeryq.dev/en/stable/getting-started/backends-and-brokers/redis.html)\nfor details and caveats:\n\n[code example]"
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "- [Celery documentation](https://docs.celeryq.dev/)\n- [Using Redis with Celery](https://docs.celeryq.dev/en/stable/getting-started/backends-and-brokers/redis.html)"
    }
  ],
  "examples": [
    {
      "id": "requirements-ex0",
      "language": "bash",
      "code": "pip install \"celery[redis]\"",
      "section_id": "requirements"
    },
    {
      "id": "configure-redis-as-the-broker-and-result-backend-ex0",
      "language": "python",
      "code": "from celery import Celery\n\napp = Celery(\n    \"tasks\",\n    broker=\"redis://<your-redis-host>:6379/0\",\n    backend=\"redis://<your-redis-host>:6379/1\",\n)\napp.conf.result_expires = 3600  # seconds",
      "section_id": "configure-redis-as-the-broker-and-result-backend"
    },
    {
      "id": "define-and-call-tasks-ex0",
      "language": "python",
      "code": "@app.task\ndef add(x, y):\n    return x + y\n\n@app.task(bind=True, max_retries=3, default_retry_delay=1)\ndef flaky(self):\n    if self.request.retries < 2:\n        raise self.retry(exc=RuntimeError(\"smtp down\"))\n    return f\"ok after {self.request.retries} retries\"",
      "section_id": "define-and-call-tasks"
    },
    {
      "id": "define-and-call-tasks-ex1",
      "language": "python",
      "code": "result = add.delay(2, 3)\nprint(result.get(timeout=10))                 # 5\n\nlate = add.apply_async((10, 5), countdown=2)  # run in about 2 seconds\nprint(late.get(timeout=10))                   # 15",
      "section_id": "define-and-call-tasks"
    },
    {
      "id": "define-and-call-tasks-ex2",
      "language": "bash",
      "code": "celery -A tasks worker --loglevel=INFO --concurrency=2",
      "section_id": "define-and-call-tasks"
    },
    {
      "id": "keep-in-flight-tasks-safe-ex0",
      "language": "python",
      "code": "app.conf.task_acks_late = True",
      "section_id": "keep-in-flight-tasks-safe"
    },
    {
      "id": "keep-in-flight-tasks-safe-ex1",
      "language": "python",
      "code": "app.conf.broker_transport_options = {\"visibility_timeout\": 3600}  # seconds",
      "section_id": "keep-in-flight-tasks-safe"
    }
  ]
}
