Course contentsShow
← Node.js
Lesson 1472 of 1,98013. Error Handling and Resilient SystemsFree lesson

Monitoring External Dependency Latency

You will track the latency of third-party APIs to detect when degradation might trigger your circuit breakers.

In this lesson, you will learn how to instrument your Node.js applications to track the response times of third-party APIs, providing the telemetry necessary to inform your circuit breaker thresholds.

Why Monitor External Latency?

When you integrate external services, your application becomes as reliable as its weakest dependency. If an external API starts responding slowly, your own event loop may become congested as pending requests accumulate. By tracking the latency of these calls, you can move beyond simple "up/down" checks and detect "gray failures"—where a service is technically functional but too slow to support your performance requirements.

Implementing Latency Tracking

The most effective way to monitor this is to wrap your fetch or request logic with a timer. You can use the native performance.now() API to capture high-resolution timestamps before and after the network call.

const { performance } = require('perf_hooks');

async function fetchWithMonitoring(url) {
  const start = performance.now();
  
  try {
    const response = await fetch(url);
    const duration = performance.now() - start;
    
    // Send this metric to your observability platform
    metrics.record('external_api_latency', duration, { url });
    
    return response;
  } catch (error) {
    // Log failures as well, as they often correlate with high latency
    metrics.record('external_api_error', 1, { url });
    throw error;
  }
}

From Metrics to Circuit Breakers

Once you have this data, you can dynamically adjust your resilience strategy. If the average latency for a specific dependency exceeds a predefined threshold (e.g., 500ms over a rolling 10-second window), your system should treat that dependency as "degraded."

At this point, you can programmatically trip your circuit breaker. This prevents your Node.js process from waiting on slow requests, allowing it to fail fast or return a cached response. This prevents "cascading latency," where your entire application slows down because it is waiting for an unresponsive third party.

By treating latency as a primary health metric rather than just an afterthought, you ensure that your circuit breakers are tuned to the reality of your network conditions rather than arbitrary guesses.

Key Takeaway: Measuring the precise latency of external dependencies allows you to trip circuit breakers before slow responses exhaust your system resources or cause event loop contention.