Microservices Communication Patterns (Saga, Circuit Breaker) · Lección

Orden de los decoradores de resiliencia

Aprenda por qué el orden en que apila los patrones de resiliencia (reintentos, circuit breaker, bulkhead, timeout y limitador de velocidad) cambia el comportamiento, y cómo elegir el orden adecuado.

Lección 4 de 413 pasos

Orden de los decoradores de resiliencia es una lección gratuita de Microservices Communication Patterns (Saga, Circuit Breaker) en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Microservices Communication Patterns (Saga, Circuit Breaker), y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Microservices Communication Patterns (Saga, Circuit Breaker) incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Stacking Patterns

Resilience libraries let you wrap a call in multiple patterns at once: retry, circuit breaker, bulkhead, time limiter, rate limiter. The patterns form a chain, and order matters.

The same set of patterns produces different behavior depending on how they are nested.

Decorators as Layers

Think of each pattern as a layer wrapping the next. The outermost layer sees the call first; the innermost actually invokes the dependency.

layers = ['Retry', 'CircuitBreaker', 'Bulkhead', 'TimeLimiter', 'ServiceCall']
for i, l in enumerate(layers):
    print('  ' * i + l)

Retry Outside the Breaker

The recommended Resilience4j order puts Retry outside the Circuit Breaker.

This means each retry attempt is itself evaluated by the breaker. After enough failed attempts, the breaker opens and stops further retries entirely.

What If Retry Were Inside?

If Retry were inside the breaker, the breaker would only see one outcome per full retry cycle. A burst of internal retries could hammer a failing service before the breaker ever notices.

That is why retry usually wraps the breaker, not the other way around.

Bulkhead Placement

Place the Bulkhead inside the breaker so it limits concurrency on the actual calls. The breaker can then short-circuit before a thread is even acquired, keeping the bulkhead from filling with doomed calls.

breaker_open = True

def call(breaker_open):
    if breaker_open:
        return 'short-circuited; no bulkhead slot used'
    return 'acquire bulkhead slot then call'

print(call(breaker_open))

Time Limiter Placement

The TimeLimiter sits close to the call so each individual attempt is bounded. A slow call times out, counts as a failure for the breaker, and can trigger a retry from the outer layer.

Rate Limiter Placement

Put the RateLimiter on the outside if you want to cap total request rate including retries, or inside if you only want to cap actual dependency calls. Decide based on what you are protecting.

Recommended Default Order

A widely used default, from outermost to innermost:

  • Retry
  • Circuit Breaker
  • Rate Limiter
  • Time Limiter
  • Bulkhead

Adjust to your goals, but understand each move's effect.

Fallback Goes Outermost

The fallback should wrap everything so it can catch failures from any inner layer, including a retry that exhausted attempts or a breaker that is open.

def with_fallback(inner):
    try:
        return inner()
    except Exception:
        return 'fallback value'

print(with_fallback(lambda: (_ for _ in ()).throw(Exception('all layers failed'))))

Interactions to Watch

Watch for surprising combos:

  • Retry plus a rate limiter can amplify load if not bounded.
  • A short time limiter inside aggressive retry can storm a slow service.
  • Bulkhead rejections may count as breaker failures.

Validate by Testing

Because ordering effects are subtle, validate your chain with integration tests that inject failures and slowness, then assert the observed behavior matches your intent.

Quick Check

Why is Retry typically placed OUTSIDE the Circuit Breaker rather than inside it?

Recap

You learned why decorator order matters:

  • Patterns nest as layers; outermost runs first.
  • Retry outside the breaker lets the breaker stop retry storms.
  • Bulkhead and time limiter sit close to the call.
  • Fallback wraps everything.

Choose order by intent, then verify it with failure-injection tests.

Gratis para empezar

Aprende Microservices Communication Patterns (Saga, Circuit Breaker) con un tutor de IA — gratis

Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.

Cursos
12
Lecciones
48

Preguntas frecuentes

¿La lección «Orden de los decoradores de resiliencia» es gratis?

Sí — el texto completo de «Orden de los decoradores de resiliencia» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Microservices Communication Patterns (Saga, Circuit Breaker), actualiza a CoddyKit PRO. El curso de Microservices Communication Patterns (Saga, Circuit Breaker) incluye 4 lecciones en total.

¿Qué aprenderé en «Orden de los decoradores de resiliencia»?

Aprenda por qué el orden en que apila los patrones de resiliencia (reintentos, circuit breaker, bulkhead, timeout y limitador de velocidad) cambia el comportamiento, y cómo elegir el orden adecuado. Practicas Microservices Communication Patterns (Saga, Circuit Breaker) con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Microservices Communication Patterns (Saga, Circuit Breaker)?

No se requiere experiencia previa. Microservices Communication Patterns (Saga, Circuit Breaker) en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Orden de los decoradores de resiliencia»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Microservices Communication Patterns (Saga, Circuit Breaker)?

Sí. Cada lección de Microservices Communication Patterns (Saga, Circuit Breaker) incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Circuit breaker y bulkhead
  2. Circuit breaker con lógica de reintento
  3. Integración de la limitación de frecuencia
  4. Orden de los decoradores de resiliencia
← Volver a Microservices Communication Patterns (Saga, Circuit Breaker)