Managing Timeout and Deadline Patterns

1. Request Timeout Pattern

AspectDetail
DefinitionCap on time to receive response
DefaultNEVER infinite
Set WhereHTTP client config; per-call override
Tuningp99 latency × 2-3, capped by user-tolerable wait

2. Connection Timeout Pattern

AspectDetail
DefinitionCap on TCP/TLS handshake
Typical1-3 seconds
Symptom If MissingConnections hang on bad DNS/firewall

3. Deadline Propagation Pattern

AspectDetail
DefinitionPass remaining time budget to downstream
HeadergRPC grpc-timeout, custom X-Deadline-Ms
EffectDownstream rejects if can't complete in time
BenefitAvoids work for already-timed-out caller

Example: gRPC Deadline (Go)

ctx, cancel := context.WithTimeout(parentCtx, 500*time.Millisecond)
defer cancel()
resp, err := client.GetOrder(ctx, &pb.OrderRequest{Id: id})
// Context.deadline propagates to downstream calls automatically

4. Cascading Timeout Pattern

RuleDetail
Inner ≤ OuterEach downstream timeout shorter than parent
BufferLeave 50-100ms for serialization, network
AvoidInner = Outer (parent times out before retry possible)

5. Adaptive Timeout Pattern

AspectDetail
MechanismTimeout based on rolling p99 of latency
BenefitAuto-adjusts to system state
RiskSlow drift hides real degradation

6. Long-Running Operation Pattern

ApproachDetail
202 Accepted + PollingReturn job ID; client polls /jobs/{id}
WebhookClient provides callback URL
WebSocket / SSEPush progress updates
Async ResultResult in queue / event

Example: Long-Running Operation Response

HTTP/1.1 202 Accepted
Location: /jobs/abc123
Retry-After: 5

{ "jobId": "abc123", "status": "PROCESSING", "checkUrl": "/jobs/abc123" }

7. Timeout Recovery Pattern

StepAction
CancelPropagate cancellation to free downstream resources
CleanupRelease connections, threads, locks
CompensateIf side-effects committed, run reversal
LogTimeout with context for debugging

8. Timeout Budget Pattern

ConceptDetail
BudgetTotal time available at edge (e.g., 2s)
SpendEach hop subtracts elapsed
RemainingPassed in deadline header
Reject EarlyIf remaining < estimated work, fail fast

9. Asynchronous Timeout Pattern

AspectDetail
UseNon-blocking timeout in reactive/async code
JavaCompletableFuture.orTimeout() (Java 9+)
ReactorMono.timeout(Duration)
CancellationTriggers downstream cleanup

10. Deadline Context Pattern

AspectDetail
PatternCarry deadline in request context (gRPC ctx, OTel baggage)
Auto-PropagationLibrary injects/extracts on every call
BenefitWhole call tree shares one deadline