---
name: retry
description: This skill should be used when the user asks about "retry pattern", "when to use retry", "exponential backoff", "transient failure", "retry strategy", or mentions needing to handle temporary failures with automatic retries.
version: 1.0.0
---

# Retry Pattern

## What Is It
The Retry pattern handles transient failures by automatically retrying failed operations. It typically uses exponential backoff to avoid overwhelming the service and includes a maximum retry limit.

## When to Use
- **Network calls** - Temporary connectivity issues
- **External APIs** - Rate limiting, timeouts
- **Database operations** - Deadlocks, connection issues
- **Cloud services** - Eventual consistency scenarios

## When NOT to Use
- **Non-idempotent operations** - Retries cause duplicates
- **Permanent failures** - Retry won't help
- **User-facing operations** - Better to fail fast
- **Long operations** - Timeout before retry completes

## How to Implement

```typescript
async function retry<T>(
  fn: () => Promise<T>,
  maxRetries = 3,
  delay = 1000
): Promise<T> {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await fn();
    } catch (error) {
      if (i === maxRetries - 1) throw error;
      await sleep(delay * Math.pow(2, i));
    }
  }
  throw new Error("Max retries");
}
```

## Code Examples
- `examples/retry.ts` - Basic exponential backoff
- `examples/retry-advanced.ts` - Full-featured with jitter

## Related Patterns
- **Circuit Breaker** - Stops retries when service is down
- **Timeout** - Limit operation duration
- **Bulkhead** - Isolate failures
