डिजिटल वस्तुओं का थोक मंच

B2B Integrations के लिए Retry/Backoff Patterns 2026: Polly, axios-retry, tenacity

B2B integrations के लिए production retry/backoff patterns: exponential + jitter, circuit breaker, 3 languages में code samples।

B2B Integrations के लिए Retry/Backoff Patterns 2026: Polly, axios-retry, tenacity

Retry का मतलब "while loop के अंदर try/catch wrap" नहीं है। सही strategy error class, upstream service की expected SLA, और recovery के दौरान current load पर depend करती है।

FoxReload-specific note: POST /api/orders retry करने से पहले GET /api/orders/{order_id} से status check करें — FoxReload में idempotency keys नहीं हैं। Order बना या नहीं, verify करके retry करें।

1. Base formula: exponential + jitter

function delay(attempt: number, baseMs = 500, capMs = 30000): number {
  const exp = Math.min(baseMs * Math.pow(2, attempt), capMs);
  const jitter = Math.random() * exp * 0.5; // ±50%
  return exp / 2 + jitter; // "full jitter" AWS-style
}
// attempt 0: 250–750ms
// attempt 1: 500–1500ms
// attempt 2: 1000–3000ms
// attempt 5: 8000–24000ms

"Full jitter" AWS-recommended है और "equal jitter" से recovery पर peak load ज़्यादा कम करता है।

2. कब retry करें और कब नहीं

HTTP status Retryable? Comment
5xx Yes Server-side transient
429 Yes Retry-After header respect करें
408 Yes Client timeout
4xx (बाकी) No Permanent client error
Network error Yes DNS, conn reset, TLS
Timeout (POST) Special case पहले status check, फिर retry

POST timeouts के लिए: FoxReload में idempotency keys नहीं हैं। Timeout पर:

  1. GET /api/orders/{order_id} से status check करें (अगर order_id मिला था)
  2. Order completed/processing/paid है तो retry मत करो
  3. Order नहीं मिला तो नया order बनाएं

3. axios-retry (Node.js)

import axios from 'axios';
import axiosRetry from 'axios-retry';

const client = axios.create({
  baseURL: 'https://public-api.foxreload.com',
  timeout: 30000,
  headers: { 'X-API-Key': process.env.FOXRELOAD_KEY! },
});

axiosRetry(client, {
  retries: 5,
  retryDelay: (count) => axiosRetry.exponentialDelay(count) + Math.random() * 1000,
  retryCondition: (err) => {
    if (!err.response) return true; // network
    const s = err.response.status;
    return s >= 500 || s === 429 || s === 408;
  },
  shouldResetTimeout: true,
});

shouldResetTimeout: true essential है — इसके बिना एक 30s timeout पूरा retry budget खा जाता है।

4. tenacity (Python)

from tenacity import retry, stop_after_attempt, wait_exponential_jitter, retry_if_exception_type
import httpx

FOXRELOAD_KEY = os.environ['FOXRELOAD_KEY']

@retry(
    stop=stop_after_attempt(6),
    wait=wait_exponential_jitter(initial=0.5, max=30, jitter=2),
    retry=retry_if_exception_type((httpx.TransportError, httpx.HTTPStatusError)),
    reraise=True,
)
def get_order(order_id: str):
    r = httpx.get(
        f'https://public-api.foxreload.com/api/orders/{order_id}',
        headers={'X-API-Key': FOXRELOAD_KEY},
        timeout=30
    )
    if r.status_code >= 500 or r.status_code == 429:
        r.raise_for_status()
    return r.json()

5. Polly (C#/.NET)

var policy = Policy
    .HandleResult<HttpResponseMessage>(r => (int)r.StatusCode >= 500 || (int)r.StatusCode == 429)
    .Or<HttpRequestException>()
    .WaitAndRetryAsync(5, attempt =>
        TimeSpan.FromMilliseconds(500 * Math.Pow(2, attempt)
            + Random.Shared.Next(0, 500)));

var circuit = Policy
    .HandleResult<HttpResponseMessage>(r => (int)r.StatusCode >= 500)
    .CircuitBreakerAsync(5, TimeSpan.FromSeconds(60));

var combined = Policy.WrapAsync(circuit, policy);
// GET /api/orders/{id} पर polling के लिए
var resp = await combined.ExecuteAsync(() =>
    client.GetAsync($"https://public-api.foxreload.com/api/orders/{orderId}"));

6. Circuit breaker — जब retries काम नहीं करते

अगर upstream 30 seconds तक 50%+ fail करे, breaker open करें और तुरंत fail करें:

import CircuitBreaker from 'opossum';
const breaker = new CircuitBreaker(callFoxreload, {
  errorThresholdPercentage: 50,
  resetTimeout: 60_000,
  rollingCountTimeout: 30_000,
});
breaker.fallback(() => ({ fromCache: true }));

यह outage के दौरान downstream को retry avalanche से बचाता है।

CTA

FoxReload API 429 पर backoff के साथ retry करें। Full retry recommendations access request करने के बाद onboarding doc में हैं।

अक्सर पूछे जाने वाले प्रश्न

B2B API के लिए optimal retry attempts कितने हैं?
5–8 attempts, total budget 5 minutes से कम। ज़्यादा होंगे तो client को timeout दिखेगा; कम होंगे तो transient errors पर orders खोएँगे।
क्या कभी retry नहीं करना चाहिए?
408 (timeout) और 429 (rate limit) के अलावा कोई भी 4xx। 400 bad request, 401 unauthorised, 403 forbidden, 404 not found, 422 validation error permanent errors हैं। उन्हें retry करना infinite loop है।
Backoff में jitter क्यों add करें?
Jitter के बिना सारे clients sync में retry करते हैं — recovery पर thundering herd। ±30% jitter retry waves को spread करता है। Recovery पर peak load 4–6× कम करता है।
POST /api/orders retry पर duplicate order से कैसे बचें?
FoxReload API में Idempotency-Key header नहीं है। Retry से पहले GET /api/orders/{order_id} से status check करें। Completed या processing हो तो retry मत करो। अपने DB में client_ref से order track करें।
FoxReload API एक्सेस पाएं

संबंधित लेख