Faraday Hedge
Hedged requests middleware for Faraday to reduce tail latency on idempotent methods.
About
Faraday Hedge issues a backup request after a small delay and returns the first response. This reduces tail latency when occasional slow requests occur, while keeping overall load bounded.
The middleware defaults to idempotent methods and can be configured to hedge only specific HTTP verbs.
Use Cases
- Reduce p99 latency for flaky upstream APIs
- Protect against tail latency spikes on critical read paths
- Improve UX for latency-sensitive services
Compatibility
- Ruby 3.0+
- Faraday 1.0+
Elysium Arc Reliability Toolkit
Also check out these related gems:
- Cache Coalescer: https://github.com/Elysium-Arc/cache-coalescer
- Cache SWR: https://github.com/Elysium-Arc/cache-swr
- Rack Idempotency Kit: https://github.com/Elysium-Arc/rack-idempotency-kit
- Env Contract: https://github.com/Elysium-Arc/env-contract
Installation
# Gemfile
gem "faraday-hedge"Usage
conn = Faraday.new do |f|
f.request :hedge, delay: 0.05
f.adapter :net_http
endOptions
-
delay(Float) seconds before firing a backup request -
max_hedges(Integer) number of backup requests to allow -
methods(Array) methods eligible for hedging -
idempotent_only(Boolean) restrict hedging to idempotent methods
Notes
- Hedging uses background threads.
- Use conservative delays to avoid excessive duplicate work.
Release
bundle exec rake release