Retry: Try Again, a Little Later
When a far-away weather service misses one call in a hundred, showing "unavailable" for a hiccup that a second try would fix lets the user down; Retry tries again a few times, waiting longer each time with a little randomness, and only for calls that are safe to repeat.
Journeyman · Software Development · OOP · Updated
After this lesson you can
- Retry a failed call a few times, waiting longer each time, with a little randomness
- Decide whether a call is safe to retry: retry only what's safe to repeat
- Spot the smells that point to Retry: one hiccup from a far-away service shown to the user as a failure
Key takeaways
- The smell: one missed call to a far-away service shown to the user as a failure, when a second try a moment later would have worked.
- The fix: try again a few times, waiting longer between tries (backoff), with a small random extra (jitter), then give up honestly.
- Backoff gives a struggling service room to recover; jitter stops a whole neighborhood of hubs from retrying at the same instant.
- Retry only what's safe to repeat (idempotent): "turn the light on" is; "brightness up 10%" isn't.
- The classic version wraps the service in a class that keeps its promise; the modern one is a function that retries any call.
Before this: Meet the Smart-Home Hub, Decorator: Same Phone, More Features (a wrapper that keeps the same promise), passing a function as a value, and catching an error with try/catch (try/except in Python).
Every morning at 7:00, the kitchen display shows the weather from SkyCast, the weather service far away on the internet. A few times a week, one call gets no answer: a network hiccup somewhere between Maya's house and SkyCast. The display shows "Weather: unavailable", and Maya checks her phone instead, where the weather loads fine. A second try a moment later would almost always have worked. This lesson's fix has the plainest name in the course: Retry.
The symptom: one hiccup, and the morning has no weather
The briefing asks SkyCast once. In these examples, a stand-in for SkyCast misses its first calls on purpose, so you can see what happens on a bad morning without waiting for one:
Console.WriteLine(Briefing(new FakeSkyCast(misses: 2)));
string Briefing(ISkyCast skyCast)
{
try
{
return "Weather: " + skyCast.Weather();
}
catch (SkyCastDownException)
{
return "Weather: unavailable";
}
}
interface ISkyCast
{
string Weather();
}
class SkyCastDownException : Exception { }
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast : ISkyCast
{
private readonly int misses;
private int calls;
public FakeSkyCast(int misses) { this.misses = misses; }
public string Weather()
{
calls++;
if (calls <= misses)
{
Console.WriteLine( quot;SkyCast call {calls}: no answer");
throw new SkyCastDownException();
}
Console.WriteLine( quot;SkyCast call {calls}: 72F, sunny");
return "72F, sunny";
}
}
SkyCast call 1: no answer
Weather: unavailable
interface SkyCast {
String weather();
}
class SkyCastDown extends RuntimeException { }
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast implements SkyCast {
private final int misses;
private int calls = 0;
FakeSkyCast(int misses) { this.misses = misses; }
public String weather() {
calls++;
if (calls <= misses) {
IO.println("SkyCast call " + calls + ": no answer");
throw new SkyCastDown();
}
IO.println("SkyCast call " + calls + ": 72F, sunny");
return "72F, sunny";
}
}
String briefing(SkyCast skyCast) {
try {
return "Weather: " + skyCast.weather();
} catch (SkyCastDown down) {
return "Weather: unavailable";
}
}
void main() {
IO.println(briefing(new FakeSkyCast(2)));
}
SkyCast call 1: no answer
Weather: unavailable
class SkyCastDown(Exception):
pass
class FakeSkyCast:
"""Stands in for SkyCast in this example: it misses its first few calls, then answers."""
def __init__(self, misses):
self.misses = misses
self.calls = 0
def weather(self):
self.calls += 1
if self.calls <= self.misses:
print(f"SkyCast call {self.calls}: no answer")
raise SkyCastDown()
print(f"SkyCast call {self.calls}: 72F, sunny")
return "72F, sunny"
def briefing(skycast):
try:
return "Weather: " + skycast.weather()
except SkyCastDown:
return "Weather: unavailable"
print(briefing(FakeSkyCast(misses=2)))
SkyCast call 1: no answer
Weather: unavailable
class SkyCastDown extends Error {}
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast {
#misses;
#calls = 0;
constructor(misses) { this.#misses = misses; }
weather() {
this.#calls++;
if (this.#calls <= this.#misses) {
console.log(`SkyCast call ${this.#calls}: no answer`);
throw new SkyCastDown();
}
console.log(`SkyCast call ${this.#calls}: 72F, sunny`);
return "72F, sunny";
}
}
function briefing(skyCast) {
try {
return "Weather: " + skyCast.weather();
} catch (error) {
if (error instanceof SkyCastDown) return "Weather: unavailable";
throw error;
}
}
console.log(briefing(new FakeSkyCast(2)));
SkyCast call 1: no answer
Weather: unavailable
interface SkyCast {
weather(): string;
}
class SkyCastDown extends Error {}
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast implements SkyCast {
#misses: number;
#calls = 0;
constructor(misses: number) { this.#misses = misses; }
weather(): string {
this.#calls++;
if (this.#calls <= this.#misses) {
console.log(`SkyCast call ${this.#calls}: no answer`);
throw new SkyCastDown();
}
console.log(`SkyCast call ${this.#calls}: 72F, sunny`);
return "72F, sunny";
}
}
function briefing(skyCast: SkyCast): string {
try {
return "Weather: " + skyCast.weather();
} catch (error) {
if (error instanceof SkyCastDown) return "Weather: unavailable";
throw error;
}
}
console.log(briefing(new FakeSkyCast(2)));
SkyCast call 1: no answer
Weather: unavailable
One missed call, and the whole morning has no weather, though SkyCast would have answered the third time.
- The symptom: a single missed call to a far-away service is shown to the user as a failure.
- The cause: the code treats every failure as final. Calls across a network fail now and then for reasons that pass in a second (a busy router, a dropped connection), and nothing gives the call a second chance.
What would you try?
Try again straight away, until it works. A while loop around the call. If SkyCast is down for an hour, the loop never ends and the display freezes. And when SkyCast comes back, every hub in town is hammering it at full speed.
Try again after a fixed wait, say one second. Better. But think of the neighborhood: if a hiccup at SkyCast fails a thousand hubs at 7:00:00, all thousand try again at 7:00:01, together, and knock it over again. Engineers call this a thundering herd.
Give up and show "unavailable". That's what we have: honest, but it gives up on a call that would have worked.
A good fix tries again, but politely: not forever, not all at once, and not right away.
The fix: try again, a little later
You already do this with a busy phone line. You call, it's busy, and you wait a minute before calling again. Still busy: this time you wait a little longer. You don't redial every second, and after a few tries you give up and send a text instead.

The fix has three parts:
- Try again a few times, then stop. Three tries in all, and then "unavailable", honestly.
- Wait longer each time (backoff). 1 second before the second try, 2 before the third: each wait doubles. A service that's struggling gets more room to recover.
- Add a small random extra (jitter). Each hub adds up to half a second at random, so a thousand hubs that failed together spread their retries out instead of arriving in one wave.

The classic version: a wrapper that tries again
The retry can be a class that keeps SkyCast's promise and wraps the real one, the way a decorator does. The briefing doesn't change at all: it's handed the wrapper instead. The wait and the random extra are handed in too, so this example can print them instead of really waiting:
ISkyCast skyCast = new RetryingSkyCast(new FakeSkyCast(misses: 2), ms => Console.WriteLine( quot;Waiting {ms} ms"), () => 250);
Console.WriteLine(Briefing(skyCast));
string Briefing(ISkyCast skyCast)
{
try
{
return "Weather: " + skyCast.Weather();
}
catch (SkyCastDownException)
{
return "Weather: unavailable";
}
}
interface ISkyCast
{
string Weather();
}
class SkyCastDownException : Exception { }
// Keeps SkyCast's promise, and tries again when a call fails.
class RetryingSkyCast : ISkyCast
{
private readonly ISkyCast skyCast;
private readonly Action<int> sleep;
private readonly Func<int> jitter;
private readonly int tries;
public RetryingSkyCast(ISkyCast skyCast, Action<int> sleep, Func<int> jitter, int tries = 3)
{
this.skyCast = skyCast;
this.sleep = sleep;
this.jitter = jitter;
this.tries = tries;
}
public string Weather()
{
int wait = 1000; // milliseconds before the second try; it doubles each time
for (int attempt = 1; ; attempt++)
{
try
{
return skyCast.Weather();
}
catch (SkyCastDownException) when (attempt < tries)
{
sleep(wait + jitter());
wait *= 2;
}
}
}
}
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast : ISkyCast
{
private readonly int misses;
private int calls;
public FakeSkyCast(int misses) { this.misses = misses; }
public string Weather()
{
calls++;
if (calls <= misses)
{
Console.WriteLine( quot;SkyCast call {calls}: no answer");
throw new SkyCastDownException();
}
Console.WriteLine( quot;SkyCast call {calls}: 72F, sunny");
return "72F, sunny";
}
}
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
interface SkyCast {
String weather();
}
class SkyCastDown extends RuntimeException { }
// Keeps SkyCast's promise, and tries again when a call fails.
class RetryingSkyCast implements SkyCast {
private final SkyCast skyCast;
private final IntConsumer sleep;
private final IntSupplier jitter;
private final int tries;
RetryingSkyCast(SkyCast skyCast, IntConsumer sleep, IntSupplier jitter, int tries) {
this.skyCast = skyCast;
this.sleep = sleep;
this.jitter = jitter;
this.tries = tries;
}
public String weather() {
int wait = 1000; // milliseconds before the second try; it doubles each time
for (int attempt = 1; ; attempt++) {
try {
return skyCast.weather();
} catch (SkyCastDown down) {
if (attempt == tries) {
throw down; // out of tries: give up, honestly
}
sleep.accept(wait + jitter.getAsInt());
wait *= 2;
}
}
}
}
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast implements SkyCast {
private final int misses;
private int calls = 0;
FakeSkyCast(int misses) { this.misses = misses; }
public String weather() {
calls++;
if (calls <= misses) {
IO.println("SkyCast call " + calls + ": no answer");
throw new SkyCastDown();
}
IO.println("SkyCast call " + calls + ": 72F, sunny");
return "72F, sunny";
}
}
String briefing(SkyCast skyCast) {
try {
return "Weather: " + skyCast.weather();
} catch (SkyCastDown down) {
return "Weather: unavailable";
}
}
void main() {
SkyCast skyCast = new RetryingSkyCast(new FakeSkyCast(2), ms -> IO.println("Waiting " + ms + " ms"), () -> 250, 3);
IO.println(briefing(skyCast));
}
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
class SkyCastDown(Exception):
pass
class RetryingSkyCast:
"""Keeps SkyCast's promise, and tries again when a call fails."""
def __init__(self, skycast, sleep, jitter, tries=3):
self.skycast = skycast
self.sleep = sleep
self.jitter = jitter
self.tries = tries
def weather(self):
wait = 1000 # milliseconds before the second try; it doubles each time
for attempt in range(1, self.tries + 1):
try:
return self.skycast.weather()
except SkyCastDown:
if attempt == self.tries:
raise # out of tries: give up, honestly
self.sleep(wait + self.jitter())
wait *= 2
class FakeSkyCast:
"""Stands in for SkyCast in this example: it misses its first few calls, then answers."""
def __init__(self, misses):
self.misses = misses
self.calls = 0
def weather(self):
self.calls += 1
if self.calls <= self.misses:
print(f"SkyCast call {self.calls}: no answer")
raise SkyCastDown()
print(f"SkyCast call {self.calls}: 72F, sunny")
return "72F, sunny"
def briefing(skycast):
try:
return "Weather: " + skycast.weather()
except SkyCastDown:
return "Weather: unavailable"
skycast = RetryingSkyCast(FakeSkyCast(misses=2), sleep=lambda ms: print(f"Waiting {ms} ms"), jitter=lambda: 250)
print(briefing(skycast))
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
class SkyCastDown extends Error {}
// Keeps SkyCast's promise, and tries again when a call fails.
class RetryingSkyCast {
#skyCast;
#sleep;
#jitter;
#tries;
constructor(skyCast, sleep, jitter, tries = 3) {
this.#skyCast = skyCast;
this.#sleep = sleep;
this.#jitter = jitter;
this.#tries = tries;
}
weather() {
let wait = 1000; // milliseconds before the second try; it doubles each time
for (let attempt = 1; ; attempt++) {
try {
return this.#skyCast.weather();
} catch (error) {
if (!(error instanceof SkyCastDown) || attempt === this.#tries) throw error; // out of tries: give up, honestly
this.#sleep(wait + this.#jitter());
wait *= 2;
}
}
}
}
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast {
#misses;
#calls = 0;
constructor(misses) { this.#misses = misses; }
weather() {
this.#calls++;
if (this.#calls <= this.#misses) {
console.log(`SkyCast call ${this.#calls}: no answer`);
throw new SkyCastDown();
}
console.log(`SkyCast call ${this.#calls}: 72F, sunny`);
return "72F, sunny";
}
}
function briefing(skyCast) {
try {
return "Weather: " + skyCast.weather();
} catch (error) {
if (error instanceof SkyCastDown) return "Weather: unavailable";
throw error;
}
}
const skyCast = new RetryingSkyCast(new FakeSkyCast(2), (ms) => console.log(`Waiting ${ms} ms`), () => 250);
console.log(briefing(skyCast));
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
interface SkyCast {
weather(): string;
}
class SkyCastDown extends Error {}
// Keeps SkyCast's promise, and tries again when a call fails.
class RetryingSkyCast implements SkyCast {
#skyCast: SkyCast;
#sleep: (ms: number) => void;
#jitter: () => number;
#tries: number;
constructor(skyCast: SkyCast, sleep: (ms: number) => void, jitter: () => number, tries = 3) {
this.#skyCast = skyCast;
this.#sleep = sleep;
this.#jitter = jitter;
this.#tries = tries;
}
weather(): string {
let wait = 1000; // milliseconds before the second try; it doubles each time
for (let attempt = 1; ; attempt++) {
try {
return this.#skyCast.weather();
} catch (error) {
if (!(error instanceof SkyCastDown) || attempt === this.#tries) throw error; // out of tries: give up, honestly
this.#sleep(wait + this.#jitter());
wait *= 2;
}
}
}
}
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast implements SkyCast {
#misses: number;
#calls = 0;
constructor(misses: number) { this.#misses = misses; }
weather(): string {
this.#calls++;
if (this.#calls <= this.#misses) {
console.log(`SkyCast call ${this.#calls}: no answer`);
throw new SkyCastDown();
}
console.log(`SkyCast call ${this.#calls}: 72F, sunny`);
return "72F, sunny";
}
}
function briefing(skyCast: SkyCast): string {
try {
return "Weather: " + skyCast.weather();
} catch (error) {
if (error instanceof SkyCastDown) return "Weather: unavailable";
throw error;
}
}
const skyCast = new RetryingSkyCast(new FakeSkyCast(2), (ms) => console.log(`Waiting ${ms} ms`), () => 250);
console.log(briefing(skyCast));
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
The waits are 1000 and then 2000 milliseconds, each plus the random extra. In the real hub, sleep really waits and jitter picks a new number from 0 to 500 each time; here both are stand-ins that print, so the example runs at once and prints the same lines every time. That's the same trick the exercise's tests use, with a fake clock: no test ever really waits. Only SkyCastDown is retried: any other error is passed straight on, because it won't fix itself in two seconds.
The modern version: a function that retries any call
A wrapper class keeps one promise, so the lights, the locks and SkyCast would each need their own. A function that's handed the call to make can retry anything:
FakeSkyCast skyCast = new FakeSkyCast(misses: 2);
Console.WriteLine(Briefing(() => WithRetry(skyCast.Weather, ms => Console.WriteLine( quot;Waiting {ms} ms"), () => 250)));
T WithRetry<T>(Func<T> action, Action<int> sleep, Func<int> jitter, int tries = 3)
{
int wait = 1000; // milliseconds before the second try; it doubles each time
for (int attempt = 1; ; attempt++)
{
try
{
return action();
}
catch (SkyCastDownException) when (attempt < tries)
{
sleep(wait + jitter());
wait *= 2;
}
}
}
string Briefing(Func<string> getWeather)
{
try
{
return "Weather: " + getWeather();
}
catch (SkyCastDownException)
{
return "Weather: unavailable";
}
}
class SkyCastDownException : Exception { }
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast
{
private readonly int misses;
private int calls;
public FakeSkyCast(int misses) { this.misses = misses; }
public string Weather()
{
calls++;
if (calls <= misses)
{
Console.WriteLine( quot;SkyCast call {calls}: no answer");
throw new SkyCastDownException();
}
Console.WriteLine( quot;SkyCast call {calls}: 72F, sunny");
return "72F, sunny";
}
}
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
class SkyCastDown extends RuntimeException { }
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast {
private final int misses;
private int calls = 0;
FakeSkyCast(int misses) { this.misses = misses; }
String weather() {
calls++;
if (calls <= misses) {
IO.println("SkyCast call " + calls + ": no answer");
throw new SkyCastDown();
}
IO.println("SkyCast call " + calls + ": 72F, sunny");
return "72F, sunny";
}
}
<T> T withRetry(Supplier<T> action, IntConsumer sleep, IntSupplier jitter, int tries) {
int wait = 1000; // milliseconds before the second try; it doubles each time
for (int attempt = 1; ; attempt++) {
try {
return action.get();
} catch (SkyCastDown down) {
if (attempt == tries) {
throw down; // out of tries: give up, honestly
}
sleep.accept(wait + jitter.getAsInt());
wait *= 2;
}
}
}
String briefing(Supplier<String> getWeather) {
try {
return "Weather: " + getWeather.get();
} catch (SkyCastDown down) {
return "Weather: unavailable";
}
}
void main() {
FakeSkyCast skyCast = new FakeSkyCast(2);
IO.println(briefing(() -> withRetry(skyCast::weather, ms -> IO.println("Waiting " + ms + " ms"), () -> 250, 3)));
}
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
class SkyCastDown(Exception):
pass
def with_retry(action, sleep, jitter, tries=3):
wait = 1000 # milliseconds before the second try; it doubles each time
for attempt in range(1, tries + 1):
try:
return action()
except SkyCastDown:
if attempt == tries:
raise # out of tries: give up, honestly
sleep(wait + jitter())
wait *= 2
class FakeSkyCast:
"""Stands in for SkyCast in this example: it misses its first few calls, then answers."""
def __init__(self, misses):
self.misses = misses
self.calls = 0
def weather(self):
self.calls += 1
if self.calls <= self.misses:
print(f"SkyCast call {self.calls}: no answer")
raise SkyCastDown()
print(f"SkyCast call {self.calls}: 72F, sunny")
return "72F, sunny"
def briefing(get_weather):
try:
return "Weather: " + get_weather()
except SkyCastDown:
return "Weather: unavailable"
skycast = FakeSkyCast(misses=2)
print(briefing(lambda: with_retry(skycast.weather, sleep=lambda ms: print(f"Waiting {ms} ms"), jitter=lambda: 250)))
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
class SkyCastDown extends Error {}
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast {
#misses;
#calls = 0;
constructor(misses) { this.#misses = misses; }
weather() {
this.#calls++;
if (this.#calls <= this.#misses) {
console.log(`SkyCast call ${this.#calls}: no answer`);
throw new SkyCastDown();
}
console.log(`SkyCast call ${this.#calls}: 72F, sunny`);
return "72F, sunny";
}
}
function withRetry(action, sleep, jitter, tries = 3) {
let wait = 1000; // milliseconds before the second try; it doubles each time
for (let attempt = 1; ; attempt++) {
try {
return action();
} catch (error) {
if (!(error instanceof SkyCastDown) || attempt === tries) throw error; // out of tries: give up, honestly
sleep(wait + jitter());
wait *= 2;
}
}
}
function briefing(getWeather) {
try {
return "Weather: " + getWeather();
} catch (error) {
if (error instanceof SkyCastDown) return "Weather: unavailable";
throw error;
}
}
const skyCast = new FakeSkyCast(2);
console.log(briefing(() => withRetry(() => skyCast.weather(), (ms) => console.log(`Waiting ${ms} ms`), () => 250)));
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
class SkyCastDown extends Error {}
// Stands in for SkyCast in this example: it misses its first few calls, then answers.
class FakeSkyCast {
#misses: number;
#calls = 0;
constructor(misses: number) { this.#misses = misses; }
weather(): string {
this.#calls++;
if (this.#calls <= this.#misses) {
console.log(`SkyCast call ${this.#calls}: no answer`);
throw new SkyCastDown();
}
console.log(`SkyCast call ${this.#calls}: 72F, sunny`);
return "72F, sunny";
}
}
function withRetry<T>(action: () => T, sleep: (ms: number) => void, jitter: () => number, tries = 3): T {
let wait = 1000; // milliseconds before the second try; it doubles each time
for (let attempt = 1; ; attempt++) {
try {
return action();
} catch (error) {
if (!(error instanceof SkyCastDown) || attempt === tries) throw error; // out of tries: give up, honestly
sleep(wait + jitter());
wait *= 2;
}
}
}
function briefing(getWeather: () => string): string {
try {
return "Weather: " + getWeather();
} catch (error) {
if (error instanceof SkyCastDown) return "Weather: unavailable";
throw error;
}
}
const skyCast = new FakeSkyCast(2);
console.log(briefing(() => withRetry(() => skyCast.weather(), (ms) => console.log(`Waiting ${ms} ms`), () => 250)));
SkyCast call 1: no answer
Waiting 1250 ms
SkyCast call 2: no answer
Waiting 2250 ms
SkyCast call 3: 72F, sunny
Weather: 72F, sunny
Same lines again. withRetry doesn't know it's talking to SkyCast; tomorrow it can retry a call to the Bolt lock. In JavaScript and TypeScript a real wait doesn't block (you await a timer), so a real withRetry is an async function; the stand-in here keeps the example in order.
Retry only what's safe to repeat
There's a catch hiding in every retry. When a call "fails", you don't always know whether it did nothing. Sometimes the request reached the device, the device did it, and only the answer got lost on the way back. Then your retry does it a second time.
So before retrying, ask: is doing this twice the same as doing it once? A call with that property is idempotent (said eye-DEM-po-tent):
| The call | Twice is the same as once? | Retry it? |
|---|---|---|
| Get the weather | yes: nothing changes | yes |
| Turn the kitchen light on | yes: on twice is still on | yes |
| Unlock the front door | yes: unlocked is unlocked | yes |
| Brightness up 10% | no: twice is 20% | not as it is |
| Skip to the next song | no: twice skips two | not as it is |
A call that isn't safe can often be made safe by saying what you want instead of what to change: "set the brightness to 60%" can be retried; "brightness up 10%" can't.
How it got its name
Retrying is as old as networks, so there's no famous first paper; the parts got their own names over time. Waiting longer each time is backoff (doubling each wait is exponential backoff), and the random extra is jitter. Together they're usually called retry with backoff. It isn't in the original 1994 pattern book, which is about objects inside one program; it's one of the patterns for programs that talk across a network, as the next lesson's circuit breaker is.
Where you've already used it
You rarely write the loop yourself at work: every language has a library that does it, with backoff, jitter and "retry only these errors" as settings. Here's the same retry in each (shown, not run here; versions checked on 2026-11-01):
// .NET: Polly 8.9.0 (dotnet add package Polly.Core --version 8.9.0)
using Polly;
using Polly.Retry;
ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions
{
ShouldHandle = new PredicateBuilder().Handle<SkyCastDownException>(), // retry only this error
MaxRetryAttempts = 2, // 3 tries in all
Delay = TimeSpan.FromSeconds(1), // wait 1 s, then 2 s
BackoffType = DelayBackoffType.Exponential,
UseJitter = true, // plus a random extra
})
.Build();
string weather = pipeline.Execute(() => skyCast.Weather());
// Java: Resilience4j 2.4.0 (Maven: io.github.resilience4j:resilience4j-retry:2.4.0)
import io.github.resilience4j.core.IntervalFunction;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
import java.time.Duration;
import java.util.function.Supplier;
RetryConfig config = RetryConfig.custom()
.maxAttempts(3) // 3 tries in all
.intervalFunction(IntervalFunction.ofExponentialRandomBackoff(Duration.ofSeconds(1), 2.0)) // 1 s, then 2 s, each with a random extra
.retryExceptions(SkyCastDown.class) // retry only this error
.build();
Retry retry = Retry.of("skycast", config);
Supplier<String> weather = Retry.decorateSupplier(retry, skyCast::weather);
String today = weather.get();
# Python: tenacity 9.2.1 (pip install tenacity==9.2.1)
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential, wait_random
@retry(
retry=retry_if_exception_type(SkyCastDown), # retry only this error
stop=stop_after_attempt(3), # 3 tries in all
wait=wait_exponential(multiplier=1) + wait_random(0, 0.5), # 1 s, then 2 s, each plus up to half a second
)
def get_weather():
return skycast.weather()
// JavaScript: cockatiel 4.0.0 (npm install cockatiel@4.0.0)
import { ExponentialBackoff, handleType, retry } from "cockatiel";
const retryPolicy = retry(handleType(SkyCastDown), { // retry only this error
maxAttempts: 2, // 2 more tries after the first: 3 in all
backoff: new ExponentialBackoff({ initialDelay: 1000 }), // waits that grow, each with a random extra
});
const weather = await retryPolicy.execute(() => skyCast.weather());
// TypeScript: cockatiel 4.0.0 (npm install cockatiel@4.0.0)
import { ExponentialBackoff, handleType, retry } from "cockatiel";
const retryPolicy = retry(handleType(SkyCastDown), { // retry only this error
maxAttempts: 2, // 2 more tries after the first: 3 in all
backoff: new ExponentialBackoff({ initialDelay: 1000 }), // waits that grow, each with a random extra
});
const weather: string = await retryPolicy.execute(() => skyCast.weather());
Each one has the three parts you built: a limit, a growing wait with a random extra, and a list of the errors worth retrying. Python's tenacity covers retries only; the next lesson names Python's library for circuit breakers.
You've also met retries without writing any: your phone retries a text that didn't send, and an email app retries a message stuck in the outbox.
When not to use it
- When the call isn't safe to repeat, unless you make it safe first (see the table above).
- When the error won't fix itself. A wrong password, a station that doesn't exist or a request the service refuses will fail the same way every time. Retry only the errors that mean "try again later".
- When someone is waiting and the answer is no longer useful. Six seconds of retries on a light switch feels broken. Keep the total wait short where a person is watching.
- When the service is really down, not hiccuping. Three polite tries on every call still add up when every call fails for an hour. That's the next lesson's problem.
The cost: each retry is more waiting, and a retry hides a failure that someone may want to know about, so log each one.
How often you'll meet it: every day (our estimate: almost any code that calls another service over a network retries, through a library or a setting).
Check yourself
2 questions to try on the lesson page.
So what
A call across a network can fail for a second and work the next. Give it a few polite tries, each a little later and a little random, then give up honestly, and only for calls that are safe to make twice.