SOLID & Design Patterns Interview Question #89

What is the Retry Pattern?

DDD Boundaries, Resilience, Caching & Practical ASP.NET Core Design Mid-Level Intermediate

Quick Interview Answer

The Retry Pattern repeats an operation after a transient failure that may succeed later, such as a temporary network interruption or service throttling. Retries should normally use limits and suitable delays or backoff.

Detailed Explanation

The Retry Pattern repeats an operation after a transient failure that may succeed later, such as a temporary network interruption or service throttling. Retries should normally use limits and suitable delays or backoff.

Retries are inappropriate for permanent failures such as invalid input. Retrying non-idempotent operations without safeguards can also duplicate business effects.

Code Example

for (var attempt = 1; attempt <= 3; attempt++)
{
    try
    {
        await remoteService.CallAsync();
        break;
    }
    catch (TransientException) when (attempt < 3)
    {
        await Task.Delay(TimeSpan.FromSeconds(attempt));
    }
}