Timeout Pattern
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."
Related Terms
Circuit Breaker
Circuit Breaker is like the electrical circuit breaker in your house.
Error Handling
Error handling is the art of planning for things to go wrong and dealing with them gracefully instead of letting everything catch fire.
Retry with Backoff
Retry with backoff means trying again when something fails, but waiting longer between each attempt. First retry: wait 1 second. Second: 2 seconds.