retry pattern
Resilience Patterns

Meaning

The retry pattern repeatedly attempts a potentially flaky operation until it succeeds or a maximum number of attempts is reached. It addresses the pain point of transient failures (e.g., network timeouts, temporary database locks) that would otherwise cause the program to abort. Learners should reach for this pattern whenever an operation can be safely retried without side‑effects.

Primary Function

Error handling

Communicative Purpose

Ensures operation is retried upon transient failures

Pattern

for attempt in range(max_retries): try: result = operation() break except transient_error as e: if attempt < max_retries - 1: sleep(backoff) continue else: raise

Core Structure

for ... in range(...): try: ... except ...: ...

Função primária

Error handling

Propósito comunicativo

Ensures operation is retried upon transient failures

Situações de gatilho

Web API client: transient HTTP 502 errors; Database access: deadlock detection and retry; Message queue consumer: temporary connection loss

Contextos

Network clients, database access layers, message‑queue consumers, cloud SDK wrappers

Padrão

for attempt in range(max_retries): try: result = operation() break except transient_error as e: if attempt < max_retries - 1: sleep(backoff) continue else: raise

Estrutura central

for ... in range(...): try: ... except ...: ...

Slots de substituição

attempt: int counter, max_retries: int ≥ 1, operation: callable, transient_error: exception type, backoff: float seconds

Colocados típicos

  • try/except
  • sleep
  • logging
  • backoff
  • raise

Substituições comuns

  • while loop instead of for
  • decorator‑based retry wrapper
  • using the tenacity library for advanced policies

Erros comuns

Retrying non‑idempotent operations leading to duplicate side‑effects; forgetting to break after success causing unnecessary extra attempts; catching overly broad exceptions and masking real bugs

Similar / contraste

Circuit breaker pattern – stops retries after repeated failures; Fallback pattern – provides an alternative result instead of retrying

Interferências

Coming from JavaScript: using setTimeout inside catch does not block execution – in Python time.sleep blocks the thread and provides the intended pause

Família do chunk

  • error handling
  • resilience
  • circuit breaker

Nuance

Do not use when the operation has side‑effects that cannot be repeated safely; each retry adds latency and may increase load on the remote service; ensure max_retries is bounded to avoid infinite loops

Efeito pragmático

Prevents transient errors from crashing the application and improves overall reliability of services that depend on unstable external resources

Dica de memória

Retry pattern is like a safety net that catches a fall and lets you stand up and try again.

Nota

Verify that the operation is idempotent or otherwise safe to repeat before applying the retry pattern

Upgrade path

Exponential backoff with jitter

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: code patternPrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Medium-term

Log in to save chunks.