Overview
Thehedge middleware sends parallel backup requests after a delay, returning the first successful response. This reduces tail latency by hedging against slow responses.
Use it when you need:
- Reduce p99 latency
- Handle slow backend responses
- Improve user experience
Installation
Quick Start
Configuration
Options
Examples
Basic Hedging
Multiple Hedges
Skip Non-Idempotent
How It Works
- Request arrives
- Start processing
- After delay, if no response, send hedge request
- Return first successful response
- Cancel other requests
API Reference
Functions
When to Use
- High-latency backends
- P99 latency optimization
- Read-heavy workloads
When NOT to Use
- Non-idempotent operations (POST, PUT, DELETE)
- Resource-intensive operations
- When backend canβt handle extra load
Technical Details
Implementation
The hedge middleware uses a sophisticated concurrent request pattern:- Request Buffering: The middleware reads and buffers the request body to enable multiple identical requests
- Response Recording: Each request (original and hedges) writes to a
responseRecorderthat captures headers, status code, and body - Atomic Winner Selection: Uses atomic operations (
CompareAndSwapInt32) to ensure only the first completing request wins - Context Management: Each request receives hedge metadata through context values (
HedgeInfo) - Graceful Cancellation: When a winner is selected, remaining requests are cancelled via context
Key Components
- Hedger: Main struct managing options and statistics
- Options: Configuration including delay, max hedges, timeout, and callbacks
- HedgeInfo: Context data containing hedge number, total hedges, winner, and duration
- Stats: Tracks total requests, hedged requests, hedges triggered, and win rates
- responseRecorder: Custom
http.ResponseWriterthat buffers responses
Statistics Tracking
The middleware tracks comprehensive statistics:- Total requests processed
- Requests eligible for hedging
- Number of hedges actually triggered
- Wins by original vs hedged requests
Callbacks
Two callback hooks are available:OnHedge: Called when a hedge request is triggeredOnComplete: Called when the winning response is selected
Default Values
Best Practices
- Only use for idempotent operations
- Set delay based on p50 latency
- Monitor hedge rate
- Ensure backend can handle increased load
Testing
The hedge middleware includes comprehensive test coverage:Related Middlewares
- timeout - Request timeout
- retry - Automatic retries
- circuitbreaker - Circuit breaker