<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mairon José Cuello Martinez</title>
    <description>The latest articles on DEV Community by Mairon José Cuello Martinez (@maironcuello).</description>
    <link>https://dev.to/maironcuello</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1076958%2Ffb5e8d80-32fd-44cc-b35c-3f89a98dff19.jpeg</url>
      <title>DEV Community: Mairon José Cuello Martinez</title>
      <link>https://dev.to/maironcuello</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maironcuello"/>
    <language>en</language>
    <item>
      <title>Construyendo un Checkout Resiliente en NestJS: Retry, Idempotencia y un Sistema que se Autoregula</title>
      <dc:creator>Mairon José Cuello Martinez</dc:creator>
      <pubDate>Wed, 20 May 2026 16:07:51 +0000</pubDate>
      <link>https://dev.to/maironcuello/construyendo-un-checkout-resiliente-en-nestjs-retry-idempotencia-y-un-sistema-que-se-autoregula-5973</link>
      <guid>https://dev.to/maironcuello/construyendo-un-checkout-resiliente-en-nestjs-retry-idempotencia-y-un-sistema-que-se-autoregula-5973</guid>
      <description>&lt;p&gt;El problema que nadie menciona&lt;/p&gt;

&lt;p&gt;Tines un payment gateway. Falla a veces. Entonces agregás un retry.&lt;/p&gt;

&lt;p&gt;Ahora tienes un problema peor: el cliente hace clic en "Pagar", el request llega a Stripe, el cobro se procesa, pero la respuesta nunca vuelve. El retry se ejecuta. Stripe cobra dos veces.&lt;/p&gt;

&lt;p&gt;Eso no es un escenario hipotético. Es el comportamiento por defecto de cualquier implementación naïve de retry, y ocurre en producción todos los días.&lt;/p&gt;

&lt;p&gt;Este post trata sobre cómo construimos un sistema de checkout que maneja esto correctamente — con retry que nunca cobra dos veces, un circuit breaker que protege el servicio cuando el gateway se degrada, y un feedback loop que ajusta su propia configuración bajo carga. Después lo pusimos a prueba con k6 y medimos todo.&lt;/p&gt;

&lt;p&gt;El código está en el ejemplo shopify-backend del monorepo (&lt;a href="https://github.com/BackendKit-labs/backendkit-monorepo/tree/master/examples/shopify-backend" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit-monorepo/tree/master/examples/shopify-backend&lt;/a&gt;).&lt;/p&gt;




&lt;p&gt;La arquitectura&lt;/p&gt;

&lt;p&gt;El flujo de una orden es un pipeline tipado de cuatro pasos:&lt;/p&gt;

&lt;p&gt;POST /orders&lt;br&gt;
    → ValidateInventoryStep     verifica stock, reserva unidades&lt;br&gt;
    → CalculatePricingStep      aplica descuentos, calcula total&lt;br&gt;
    → ChargePaymentStep         llama al gateway con retry + idempotencia&lt;br&gt;
    → CreateOrderStep           persiste la orden, emite eventos&lt;/p&gt;

&lt;p&gt;Cada paso recibe un OrderContext tipado, devuelve Result, y el pipeline se detiene ante el primer fallo.&lt;br&gt;
Sin excepciones, sin cadenas de try/catch — los errores son valores.&lt;/p&gt;

&lt;p&gt;El paso de pago es donde ocurre el trabajo interesante:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fumb1cvh6lzgc7on7rziz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fumb1cvh6lzgc7on7rziz.png" alt=" " width="800" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tres cosas a notar:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;La idempotency.key es charge:${ctx.orderId} — única por orden, no por request. Si el primer intento cobra exitosamente pero la respuesta se pierde, el retry impacta el cache de idempotencia y devuelve el resultado guardado. Stripe nunca se vuelve a llamar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;retryIf solo reintenta en 500/503 — no en 400/422/404. Los errores de negocio no se reintentan.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;jitter: 'full' aleatoriza el delay del backoff para evitar thundering herd cuando múltiples órdenes fallan simultáneamente.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Test 1: El baseline — tráfico normal&lt;/p&gt;

&lt;p&gt;Script: order-flow.k6.js — rampa hasta 50 VUs en 2 minutos, pipeline completo por iteración.&lt;/p&gt;

&lt;p&gt;✓ tasa de éxito:   96.58%&lt;br&gt;
  ✓ latencia p95:    2.03s&lt;br&gt;
    latencia promedio: 1.17s&lt;br&gt;
    throughput:      13.5 órdenes/segundo&lt;br&gt;
    tasa de fallo:   3.42%  (ruido del simulador)&lt;/p&gt;

&lt;p&gt;Con 50 usuarios concurrentes, el pipeline completo — verificación de inventario, pricing, pago, creación de orden — termina en 1.17 segundos en promedio. El 3.42% de fallo es el ruido de fondo configurado en el simulador de pagos, no errores de la aplicación.&lt;/p&gt;

&lt;p&gt;Este es el baseline. Todos los números que siguen se miden contra este.&lt;/p&gt;




&lt;p&gt;Test 2: Qué pasa cuando el gateway se degrada&lt;/p&gt;

&lt;p&gt;Script: circuit-breaker.k6.js — configura PAYMENT_FAILURE_RATE=0.8, rampa hasta 30 VUs.&lt;/p&gt;

&lt;p&gt;Sin circuit breaker, 80% de tasa de fallo significa que el 80% de los requests esperan el timeout completo del gateway antes de fallar. Con 30 VUs × 1-2 segundos de timeout = los threads se agotan, la cola se llena, el servicio entero se degrada — no solo los pagos.&lt;/p&gt;

&lt;p&gt;Con circuit breaker:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;health endpoint:       100% disponible durante todo el test
latencia promedio:     5.05ms
fast-fail:             ~5ms  (vs 1.17s del baseline)
http_req_failed:       49.99%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;El 49.99% de fallos se divide en exactamente la mitad: requests al health check (todos exitosos) y requests de pago (circuit abierto, fast-fail). Cuando el breaker se abre, los fallos de pago vuelven en 5 milisegundos en vez de esperar 1-2 segundos por un gateway que se sabe que está caído.&lt;/p&gt;

&lt;p&gt;El servicio nunca dejó de responder. Los endpoints de health se mantuvieron al 100% durante todo el test. El circuit breaker aisló el fallo de pagos del resto del sistema.&lt;/p&gt;




&lt;p&gt;Test 3: Retry + idempotencia con 60% de tasa de fallo&lt;/p&gt;

&lt;p&gt;Script: retry-idempotency.k6.js — tres escenarios concurrentes, 60% de fallo en gateway.&lt;/p&gt;

&lt;p&gt;Este es el escenario que más importa en e-commerce. Un gateway fallando al 60% es una dependencia degradada pero no muerta — exactamente cuando el retry es más valioso y más peligroso.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[retry_resilience]   tasa de éxito:       78.2%
[retry_resilience]   requests reintentadas: 825  con latencia &amp;gt;500ms
[idempotency_replay] replay rate:          100.0%
[lifecycle]          ciclos correctos:     4/4
p95 latencia (con reintentos):             1345ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;La matemática es exacta. Con 60% de fallo por intento y 3 intentos máximos, la probabilidad de que fallen los tres es 0.6³ = 21.6%. Tasa de fallo real: 21.8%. El retry funciona exactamente como predice el modelo probabilístico.&lt;/p&gt;

&lt;p&gt;Esas 825 requests con latencia &amp;gt;500ms son órdenes que fallaron en el primer intento pero tuvieron éxito en el retry. Sin retry, son ventas perdidas. Con retry, son transacciones completadas — y ninguna cobró dos veces al cliente.&lt;/p&gt;

&lt;p&gt;Idempotency replay: 100%. Cada request duplicado — simulando el escenario de "respuesta perdida en tránsito" — devolvió el resultado cacheado sin ejecutar el handler de pago. La tasa del 100% se mantuvo tanto en este test como en el test de idempotencia corrido de forma independiente.&lt;/p&gt;

&lt;p&gt;Lifecycle test: 4/4. Valida el comportamiento sutil pero crítico:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handler falla → clave no se cachea → retry ejecuta el handler de nuevo ✓&lt;/li&gt;
&lt;li&gt;Handler tiene éxito → clave cacheada → request duplicado devuelve replay ✓&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Una implementación naïve de idempotencia que cachea también los fallos bloquearía los reintentos legítimos. Esta no lo hace.&lt;/p&gt;




&lt;p&gt;Test 4: El contrato de idempotencia&lt;/p&gt;

&lt;p&gt;Script: idempotency.k6.js — cuatro escenarios paralelos, 1383 iteraciones totales.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;replay success rate:              100%
sin Idempotency-Key → 422:        30/30
formato de clave inválido → 422:  30/30
tasa de fallo general:            3.3%   (igual al baseline)
latencia p95:                     1322ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;El contrato se aplica en el borde. Un cliente que olvida enviar el header Idempotency-Key recibe un 422 — no un pass-through silencioso que bypasea la protección. Los formatos de clave inválidos se rechazan antes de tocar cualquier lógica de negocio.&lt;/p&gt;

&lt;p&gt;La tasa de fallo general del 3.3% es estadísticamente idéntica al baseline 3.42%. El layer de idempotencia no agrega latencia ni fallos al flujo normal.&lt;/p&gt;




&lt;p&gt;Test 5: El sistema que se regula a sí mismo&lt;/p&gt;

&lt;p&gt;Script: auto-learning.k6.js — tres fases durante 160 segundos.&lt;/p&gt;

&lt;p&gt;Esta es la parte que no tiene equivalente en el ecosistema NestJS.&lt;/p&gt;

&lt;p&gt;Fase 1 — Baseline  (t=0s):    5 VUs, 5% fallo, 100ms delay&lt;br&gt;
  Fase 2 — Stress    (t=50s):   25 VUs, 85% fallo, 1000ms delay&lt;br&gt;
  Fase 3 — Recovery  (t=110s):  5 VUs, 2% fallo, 80ms delay&lt;/p&gt;

&lt;p&gt;El módulo de auto-learning observa cada request, corre análisis de z-score sobre las distribuciones de latencia y error, y ajust la configuración en ciclos de 30 segundos.&lt;/p&gt;

&lt;p&gt;Esto es lo que mostraron los logs:&lt;/p&gt;

&lt;p&gt;t=0s    Config inicial:&lt;br&gt;
          timeoutMs=2804ms  maxRetries=2  cbFailureThreshold=10&lt;/p&gt;

&lt;p&gt;[t=50s a t=100s — el stress está corriendo, el sistema recolecta datos]&lt;/p&gt;

&lt;p&gt;t=105s  AUTO-LEARNING ACTÚA:&lt;br&gt;
          timeoutMs:  2804ms → 3916ms  (+40%)&lt;br&gt;
          maxRetries: 2 → 3            (+1)&lt;br&gt;
          cbFailureThreshold: sin cambio&lt;/p&gt;

&lt;p&gt;t=110s  Fase de recovery comienza&lt;br&gt;
 Config se mantiene — datos de recovery insuficientes para el próximo ciclo&lt;/p&gt;

&lt;p&gt;55 segundos desde que empezó el stress hasta el cambio autónomo de configuración. Dos decisiones tomadas sin intervención humana:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;timeoutMs +40% — el gateway respondía en ~1000ms. El sistema amplió su ventana de timeout para no fallar prematuramente requests que eventualmente iban a tener éxito.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;maxRetries +1 — tasa de error elevada detectada. Un intento más aumenta la probabilidad de recuperación del 78.4% al 91.6% bajo esas condiciones.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;El cbFailureThreshold se mantuvo en 10. El sistema identificó que la configuración del circuit breaker ya era correcta para el patrón observado y no la tocó.&lt;/p&gt;

&lt;p&gt;La config no revirtió durante el recovery. Eso es intencional — el sistema es conservador. Necesita evidencia sostenida de tráfico saludable antes de relajar umbrales, para no oscilar entre estados. En producción, ese es el comportamiento correcto.&lt;/p&gt;

&lt;p&gt;El health check del endpoint de auto-learning: 100% durante los 160 segundos completos.&lt;/p&gt;




&lt;p&gt;Lo que dicen los números en conjunto&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi19elpkws2tlvx0vi8c8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi19elpkws2tlvx0vi8c8.png" alt=" " width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Qué es esto y qué no es&lt;/p&gt;

&lt;p&gt;Esta es una implementación de referencia construida sobre BackendKit Labs (&lt;a href="https://github.com/BackendKit-labs/backendkit-monorepo" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit-monorepo&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;— una suite de resiliencia y observabilidad para NestJS que estamos construyendo y validando públicamente. El ejemplo shopify-backend existe específicamente para testear estos patrones bajo condiciones realistas y compartir los resultados.&lt;/p&gt;

&lt;p&gt;La suite es joven. Estos tests son parte del proceso de validación, no evidencia de hardening en producción. Si corrés patrones similares en tu propio código y encontrás edge cases, abrí un issue (&lt;a href="https://github.com/BackendKit-labs/backendkit-monorepo/issues" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit-monorepo/issues&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;— ese es exactamente el feedback que importa en esta etapa.&lt;/p&gt;

&lt;p&gt;El ejemplo completo, todos los scripts de k6 y el código fuente están en el monorepo&lt;br&gt;
(&lt;a href="https://github.com/BackendKit-labs/backendkit-monorepo/tree/master/examples/shopify-backend" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit-monorepo/tree/master/examples/shopify-backend&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Written by Mairon Cuello (&lt;a href="https://www.linkedin.com/in/maironcuellomartinez/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/maironcuellomartinez/&lt;/a&gt;) — Building open source resilience tooling for NestJS backends.&lt;/p&gt;

&lt;p&gt;GitHub: BackendKit-labs/backendkit-monorepo (&lt;a href="https://github.com/BackendKit-labs/backendkit-monorepo" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit-monorepo&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>spanish</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Building a Resilient Checkout in NestJS: Retry, Idempotency, and a System That Tunes Itself</title>
      <dc:creator>Mairon José Cuello Martinez</dc:creator>
      <pubDate>Wed, 20 May 2026 15:52:36 +0000</pubDate>
      <link>https://dev.to/maironcuello/building-a-resilient-checkout-in-nestjs-retry-idempotency-and-a-system-that-tunes-itself-38d8</link>
      <guid>https://dev.to/maironcuello/building-a-resilient-checkout-in-nestjs-retry-idempotency-and-a-system-that-tunes-itself-38d8</guid>
      <description>&lt;p&gt;The problem nobody talks about&lt;/p&gt;

&lt;p&gt;You have a payment gateway. It fails sometimes. So you add a retry.&lt;/p&gt;

&lt;p&gt;Now you have a worse problem: a customer clicks "Pay", the request reaches Stripe, the charge goes through, but the response never comes back. Your retry fires. Stripe charges them again.&lt;/p&gt;

&lt;p&gt;That's not a hypothetical. It's the default behavior of any naive retry implementation, and it happens in production every day.&lt;/p&gt;

&lt;p&gt;This post is about how we built a checkout system that handles this correctly — with retry that never double-charges, a circuit breaker that protects the service when the gateway is degraded, and a feedback loop that adjusts its own configuration under load.&lt;br&gt;
Then we stress-tested it with k6 and measured everything.&lt;/p&gt;

&lt;p&gt;The code is in the backendkit-monorepo shopify-backend example&lt;br&gt;
(&lt;a href="https://github.com/BackendKit-labs/backendkit-monorepo/tree/master/examples/shopify-backend" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit-monorepo/tree/master/examples/shopify-backend&lt;/a&gt;).&lt;/p&gt;




&lt;p&gt;The architecture&lt;/p&gt;

&lt;p&gt;The order flow is a typed pipeline of four steps:&lt;/p&gt;

&lt;p&gt;POST /orders&lt;br&gt;
  → ValidateInventoryStep     checks stock, reserves units&lt;br&gt;
  → CalculatePricingStep      applies discounts, computes total&lt;br&gt;
  → ChargePaymentStep         calls payment gateway with retry + idempotency&lt;br&gt;
  → CreateOrderStep           persists order, emits events&lt;/p&gt;

&lt;p&gt;Each step receives a typed OrderContext, returns Result, and the pipeline stops at the first failure. No exceptions, no try/catch chains — errors are values.&lt;/p&gt;

&lt;p&gt;The payment step is where the interesting work happens:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqw7yo1j8zeupelmssoj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqw7yo1j8zeupelmssoj.png" alt=" " width="800" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Three things to notice:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The idempotency.key is charge:${ctx.orderId} — unique per order, not per request. If the first attempt charges successfully but the response is lost, the retry hits the idempotency cache and returns the stored result. Stripe is never called again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;retryIf only retries on 500/503 — not on 400/422/404. Business errors don't retry.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. jitter: 'full' randomizes the backoff delay to prevent thundering herd when multiple orders fail simultaneously.
&lt;/h2&gt;

&lt;p&gt;Test 1: The baseline — normal traffic&lt;/p&gt;

&lt;p&gt;Script: order-flow.k6.js — ramps to 50 VUs over 2 minutes, full pipeline per iteration.&lt;/p&gt;

&lt;p&gt;✓ success rate:    96.58%&lt;br&gt;
  ✓ p95 latency:     2.03s&lt;br&gt;
    avg latency:     1.17s&lt;br&gt;
    throughput:      13.5 orders/second&lt;br&gt;
    fail rate:       3.42%  (simulated gateway noise)&lt;/p&gt;

&lt;p&gt;Under 50 concurrent users, the full pipeline — inventory check, pricing, payment, order creation — completes in 1.17 seconds on average. The 3.42% failure rate is the configured background noise of the payment simulator, not application errors.&lt;/p&gt;

&lt;p&gt;This is the baseline. Every number that follows is measured against this.&lt;/p&gt;




&lt;p&gt;Test 2: What happens when the gateway degrades&lt;/p&gt;

&lt;p&gt;Script: circuit-breaker.k6.js — sets PAYMENT_FAILURE_RATE=0.8, ramps to 30 VUs.&lt;/p&gt;

&lt;p&gt;Without a circuit breaker, 80% failure rate means 80% of requests wait for the full gateway timeout before failing. &lt;br&gt;
With 30 VUs × 1-2 second timeout = threads exhaust, &lt;br&gt;
queue backs up, the entire service starts degrading — not just payments.&lt;/p&gt;

&lt;p&gt;With a circuit breaker:&lt;/p&gt;

&lt;p&gt;health endpoint:       100% reachable throughout&lt;br&gt;
  avg response time:     5.05ms&lt;br&gt;
  fast-fail response:    ~5ms  (vs 1.17s baseline)&lt;br&gt;
  http_req_failed:       49.99%&lt;/p&gt;

&lt;p&gt;The 49.99% failure rate splits exactly in half: health check requests (all succeed) and payment requests (circuit open, fast-fail).&lt;br&gt;
When the breaker trips, payment failures come back in 5 milliseconds instead of waiting 1-2 seconds for a gateway that's known to be down.&lt;/p&gt;

&lt;p&gt;The service never stopped responding. Health endpoints stayed at 100% throughout. The circuit breaker isolated the payment failure from the rest of the system.&lt;/p&gt;




&lt;p&gt;Test 3: Retry + idempotency under 60% failure rate&lt;/p&gt;

&lt;p&gt;Script: retry-idempotency.k6.js — three concurrent scenarios, 60% gateway failure rate.&lt;/p&gt;

&lt;p&gt;This is the scenario that matters most for e-commerce. A gateway failing 60% of the time is a degraded but not dead dependency — exactly when retry is most valuable and most dangerous.&lt;/p&gt;

&lt;p&gt;[retry_resilience]   success rate:      78.2%&lt;br&gt;
  [retry_resilience]   retried requests:  825  with latency &amp;gt;500ms&lt;br&gt;
  [idempotency_replay] replay rate:       100.0%&lt;br&gt;
  [lifecycle]          correct cycles:    4/4&lt;br&gt;
  p95 latency (with retries):             1345ms&lt;/p&gt;

&lt;p&gt;The math checks out. With 60% failure per attempt and 3 max attempts, probability of all three failing = 0.6³ = 21.6%. Actual failure rate: 21.8%. The retry is working exactly as the probability model predicts.&lt;/p&gt;

&lt;p&gt;Those 825 requests with latency &amp;gt;500ms are orders that failed on the first attempt but succeeded on retry. Without retry, they're lost sales. With retry, they're completed transactions — and none of them charged the customer twice.&lt;/p&gt;

&lt;p&gt;Idempotency replay: 100%. Every duplicate request — simulating the "response lost in transit" scenario — returned the cached result without executing the payment handler. The 100% rate held across both this test and the dedicated idempotency test run independently.&lt;/p&gt;

&lt;p&gt;Lifecycle test: 4/4. This validates the subtle but critical behavior:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handler fails → key not cached → retry executes handler again ✓&lt;/li&gt;
&lt;li&gt;Handler succeeds → key cached → duplicate request returns replay ✓&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A naive idempotency implementation that caches failures would block legitimate retries. This one doesn't.&lt;/p&gt;




&lt;p&gt;Test 4: The idempotency contract&lt;/p&gt;

&lt;p&gt;Script: idempotency.k6.js — four parallel scenarios, 1383 total iterations.&lt;/p&gt;

&lt;p&gt;replay success rate:    100%&lt;br&gt;
  missing Idempotency-Key → 422:   30/30&lt;br&gt;
  invalid key format → 422:        30/30&lt;br&gt;
  overall fail rate:               3.3%   (same as baseline)&lt;br&gt;
  p95 latency:                     1322ms&lt;/p&gt;

&lt;p&gt;The contract is enforced at the boundary. A client that forgets to send an Idempotency-Key header gets a 422 — not a silent pass-through that bypasses the protection. Invalid key formats are rejected before touching any business logic.&lt;/p&gt;

&lt;p&gt;The 3.3% overall failure rate is statistically identical to the baseline 3.42%. The idempotency layer adds zero latency and zero failures to the normal flow.&lt;/p&gt;




&lt;p&gt;Test 5: The system that tunes itself&lt;/p&gt;

&lt;p&gt;Script: auto-learning.k6.js — three phases over 160 seconds.&lt;/p&gt;

&lt;p&gt;This is the part that has no equivalent in the NestJS ecosystem.&lt;/p&gt;

&lt;p&gt;Phase 1 — Baseline  (t=0s):    5 VUs, 5% failure, 100ms delay&lt;br&gt;
  Phase 2 — Stress    (t=50s):   25 VUs, 85% failure, 1000ms delay&lt;br&gt;
  Phase 3 — Recovery  (t=110s):  5 VUs, 2% failure, 80ms delay&lt;/p&gt;

&lt;p&gt;The auto-learning module observes every request, runs z-score analysis on latency and error distributions, and adjusts configuration on a 30-second feedback cycle.&lt;/p&gt;

&lt;p&gt;Here's what the logs showed:&lt;/p&gt;

&lt;p&gt;t=0s    Initial config:&lt;br&gt;
          timeoutMs=2804ms  maxRetries=2  cbFailureThreshold=10&lt;/p&gt;

&lt;p&gt;[t=50s to t=100s — stress is running, system is collecting data]&lt;/p&gt;

&lt;p&gt;t=105s  AUTO-LEARNING ADJUSTS:&lt;br&gt;
          timeoutMs:  2804ms → 3916ms  (+40%)&lt;br&gt;
          maxRetries: 2 → 3            (+1)&lt;br&gt;
          cbFailureThreshold: unchanged&lt;/p&gt;

&lt;p&gt;t=110s  Recovery phase begins&lt;br&gt;
          Config maintained — insufficient recovery data for next cycle&lt;/p&gt;

&lt;p&gt;55 seconds from stress beginning to autonomous configuration change. Two decisions made without human intervention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;timeoutMs +40% — the gateway was responding in ~1000ms. The system widened its timeout window to avoid prematurely failing requests that would eventually succeed.

&lt;ul&gt;
&lt;li&gt;maxRetries +1 — high failure rate detected. One more retry attempt increases recovery probability from 78.4% to 91.6% under those conditions.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;The cbFailureThreshold stayed at 10. The system identified that the circuit breaker configuration was already correct for the observed pattern and left it alone.&lt;/p&gt;

&lt;p&gt;The config did not revert during recovery. This is intentional — the system is conservative. It needs sustained evidence of healthy traffic before relaxing thresholds, to avoid oscillating between states. In production, that's the right behavior.&lt;/p&gt;

&lt;p&gt;The health check on the auto-learning endpoint: 100% throughout all 160 seconds.&lt;/p&gt;




&lt;p&gt;What the numbers say together&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0kv72r784bsgfan1j8o6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0kv72r784bsgfan1j8o6.png" alt=" " width="800" height="354"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;p&gt;What this is and what it isn't&lt;/p&gt;

&lt;p&gt;This is a reference implementation built on BackendKit Labs (&lt;a href="https://github.com/BackendKit-labs/backendkit-monorepo" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit-monorepo&lt;/a&gt;) — a suite o resilience and observability packages for NestJS we're building and validating publicly. The shopify-backend example exists specifically to test these patterns under realistic conditions and share the results.&lt;/p&gt;

&lt;p&gt;The suite is young. These tests are part of the validation process, not proof of production hardening. If you run similar patterns in your own codebase and find edge cases, open an issue (&lt;a href="https://github.com/BackendKit-labs/backendkit-monorepo/issues" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit-monorepo/issues&lt;/a&gt;) — that's exactly the feedback that matters at this stage.&lt;/p&gt;

&lt;p&gt;The full example, all k6 scripts, and the source are in the monorepo&lt;br&gt;
(&lt;a href="https://github.com/BackendKit-labs/backendkit" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit&lt;/a&gt; monorepo/tree/master/examples/shopify-backend).&lt;/p&gt;

&lt;p&gt;Written by Mairon Cuello (&lt;a href="https://www.linkedin.com/in/maironcuellomartinez/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/maironcuellomartinez/&lt;/a&gt;) — Building open source resilience tooling for NestJS backends.&lt;br&gt;
GitHub: BackendKit-labs/backendkit-monorepo (&lt;a href="https://github.com/BackendKit-labs/backendkit-monorepo" rel="noopener noreferrer"&gt;https://github.com/BackendKit-labs/backendkit-monorepo&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>node</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
