Skip to content

Timeout Pattern

Medium — good to knowBackend

ELI5 — The Vibe Check

The timeout pattern is setting a deadline for operations. If a database query or API call takes too long, cancel it and return an error. Without timeouts, one slow dependency can make your entire system hang forever. It's like saying 'if this takes more than 5 seconds, give up.'

Real Talk

The timeout pattern sets maximum duration limits on operations to prevent indefinite blocking. It applies to HTTP requests, database queries, message processing, and any I/O operation. Timeouts should cascade properly through the system: gateway timeout > service timeout > database timeout. Combined with retries and circuit breakers for comprehensive resilience.

Show Me The Code

// Fetch with timeout
const controller = new AbortController();
setTimeout(() => controller.abort(), 5000);

try {
  const res = await fetch('https://api.slow.com/data', {
    signal: controller.signal
  });
} catch (err) {
  if (err.name === 'AbortError') console.log('Request timed out');
}

When You'll Hear This

"Set a 5-second timeout on all external API calls." / "The cascade of timeouts should be: gateway 30s > service 10s > database 5s."

Made with passive-aggressive love by manoga.digital. Powered by Claude.