0
0
Microservicessystem_design~7 mins

Timeout pattern in Microservices - System Design Guide

Choose your learning style9 modes available
Problem Statement
When a microservice calls another service that is slow or unresponsive, the calling service can hang indefinitely waiting for a response. This causes resource exhaustion, delays in processing, and cascading failures across the system.
Solution
The timeout pattern sets a maximum time limit for a service call to complete. If the called service does not respond within this limit, the call is aborted and an error or fallback response is returned. This prevents the caller from waiting forever and allows it to handle failures gracefully.
Architecture
Calling
Service
Called
Timeout
Controller

This diagram shows a calling service making a request to a called service with a timeout controller that aborts the call if it exceeds the time limit.

Trade-offs
✓ Pros
Prevents resource exhaustion by limiting wait time on slow or unresponsive services.
Improves system resilience by enabling fast failure detection and fallback handling.
Reduces cascading failures by isolating slow dependencies.
✗ Cons
Choosing too short a timeout can cause premature failures for slow but healthy services.
Requires careful tuning per service and operation to balance latency and availability.
Adds complexity to error handling and retry logic.
Use when microservices communicate over networks with unpredictable latency and when slow responses can block critical workflows. Recommended when average response times exceed 100ms and failure isolation is needed.
Avoid when services are guaranteed to respond quickly under all conditions or when the overhead of managing timeouts outweighs benefits, such as in simple synchronous calls within the same process.
Real World Examples
Netflix
Netflix uses the timeout pattern in its microservices to avoid waiting indefinitely on downstream services, enabling fast failure detection and fallback to cached or default content.
Uber
Uber applies timeouts on service calls to prevent delays in trip processing caused by slow responses from location or pricing services.
Amazon
Amazon uses timeouts in its distributed services to maintain responsiveness during high traffic and to trigger retries or alternative workflows.
Code Example
The before code waits indefinitely for the service response, risking hanging. The after code sets a 2-second timeout on the request. If the service does not respond in time, a Timeout exception is raised and handled gracefully with a fallback.
Microservices
### Before (no timeout) ###
import requests

def call_service():
    response = requests.get('http://service/api')
    return response.text


### After (with timeout) ###
import requests
from requests.exceptions import Timeout

def call_service():
    try:
        response = requests.get('http://service/api', timeout=2)  # 2 seconds timeout
        return response.text
    except Timeout:
        return 'Service timed out, fallback response'
OutputSuccess
Alternatives
Circuit Breaker
Circuit breaker stops calls to a failing service after repeated failures, while timeout only limits wait time per call.
Use when: Use circuit breaker when you want to prevent calls to services that are likely down, combined with timeout for each call.
Retry Pattern
Retry pattern attempts the call multiple times on failure, while timeout aborts the call after a set time.
Use when: Use retry with timeout to handle transient failures but avoid waiting indefinitely.
Summary
Timeout pattern prevents indefinite waiting on slow or unresponsive services by limiting call duration.
It improves system resilience by enabling fast failure detection and fallback handling.
Timeouts must be carefully tuned to balance latency and availability in distributed systems.