❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Learning Notes #23 – Retry Pattern | Cloud Patterns

31 December 2024 at 17:34

Today, i refreshed Retry pattern. It handles transient failures ( network issues, throttling, or temporary unavailability of a service).

The Retry Pattern provides a structured approach to handle these failures gracefully, ensuring system reliability and fault tolerance. It is often used in conjunction with related patterns like the Circuit Breaker, which prevents repeated retries during prolonged failures, and the Bulkhead Pattern, which isolates system components to prevent cascading failures.

In this blog, i jot down my notes on Retry pattern for better understanding.

What is the Retry Pattern?

The Retry Pattern is a design strategy used to manage transient failures by retrying failed operations. Instead of immediately failing an operation after an error, the pattern retries it with an optional delay or backoff strategy. This is particularly useful in distributed systems where failures are often temporary.

Key Components of the Retry Pattern

  • Retry Logic: The mechanism that determines how many times to retry and under what conditions.
  • Backoff Strategy: A delay mechanism to space out retries. Common strategies include fixed, incremental, and exponential backoff.
  • Termination Policy: A limit on the number of retries or a timeout to prevent infinite retry loops.
  • Error Handling: A fallback mechanism to gracefully handle persistent failures after retries are exhausted.

Retry Pattern Strategies

1. Fixed Interval Retry

  • Retries are performed at regular intervals.
  • Example: Retry every 2 seconds for up to 5 attempts.

2. Incremental Backoff

  • Retry intervals increase linearly.
  • Example: Retry after 1, 2, 3, 4, and 5 seconds.

3. Exponential Backoff

  • Retry intervals grow exponentially, often with jitter to randomize delays.
  • Example: Retry after 1, 2, 4, 8, and 16 seconds.

4. Custom Backoff

  • Tailored to specific use cases, combining strategies or using domain-specific logic.

Implementing the Retry Pattern in Python with Tenacity

tenacity is a powerful Python library that simplifies the implementation of the Retry Pattern. It provides built-in support for various retry strategies, including fixed interval, incremental backoff, and exponential backoff with jitter.

Example with Fixed Interval Retry


from tenacity import retry, stop_after_attempt, wait_fixed

@retry(stop=stop_after_attempt(5), wait=wait_fixed(2))
def example_operation():
    print("Trying operation...")
    raise Exception("Transient error")

try:
    example_operation()
except Exception as e:
    print(f"Operation failed after retries: {e}")

Example with Exponential Backoff and Jitter


from tenacity import retry, stop_after_attempt, wait_exponential_jitter

@retry(stop=stop_after_attempt(5), wait=wwmultiplier=1, max=10))
def example_operation():
    print("Trying operation...")
    raise Exception("Transient error")

try:
    example_operation()
except Exception as e:
    print(f"Operation failed after retries: {e}")

Example with Custom Termination Policy


from tenacity import retry, stop_after_delay, wait_exponential

@retry(stop=stop_after_delay(10), wait=wait_exponential(multiplier=1))
def example_operation():
    print("Trying operation...")
    raise Exception("Transient error")

try:
    example_operation()
except Exception as e:
    print(f"Operation failed after retries: {e}")

Real-World Use Cases

  • API Rate Limiting – Retrying failed API calls when encountering HTTP 429 errors.
  • Database Operations – Retrying failed database queries due to deadlocks or transient connectivity issues.
  • File Uploads/Downloads – Retrying uploads or downloads in case of network interruptions.
  • Message Processing – Retries for message processing failures in systems like RabbitMQ or Kafka.

❌
❌