Skip to content

Support configuring additional connection errors for Redis backend (e.g., ReadOnlyError) #10119

Closed

Description

@piotrszyma

Checklist

  • I have checked the issues list
    for similar or identical feature requests.
  • I have checked the pull requests list
    for existing proposed implementations of this feature.
  • I have checked the commit log
    to find out if the same feature was already implemented in the
    main branch.
  • I have included all related issues and possible duplicate issues
    in this issue (If there are none, check this box anyway).

Related Issues and Possible Duplicates

Related Issues

  • None

Possible Duplicates

  • None

Brief Summary

Celery's Redis backend does not provide a way to configure additional exceptions that should be treated as connection errors and trigger reconnection. This is needed for handling failover scenarios in Redis-compatible databases (e.g., Dragonfly) where redis.exceptions.ReadOnlyError is raised when a worker still has a TCP connection to a demoted replica.

Use case: We use Dragonfly (Redis-compatible database) in a master-replica setup. During failover:

  1. The master fails or is demoted
  2. A replica is promoted to become the new master
  3. Celery workers still have TCP connections to the old replica (now demoted)
  4. Write operations raise redis.exceptions.ReadOnlyError
  5. Celery treats this as a regular exception, not a connection error, so tasks fail instead of retrying with a fresh connection

Design

Architectural Considerations

The RedisBackend class uses a connection_errors tuple to determine which exceptions trigger retry logic in ensure() and exception_safe_to_retry(). Additionally, the ResultConsumer class caches this tuple during __init__, so any modification must update both places.

Reference: https://github.com/celery/celery/blob/main/celery/backends/redis.py#L91

Proposed Behavior

When a configured exception (e.g., ReadOnlyError) is raised during a Redis operation:

  1. The exception is recognized as a connection error
  2. The current connection is disconnected
  3. Fresh DNS resolution occurs
  4. A new connection is established (connecting to the new master after failover)
  5. The operation is retried

This matches the behavior that can be configured i.e. redis-py via retry_on_error=[ReadOnlyError] parameter.

Proposed UI/UX

Option 1: Transport option for additional connection errors

result_backend_transport_options = {
    "additional_connection_errors": ["redis.exceptions.ReadOnlyError"],
}

broker_transport_options = {
    "additional_connection_errors": ["redis.exceptions.ReadOnlyError"],
}

Option 2: Include ReadOnlyError in connection_errors by default

Since ReadOnlyError typically indicates a failover scenario where reconnection is the correct action, it could be included in the default connection_errors tuple.

Diagrams

N/A

Alternatives

Current workaround: Custom backend

We implemented a custom backend that adds ReadOnlyError to connection_errors:

from celery.backends.redis import RedisBackend
from redis import exceptions as redis_exceptions


class RedisBackendReconnectOnReadOnlyError(RedisBackend):
    """Custom Redis backend that treats ReadOnlyError as a connection error."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Add ReadOnlyError to connection_errors tuple.
        self.connection_errors = self.connection_errors + (
            redis_exceptions.ReadOnlyError,
        )

        # IMPORTANT: result_consumer copies connection_errors during __init__.
        # We must update its copy as well.
        self.result_consumer._connection_errors = self.connection_errors

Registered via:

override_backends = {
    "redis": "myapp.celery.backends.redis:RedisBackendReconnectOnReadOnlyError",
    "rediss": "myapp.celery.backends.redis:RedisBackendReconnectOnReadOnlyError",
}

This workaround is fragile because it relies on the internal _connection_errors attribute of ResultConsumer, which could change in future Celery versions. A built-in solution would be more maintainable.

Activity

  1. xXMrNidaXx commented on Feb 25, 2026

    @xXMrNidaXx

    Supporting ReadOnlyError for Redis failover scenarios is important!

    Current workaround:

    from celery import Celery
    from redis.exceptions import ReadOnlyError
    from kombu.transport.redis import Transport
    
    # Monkey-patch recoverable errors
    original_errors = Transport.recoverable_connection_errors
    Transport.recoverable_connection_errors = original_errors + (ReadOnlyError,)
    
    app = Celery('tasks')

    Better - custom transport:

    from kombu.transport.redis import Transport as RedisTransport
    from redis.exceptions import ReadOnlyError
    
    class ResilientRedisTransport(RedisTransport):
        recoverable_connection_errors = RedisTransport.recoverable_connection_errors + (
            ReadOnlyError,
        )
    
    # Register transport
    from kombu.transport import TRANSPORT_ALIASES
    TRANSPORT_ALIASES['resilient-redis'] = 'myapp.transport:ResilientRedisTransport'
    
    app.conf.broker_url = 'resilient-redis://localhost:6379/0'

    For Redis Sentinel/Cluster:

    app.conf.broker_transport_options = {
        'master_name': 'mymaster',
        'sentinel_kwargs': {'socket_timeout': 1.0},
        'retry_on_timeout': True,
    }

    +1 for making this configurable in core!


    🔧 Building distributed systems? We're hiring at RevolutionAI:
    https://www.revolutionai.io/find-work

    Production AI projects • Great rates 💼

  2. auvipy commented on Mar 1, 2026

    @auvipy
    Member

    contributions are welcome

  3. abdullahmujahidali commented on Mar 1, 2026

    @abdullahmujahidali
    Contributor

    I'd like to work on this

  4. added this to the 5.7.0 milestone on Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions