|
| 1 | +# Timeout Pattern |
| 2 | + |
| 3 | +## Intent |
| 4 | +Prevent operations from blocking indefinitely by setting a maximum time limit for their completion. If the operation doesn't complete within the specified timeout period, it is cancelled and control is returned to the caller with an error. |
| 5 | + |
| 6 | +## Explanation |
| 7 | + |
| 8 | +Real-world example |
| 9 | + |
| 10 | +> Consider a restaurant where customers place orders. The kitchen has a policy that if a dish cannot be prepared within 30 minutes, the order is cancelled and the customer is notified. This prevents customers from waiting indefinitely and allows them to make alternative arrangements. |
| 11 | +
|
| 12 | +In plain words |
| 13 | + |
| 14 | +> The Timeout pattern ensures that operations complete within a specified time limit, preventing indefinite blocking and improving system responsiveness. |
| 15 | +
|
| 16 | +**Programmatic Example** |
| 17 | + |
| 18 | +The Timeout pattern is implemented using the `TimeoutHandler` class which wraps operations and enforces time constraints: |
| 19 | + |
| 20 | +```java |
| 21 | +TimeoutHandler handler = new TimeoutHandler(); |
| 22 | + |
| 23 | +// Execute a task with 3 second timeout |
| 24 | +try { |
| 25 | + String result = handler.execute(() -> { |
| 26 | + // Simulate some processing |
| 27 | + Thread.sleep(1000); |
| 28 | + return "Operation completed"; |
| 29 | + }, 3, TimeUnit.SECONDS); |
| 30 | + |
| 31 | + System.out.println(result); |
| 32 | +} catch (TimeoutException e) { |
| 33 | + System.err.println("Operation timed out: " + e.getMessage()); |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +The `TimeoutHandler` uses an `ExecutorService` to run tasks asynchronously and monitors their completion: |
| 38 | + |
| 39 | +```java |
| 40 | +public <T> T execute(Callable<T> task, long timeout, TimeUnit unit) |
| 41 | + throws TimeoutException { |
| 42 | + Future<T> future = executor.submit(task); |
| 43 | + |
| 44 | + try { |
| 45 | + return future.get(timeout, unit); |
| 46 | + } catch (java.util.concurrent.TimeoutException e) { |
| 47 | + future.cancel(true); |
| 48 | + throw new TimeoutException("Operation timed out", e); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## Class diagram |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## Applicability |
| 58 | + |
| 59 | +Use the Timeout pattern when: |
| 60 | + |
| 61 | +- Operations might block indefinitely due to network issues, slow services, or resource contention |
| 62 | +- You need to maintain system responsiveness even when dependencies are slow |
| 63 | +- Cascading failures need to be prevented in distributed systems |
| 64 | +- SLA requirements mandate maximum response times |
| 65 | +- You want to implement circuit breaker or retry patterns |
| 66 | + |
| 67 | +## Known Uses |
| 68 | + |
| 69 | +- HTTP client libraries (setting connection and read timeouts) |
| 70 | +- Database connection pools (query timeouts) |
| 71 | +- Microservices communication (service-to-service call timeouts) |
| 72 | +- Message queue consumers (message processing timeouts) |
| 73 | +- RPC frameworks (remote procedure call timeouts) |
| 74 | + |
| 75 | +## Consequences |
| 76 | + |
| 77 | +Benefits: |
| 78 | +- Prevents indefinite blocking and resource exhaustion |
| 79 | +- Improves system responsiveness and user experience |
| 80 | +- Enables better error handling and recovery strategies |
| 81 | +- Helps maintain SLA commitments |
| 82 | +- Prevents cascading failures in distributed systems |
| 83 | + |
| 84 | +Trade-offs: |
| 85 | +- Requires careful tuning of timeout values |
| 86 | +- May need retry logic for transient failures |
| 87 | +- Can increase system complexity |
| 88 | +- Might cancel operations that would have eventually succeeded |
| 89 | + |
| 90 | +## Related Patterns |
| 91 | + |
| 92 | +- Circuit Breaker: Often used together; circuit breaker opens after multiple timeout failures |
| 93 | +- Retry: Used to handle timeout exceptions by retrying the operation |
| 94 | +- Bulkhead: Isolates resources to prevent timeout cascades |
| 95 | +- Async Method Invocation: Provides non-blocking execution with timeout support |
| 96 | + |
| 97 | +## Credits |
| 98 | + |
| 99 | +- [Microsoft Azure - Timeout Pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/timeout) |
| 100 | +- [Release It!: Design and Deploy Production-Ready Software](https://www.amazon.com/Release-Design-Deploy-Production-Ready-Software/dp/1680502395) |
| 101 | +- [Building Microservices: Designing Fine-Grained Systems](https://www.amazon.com/Building-Microservices-Designing-Fine-Grained-Systems/dp/1491950358) |
0 commit comments