Retry Policies

Noting is perfect, errors and timeouts may happen, and when such failures happen, the consumer has to decide what to do with that. By default, the consumer would reject the envelope (RabbitMQ message) when a failure happens. However, errors and timeouts issues, unless there is a software bug, usually solved with retries. Just like the routing, the consumer doesn’t make the retry decision itself, the consumer delegates it to a retry policy. Retry policy defines how the retry is performed. Retries usually happens with back-offs to avoid worsening the situation by hammering other services with more requests, especially if it was a timeout issue. The consumer can be configured to use a retry policy by calling Consumer.set_retry_policy(), passing an instance of RetryPolicy. When a retry policy is set, the consumer won’t reject messages, but rather, it send them to the retry policy to deal with the situation by invoking RetryPolicy.retry() method. Based on it’s implementation, The retry policy decides how to do retries.

There are 4 different retry policies available:

  1. UnlimitedRetriesPolicy, Unlimited retries policy
  2. LimitedRetriesPolicy, Limited retries policy
  3. FixedDelayUnlimitedRetriesPolicy, Fixed delay unlimited retries policy
  4. FixedDelayLimitedRetriesPolicy, Fixed delay limited retries policy

Custom retry policies can be created by implementing the base class RetryPolicy

Retry Policy

class rabbitleap.retry_policies.RetryPolicy(**kwargs)[source]

Base class for retry policies.

Subclasses MUST implement retry() method.

Unlimited Retries Policy

class rabbitleap.retry_policies.UnlimitedRetriesPolicy(consumer, initial_delay, max_delay, delay_incremented_by, retry_queue_suffix='retry', **kwargs)[source]

Bases: rabbitleap.retry_policies.BaseRetryPolicy

Unlimited Retries Policy.

This is an implementation of RetryPolicy which does incremental backoff, unlimited retries.

initial_delay: is the initial/first backoff delay in seconds

delay_incremented_by: is number of seconds the backoff should be incremented by after each death

max_delay: is the final/maximum backoff delay in seconds that should net be exceeded

UnlimitedRetriesPolicy.retry(envelope)[source]

Send message to retry queue to retry handling it later.

Death count is calculated by examining ‘x-death’ header. Based on the death count, the message is sent to a retry queue where it waits there till it expires and gets sent back to the original queue for handling retry.

Parameters:envelope (Envelope) – Message envelope

Limited Retries Policy

class rabbitleap.retry_policies.LimitedRetriesPolicy(consumer, retry_delays, retry_queue_suffix='retry', **kwargs)[source]

Bases: rabbitleap.retry_policies.BaseRetryPolicy

Limited Retries Policy.

This is an implementation of RetryPolicy which does incremental backoff, limited number of retries.

consumer: message consumer instance

retry_delays: immutable list of retry backoff delays in seconds. Message is sent to dlx when this list is exhausted. e.g (1, 5, 10, 60, 5 * 60)

retry_queue_suffix: suffix str used when naming retry queues.

LimitedRetriesPolicy.retry(envelope)[source]

Send message to retry queue to retry handling it later.

Death count is calculated by examining ‘x-death’ header. Based on the death count, the message is sent to a retry queue where it waits there till it expires and gets sent back to the original queue for handling retry.

The death count is used as an index for retry_delays list. Where each item in the list represents a retry delay in seconds.

The message will be rejected if the death count exceeded the length of retry_delays list.

Parameters:envelope (Envelope) – Message envelope

Fixed Delay Unlimited Retries Policy

class rabbitleap.retry_policies.FixedDelayUnlimitedRetriesPolicy(consumer, delay, retry_queue_suffix='retry', **kwargs)[source]

Bases: rabbitleap.retry_policies.UnlimitedRetriesPolicy

Fixed delay unlimited retries policy.

This is an implementation of RetryPolicy which does fix backoff delay, unlimited retries.

consumer: consumer instance

delay: retry delay in seconds

retry_queue_suffix: suffix str used when naming retry queues.

FixedDelayUnlimitedRetriesPolicy.retry(envelope)

Send message to retry queue to retry handling it later.

Death count is calculated by examining ‘x-death’ header. Based on the death count, the message is sent to a retry queue where it waits there till it expires and gets sent back to the original queue for handling retry.

Parameters:envelope (Envelope) – Message envelope

Fixed Delay Limited Retries Policy

class rabbitleap.retry_policies.FixedDelayLimitedRetriesPolicy(consumer, delay, retries_limit, retry_queue_suffix='retry', **kwargs)[source]

Bases: rabbitleap.retry_policies.LimitedRetriesPolicy

Fixed delay limited retries policy.

This is an implementation of RetryPolicy which does fix backoff delay, limited number of retries.

consumer: consumer instance

delay: retry delay in seconds.

retries_limit: retries limit count.

retry_queue_suffix: suffix str used when naming retry queues.

FixedDelayLimitedRetriesPolicy.retry(envelope)

Send message to retry queue to retry handling it later.

Death count is calculated by examining ‘x-death’ header. Based on the death count, the message is sent to a retry queue where it waits there till it expires and gets sent back to the original queue for handling retry.

The death count is used as an index for retry_delays list. Where each item in the list represents a retry delay in seconds.

The message will be rejected if the death count exceeded the length of retry_delays list.

Parameters:envelope (Envelope) – Message envelope