Go retry backoff is a simple idea. When a call fails, wait a little and try again, then wait longer each time. In this post, you will learn why a plain retry loop can make an outage worse. Then we will build a safe retry helper with exponential delay and random jitter.
This lesson builds on Go error handling and Go context for beginners. We also use the Go select statement to wait safely. So it helps to read those first.
What Is Go Retry Backoff?
Some failures are short, so they pass quickly. For instance, a network packet gets lost, a server restarts or a database is busy for a second. If you try again a moment later, the call often works. So a retry is just that second try.
Backoff means that the wait grows after each failure. For example, you wait 100 milliseconds, then 200, then 400. Because the delay doubles, the pattern is called exponential backoff. Go retry backoff combines both ideas in a few lines of code.
Think of calling a friend who does not pick up the phone. You do not call ten times in one second, since that would only annoy your friend. Instead, you wait a few minutes, call again, and then wait longer. In the same way, your program gives the other side time to recover. That is the whole point of Go retry backoff.
Why Go Retry Backoff Matters
Networks are not perfect, so services in a chain fail from time to time. Without retries, one small glitch becomes a visible error for your user. A good Go retry backoff helper hides most of these glitches. However, a retry loop without waiting is dangerous. Here is what good Go retry backoff gives you.
- Fewer visible errors: short problems fix themselves before the user sees them.
- Relief for a busy server: longer waits give an overloaded service time to recover.
- Predictable cost: a limit on tries stops a bad call from running forever.
- Cleaner code: one helper replaces many copied loops.
Now think about what happens when a server fails and a thousand clients retry at once. Then each client sends a new request at the same moment. As a result, the server gets hit again and again, and it never recovers. This problem has a name, and it is the thundering herd.
How Go Retry Backoff Works
The Go retry backoff helper runs your function. When it works, we return at once. When it fails with a temporary error, we wait and try again. Then the delay grows, up to a maximum.

Look at the diagram from left to right. Each failed try is followed by a longer wait, so the gaps grow. After the last allowed try, the helper gives up and returns the error.
Exponential Delay in One Line
Because the delay doubles, the wait for attempt number n is base * 2^n. In Go, a left shift does this job: base << attempt. We also cap the result at a maximum, because a delay of several minutes helps nobody.
Why Jitter Matters
Jitter also means a random amount of time. With full jitter, we pick a random wait between zero and the current delay. Therefore, a thousand clients no longer retry at the same moment, and the load on the server drops. Instead, their retries spread out over time, and the server gets room to breathe.
Which Errors Deserve a Retry
However, not every error should be retried. A timeout or a 503 status is temporary, so a retry makes sense. A 400 status means your request is wrong, so it will fail again. Likewise, a “not found” error will not change, even after an hour. We mark retryable errors with a special error value and check it with errors.Is. The table below shows common cases and the right choice.
| Situation | Example | Retry? |
|---|---|---|
| Network timeout | Request took too long | Yes, it may pass next time |
| Server busy | 503 status | Yes, after a wait |
| Too many requests | 429 status | Yes, with a longer wait |
| Bad input | 400 status | No, fix the request |
| Missing item | 404 status | No, it will not appear |
| Wrong login | 401 status | No, refresh the token first |
When in doubt, do not retry. A missed retry costs one error, but a wrong retry can repeat a side effect.
Go Retry Backoff vs Fixed Delay
A fixed delay is the simplest option. However, it has weaknesses. The table below compares the four common styles.
| Style | How the wait changes | Main weakness |
|---|---|---|
| No delay | Zero wait | Hammers a failing server |
| Fixed delay | Same wait each time | Clients still retry together |
| Exponential backoff | Doubles up to a cap | Clients can still line up |
| Backoff with jitter | Random wait up to the cap | Slightly harder to test |

So the best choice for most services is backoff with jitter. The extra code is only a few lines, yet the effect on a busy system is large.
Go Retry Backoff Hands-On: A Reusable Helper
Now let’s write the helper. We build it in two small pieces and then one function that uses them.
Step 1: The Delay Functions
First, backoff returns the capped exponential delay. Next, withJitter picks a random value between zero and that delay.
var ErrTemporary = errors.New("temporary failure")
func backoff(attempt int, base, max time.Duration) time.Duration {
d := base << attempt
if d > max || d <= 0 {
d = max
}
return d
}
func withJitter(d time.Duration) time.Duration {
return time.Duration(rand.Int64N(int64(d) + 1))
}
The check d <= 0 matters. After many shifts, the number can overflow and turn negative. In that case, we fall back to the maximum. Also, rand.Int64N comes from math/rand/v2, so you do not need to set a seed.
Step 2: The Retry Function
Then comes the main function. It calls your code, stops on success, and stops at once for a permanent error. Otherwise, it waits with select, so a cancelled context ends the wait early.
func Retry(ctx context.Context, maxTries int, base, max time.Duration, fn func() error) error {
var err error
for attempt := 0; attempt < maxTries; attempt++ {
if err = fn(); err == nil {
return nil
}
if !errors.Is(err, ErrTemporary) {
return err
}
if attempt == maxTries-1 {
break
}
wait := withJitter(backoff(attempt, base, max))
select {
case <-time.After(wait):
case <-ctx.Done():
return ctx.Err()
}
}
return fmt.Errorf("gave up after %d tries: %w", maxTries, err)
}
Let’s look at the key lines. errors.Is finds ErrTemporary even when it is wrapped. The select waits for either the timer or the context, whichever comes first. Finally, %w keeps the original error for the caller.
Step 3: Try It
Then we test four cases. We print the plain delay table first, because jitter would hide it. After that, we run four calls. One is flaky, one always fails, one has a permanent error and one has a short deadline.
for i := 0; i < 6; i++ {
fmt.Printf("attempt %d: wait up to %v\n", i+1, backoff(i, 100*time.Millisecond, 2*time.Second))
}
Here is the real output from our run. The first six lines come from the loop above, and the rest come from the four test calls.
attempt 1: wait up to 100ms
attempt 2: wait up to 200ms
attempt 3: wait up to 400ms
attempt 4: wait up to 800ms
attempt 5: wait up to 1.6s
attempt 6: wait up to 2s
flaky call: calls = 3 err = <nil>
always fails: calls = 3
gave up after 3 tries: temporary failure
permanent error: calls = 1 err = bad request
cancelled: context deadline exceeded
As you can see, the delay doubles until it hits the 2 second cap. Also, the flaky call succeeded on its third try. The permanent error was not retried, because it did not wrap ErrTemporary. Finally, the short deadline stopped the retries early. These results show Go retry backoff behaving as planned, and you can reuse the helper in any project.
Respecting the Retry-After Header
Some servers tell you how long to wait. They send a Retry-After header with a number of seconds. When you see it, use that value instead of your own guess. After all, the server knows its own state better than you do. Also, add a small random amount, so that clients still do not move in lockstep.
Common Mistakes with Go Retry Backoff
- Retrying forever: an endless loop can hide a real outage. So always set a maximum number of tries.
- Retrying every error: a bad request will never succeed. Therefore, retry only temporary errors.
- Skipping jitter: all clients then retry together. Instead, add random time to every wait.
- Ignoring the context: a sleeping retry loop blocks shutdown. So use
selectwithctx.Done(). - Retrying unsafe actions: a payment call may charge twice. For that reason, retry only calls that are safe to repeat.
Best Practices for Go Retry Backoff
- Cap the delay, because a wait of minutes is rarely useful. Most services pick a maximum between 2 and 30 seconds.
- Keep the try count small, usually three to five.
- Use idempotent calls, since the same request may arrive twice.
- Log each retry, so you can see patterns in your metrics.
- Combine retries with timeouts, because each try needs its own deadline.
- Pair retries with a circuit breaker, which stops calls to a service that stays down.
Practice Task
To practise Go retry backoff, wrap the http.Get call from our Go HTTP client guide in the Retry helper. Treat status 503 and network timeouts as temporary.
Hint: write a small function that returns ErrTemporary for those cases and a plain error for the rest. After that, count the calls against a test server that fails twice.
Official Resources
These pages describe the packages and ideas used in this lesson.
Quick Recap
- Go retry backoff means trying again after a wait that grows each time.
- Cap the delay and the number of tries.
- Add jitter, so many clients do not retry together.
- Retry only temporary errors, and stop at once on permanent ones.
- Use
selectwith the context, so a cancel ends the wait early. - Retry only calls that are safe to repeat.
What’s Next?
By now, you can retry a failing call without hurting the server. However, retries alone do not help when a service stays down for a long time. So next, we will stop those calls with a circuit breaker.
Next up: Go circuit breaker pattern explained


