Skip to main content

Why API Timeouts Matter More Than Speed

Updated 5 min readMarkdown

Imagine you're the engineer on call for a payments platform. Every transaction your service authorizes has to pass through a fraud-check API before it clears. Most days that call returns in 150ms and nobody thinks about it.

One evening the fraud-check provider has a partial outage. Not a hard failure, just degraded. Responses start taking 6 to 10 seconds instead of milliseconds. Your payment service has no timeout set on that call, so every authorization request just waits.

Within minutes, every thread in your connection pool is stuck waiting on the fraud service. New payment requests start queuing behind them. Customers see spinners, then errors, then duplicate charges when their banking app retries on its own. Support tickets start piling in before anyone on your team even knows the fraud service is having a bad day.

Nobody asked whether the fraud-check API was fast enough. The real question was what your payment service does when it isn't. That question gets skipped far more often than it should.

The Root Cause

Teams obsess over P50 and P99 latency because it's easy to graph and easy to report on. But latency numbers only describe the happy path. They say nothing about what happens when a downstream dependency stalls instead of failing cleanly.

A slow response is worse than a failed one. A failure is a signal your code can react to right away. A slow response looks like progress is still being made. It keeps threads busy, keeps connections open, keeps memory reserved, while your service slowly runs out of room to serve anyone else.

Load testing rarely catches this. Load tests simulate volume, not a dependency that degrades from 150ms to 8 seconds under its own internal pressure. That kind of failure is exactly what drags healthy services down with it.

Why Speed Alone Doesn't Save You

A fast API with no timeout policy is still a risk. Speed describes the common case. Timeout policy describes the worst case, and production systems get judged by their worst case, not their average one.

The fix isn't a faster fraud-check service. It's making sure every outbound call your service makes has a bounded worst-case duration, and a defined plan for what happens when that bound is hit.

The Solution

Three things work together: a timeout on every outbound call, a circuit breaker so you stop hammering a struggling dependency, and a fallback so the caller has something sensible to do instead of hanging.

Here's the wrong way first. This is what most services ship with by default.

// Wrong: no timeout, no fallback, no circuit breaker
async function authorizePayment(payment: Payment): Promise<AuthorizationResult> {
  const response = await axios.get(
    `${fraudServiceUrl}/check?paymentId=${payment.id}`
  );
  // If the fraud service hangs, this request hangs with it
  return response.data;
}

This passes every test in staging and becomes an incident the first time the fraud service has a bad night.

Here's a version with an explicit timeout, a circuit breaker, and a fallback, using opossum for the breaker.

// Right: bounded timeout, circuit breaker, explicit fallback
import CircuitBreaker from 'opossum';
import axios from 'axios';

const breakerOptions = {
  timeout: 800, // ms, hard ceiling on the call
  errorThresholdPercentage: 50,
  resetTimeout: 10000, // ms before trying the dependency again
};

async function callFraudService(payment: Payment): Promise<AuthorizationResult> {
  const response = await axios.get(
    `${fraudServiceUrl}/check?paymentId=${payment.id}`
  );
  return response.data;
}

const fraudBreaker = new CircuitBreaker(callFraudService, breakerOptions);

fraudBreaker.fallback((payment: Payment) => holdForManualReview(payment));

async function authorizePayment(payment: Payment): Promise<AuthorizationResult> {
  return fraudBreaker.fire(payment);
}

function holdForManualReview(payment: Payment): AuthorizationResult {
  // Degraded but functional: queue for manual review instead of blind approval
  reviewQueue.push(payment);
  return { status: 'PENDING_REVIEW', source: 'FALLBACK' };
}

The timeout gives the call a hard ceiling. The circuit breaker stops sending traffic to a fraud service that's already struggling, so it can recover instead of getting buried under retries. The fallback keeps payments moving, just routed to manual review instead of authorized blindly or stuck forever.

Where This Still Breaks

Timeouts alone aren't a complete answer. A few ways this pattern fails if you're not careful.

Retries stacking on top of timeouts. If your client times out at 800ms and retries three times with no backoff, one slow request just became four, adding more load to a dependency that was already struggling.

Timeout values copied across services without thought. An 800ms timeout is reasonable for a fraud check. It's wrong for a nightly settlement batch job that legitimately needs 30 seconds. Timeout budgets should match the actual cost of the operation, not a number someone picked once and everyone else copied.

Fallbacks that fail silently. Routing payments to manual review needs to show up in your metrics as a fallback, not get counted as a normal authorization. Skip that tagging and you'll find out about the degradation from an angry finance team, not a dashboard.

No timeout on the fallback path itself. It sounds obvious, but pushing a payment onto a review queue still involves I/O, and that I/O needs its own bound too.

Final Thought

Speed tells you how your system behaves when everything is healthy. Timeouts tell you how it behaves when something isn't. Only one of those two actually decides whether you get paged at 2 AM.

Have you shipped a service without a timeout and paid for it later? Drop the story in the comments, I'd like to hear how you found out.

#backend #distributedsystems #typescript #softwareengineering

About the author

  • Software Development Engineer ~ System Design, AI, Finance, Tech

No comments yet. Be the first to comment.

More from this blog