Home Projects Portfolio Dashboard Export PDF Log in
TypeScript

Improving Reliability: Implementing Transparent Fallbacks in Worldmonitor

In distributed environments, relying on a local sidecar service is efficient, but it creates a fragile single point of failure. If that sidecar returns a 4xx or 5xx error, your application effectively stalls. We recently addressed this in the worldmonitor project by implementing a more resilient fallback strategy.

The Problem with Partial Failures

Previously, our runtime fetch implementation treated local sidecar responses as final. If the sidecar returned a non-OK status, the application simply passed that error back to the user. This is problematic because sidecars can experience intermittent health issues even when the main cloud infrastructure remains perfectly operational.

Moving to a Resilient Architecture

Instead of treating a local error as a terminal event, we now treat it as a trigger to pivot. By checking the HTTP status code, we can decide when it is appropriate to bypass the local proxy and route traffic directly to the cloud API.

async function fetchWithFallback(url: string): Promise<Response> {
  try {
    const localResponse = await fetch(`http://localhost:8080/${url}`);
    
    if (localResponse.ok) {
      return localResponse;
    }
    
    // Log error and proceed to fallback
    console.warn("Local sidecar failed, falling back to cloud");
  } catch (error) {
    // Network errors already triggered a fallback
  }

  return fetch(`https://api.worldmonitor.app/${url}`);
}

Why This Matters

This pattern aligns our handling of "expected" API errors (4xx/5xx) with our existing network-error recovery logic. By treating the local sidecar as an optional optimization rather than a mandatory gateway, we significantly reduce the surface area for service outages.

This is similar to how a web browser handles CDN failures: if the primary resource fails to load from the edge, it gracefully attempts to fetch the source from the origin server. It adds a slight latency penalty on failure, but it preserves system availability.

Actionable Takeaway

Audit your service-to-service communication. If you are using sidecars, ask yourself: "Does a failure in this proxy have to stop the entire request?" If the answer is no, implement a fallback mechanism that promotes your secondary provider to primary when the local node reports a failure.


Generated with Gitvlg.com

Improving Reliability: Implementing Transparent Fallbacks in Worldmonitor
Keber Flores

Keber Flores

Author

Share: