kapa.ai Here's our current retry implementation: Proxy Retry Logic (MixpanelProxyClient) // Constants private val RETRYABLE_STATUS_CODES = setOf(429, 502, 503, 504) private const val RATE_LIMIT_BODY_MARKER = "Too many requests" private const val MAX_RETRIES = 6 private const val INITIAL_DELAY_MS = 2_000L // Start at 2s private const val MAX_DELAY_MS = 60_000L // Cap at 60s private const val JITTER_RANGE_MS = 5_000L // 1-5s random jitter // Retry loop inside forward() for (attempt in 0..MAX_RETRIES) { if (attempt > 0) { // Exponential backoff: 2s โ 4s โ 8s โ 16s โ 32s โ 60s (capped) + 1-5s jitter val exponentialDelay = (INITIAL_DELAY_MS * (1 shl (attempt - 1))).coerceAtMost(MAX_DELAY_MS) val jitter = Random.nextLong(1_000L, JITTER_RANGE_MS + 1) val delayMs = exponentialDelay + jitter delay(delayMs) } val response = try { // Forward to api.mixpanel.com httpClient.post(targetUrl) { ... } } catch (e: Exception) { // Network failure โ synthetic 502, will be retried MixpanelProxyResponse(statusCode = HttpStatusCode.BadGateway, body = """{"error": "proxy_error"}""") } // Normalize: 504 with "Too many requests" body โ 429 // (Mixpanel returns 429 but intermediary LB/proxy surfaces it as 504) val normalizedStatus = if (status != 429 && body.contains("Too many requests")) 429 else status // Never retry 400 โ validation errors always fail and waste rate-limit quota if (response.statusCodeValue == 400) return response // Success or non-retryable error โ return immediately if (response.statusCodeValue !in RETRYABLE_STATUS_CODES) return response // Otherwise: retryable (429/502/503/504) โ loop continues with backoff } // All 6 retries exhausted โ return last response (biz layer logs + Slack alerts) return lastResponse Backoff Sequence (per attempt) โโโโโโโโโโโฌโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโ โ Attempt โ Base Delay โ + Jitter (1-5s) โ Total Range โ โโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโค โ 1 โ 2s โ 1-5s โ 3-7s โ โโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโค โ 2 โ 4s โ 1-5s โ 5-9s โ โโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโค โ 3 โ 8s โ 1-5s โ 9-13s โ โโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโค โ 4 โ 16s โ 1-5s โ 17-21s โ โโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโค โ 5 โ 32s โ 1-5s โ 33-37s โ โโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโค โ 6 โ 60s (capped) โ 1-5s โ 61-65s โ โโโโโโโโโโโดโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโ Total retry window: ~2 minutes worst case before giving up. What We Handle - 429 / 502 / 503 / 504 โ retry with exponential backoff - 504 with "Too many requests" body โ normalize to 429, then retry (catches LB-masked rate limits) - 400 (validation error) โ never retry, return immediately (per Mixpanel docs) - Network exceptions โ treated as 502, retried with same backoff - Jitter โ random 1-5s per attempt to desynchronize parallel requests
Current Retry Implementation Proxy Layer (MixpanelProxyClient) - Retryable status codes: 429, 502, 503, 504 - Body-based detection: Also retries when response body contains "Too many requests" (catches 504s that are actually rate-limits from intermediary LBs) - Status normalization: If body says "Too many requests" but status is not 429, we normalize it to 429 before returning to the client - 3 retries with fixed delays: 30s โ 1m โ 2m (+ up to 25% random jitter) - Total retry window: ~3.5 minutes before giving up - 400 errors are never retried โ logged as validation errors and returned immediately (per Mixpanel's recommendation) - Network exceptions (connection failures) are also retried with the same backoff kapa.ai Error Classification (Biz Layer) After all retries are exhausted, we classify and alert: - RATE_LIMITED โ 429/503/504 or body-detected rate-limits - NETWORK_ERROR โ genuine 502s (not rate-limit related) - VALIDATION_ERROR โ 400s from Mixpanel (not retried) - MIXPANEL_API_ERROR โ any other non-2xx What We Don't Do Yet - No gzip compression on forwarded requests - No batching/coalescing โ each SDK request is forwarded 1:1 - No concurrency limiting โ all incoming requests hit Mixpanel in parallel - No hot shard protection โ no per-distinct_id daily event volume tracking
here I am getting this suddenly, there are a lot of errors for this [PROD] Mixpanel Proxy Error: MIXPANEL_API_ERROR ๐จ <@S0ACHGFHGS0> cc: @Mani - Attention needed! Error Type: MIXPANEL_API_ERROR Path: track Message:
Mixpanel returned 504statusCode: 504 responseBody: `Too many requests ` [5:56 PM] ๐ด [PROD] Mixpanel Proxy Error: MIXPANEL_API_ERROR ๐จ <@S0ACHGFHGS0> cc: @Mani - Attention needed! Error Type: MIXPANEL_API_ERROR Path: track Message:
Mixpanel returned 504statusCode: 504 responseBody: `Too many requests ` [5:57 PM] ๐ด [PROD] Mixpanel Proxy Error: MIXPANEL_API_ERROR ๐จ <@S0ACHGFHGS0> cc: @Mani - Attention needed! Error Type: MIXPANEL_API_ERROR Path: track Message:
Mixpanel returned 504statusCode: 504 responseBody: `Too many requests ` [5:57 PM] ๐ด [PROD] Mixpanel Proxy Error: MIXPANEL_API_ERROR ๐จ <@S0ACHGFHGS0> cc: @Mani - Attention needed! Error Type: MIXPANEL_API_ERROR Path: track Message:
Mixpanel returned 504statusCode: 504 responseBody: `Too many requests ` [5:57 PM] ๐ด [PROD] Mixpanel Proxy Error: MIXPANEL_API_ERROR ๐จ <@S0ACHGFHGS0> cc: @Mani - Attention needed! Error Type: MIXPANEL_API_ERROR Path: track Message:
Mixpanel returned 504statusCode: 504 responseBody: `Too many requests ` [5:57 PM] ๐ด [PROD] Mixpanel Proxy Error: MIXPANEL_API_ERROR ๐จ <@S0ACHGFHGS0> cc: @Mani - Attention needed! Error Type: MIXPANEL_API_ERROR Path: track Message:
Mixpanel returned 504statusCode: 504 responseBody: `Too many requests ` [5:57 PM] ๐ด [PROD] Mixpanel Proxy Error: MIXPANEL_API_ERROR ๐จ <@S0ACHGFHGS0> cc: @Mani - Attention needed! Error Type: MIXPANEL_API_ERROR Path: engage Message:
Mixpanel returned 504statusCode: 504 responseBody: `Too many requests ` [5:57 PM] ๐ด [PROD] Mixpanel Proxy Error: MIXPANEL_API_ERROR ๐จ <@S0ACHGFHGS0> cc: @Mani - Attention needed! Error Type: MIXPANEL_API_ERROR Path: track Message:
Mixpanel returned 504statusCode: 504 responseBody: `Too many requests `
