ruby
29 lines · 7 steps
Exponential backoff retries in Ruby
A reusable module that retries a block on transient network errors with exponentially growing, jittered delays.
Explained by
highlit
1module Retryable
2 class RetriesExhausted < StandardError; end
3
4 RETRYABLE_ERRORS = [
5 Net::OpenTimeout,
6 Net::ReadTimeout,
7 Errno::ECONNRESET,
8 Errno::ECONNREFUSED,
9 SocketError
10 ].freeze
11
12 def self.with_backoff(max_attempts: 5, base_delay: 0.5, max_delay: 30.0, jitter: true)
13 attempt = 0
14
15 begin
16 attempt += 1
17 yield(attempt)
18 rescue *RETRYABLE_ERRORS => error
19 raise RetriesExhausted, "gave up after #{attempt} attempts: #{error.message}" if attempt >= max_attempts
20
21 delay = [base_delay * (2**(attempt - 1)), max_delay].min
22 delay += rand * delay if jitter
23
24 warn "attempt #{attempt} failed (#{error.class}: #{error.message}); retrying in #{delay.round(2)}s"
25 sleep(delay)
26 retry
27 end
28 end
29end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Retrying only a curated list of transient errors avoids masking real bugs that should fail fast.
- 2Exponential backoff capped by a maximum delay prevents both thundering retries and unbounded waits.
- 3Adding jitter spreads out retries from many clients so they don't all reconnect in sync.
Related explainers
ruby
class ToastBroadcaster include ActionView::RecordIdentifier def self.broadcast_to(user, message:, type: :notice)
How Turbo Stream toasts broadcast in Rails
turbo-streams
service-object
real-time
Intermediate
6 steps
ruby
class TemplateInterpolator PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/ def initialize(strict: false)
Interpolating templates with dotted keys in Ruby
regex
string-interpolation
hash-traversal
Intermediate
6 steps
ruby
class KeyTransformer def self.camelize(data) new.camelize(data) end
Recursively camelizing nested Ruby data
recursion
data-transformation
pattern-matching
Intermediate
7 steps
ruby
module Paginatable extend ActiveSupport::Concern private
A reusable pagination concern in Rails
pagination
concerns
http-headers
Intermediate
8 steps
go
package config import ( "fmt"
A thread-safe config singleton in Go
singleton
concurrency
environment-variables
Intermediate
7 steps
rust
use std::net::Ipv4Addr; use std::str::FromStr; #[derive(Debug, Clone, Copy)]
Parsing and matching IPv4 CIDR ranges in Rust
bitwise-operations
parsing
error-handling
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/exponential-backoff-retries-in-ruby-explained-ruby-1d7b/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.