Imagine placing an order in an e-commerce application.
The Order Service calls the Payment Service to process your payment. But due to a temporary network issue, the request fails.
Should the order fail immediately?
Not necessarily.
The Payment Service might be healthy, and the failure could be temporary. Giving the request another chance might be enough.
This is where the Retry Pattern comes in.
The Problem
In a distributed system, services communicate over networks. And networks are not always reliable.
A request can fail because of:
- Temporary network interruptions.
- Connection timeouts.
- A service being temporarily unavailable.
- Rate limiting.
- Short-lived resource contention.
Some failures are temporary and may succeed if we try again.
But blindly retrying every failure can make things worse.
Let's understand how to retry correctly.
Solution: Retry the Request
The basic idea is simple: when an operation fails due to a transient error, attempt it again.
Order Service
|
v
Payment Service
|
X Temporary failure
|
v
Retry
|
v
Payment Service
|
✓ Success
Instead of immediately returning an error, the Order Service gives the operation another chance.
But there is an important question:
How many times should we retry, and how long should we wait?
1. Set a Retry Limit
Never retry indefinitely.
For example, allow up to 3 attempts:
Attempt 1 ---> Failure
Attempt 2 ---> Failure
Attempt 3 ---> Failure
|
v
Return Error
A retry limit prevents one failing operation from consuming resources forever.
The limit should depend on the operation, its latency requirements, and the system's overall timeout budget.
2. Use Exponential Backoff
What if we retry immediately after every failure?
When a service is overloaded, thousands of clients might retry at the same time, creating even more load.
This is known as a Retry Storm.
Instead, gradually increase the delay between retries.
| Retry | Example delay |
|---|---|
| 1st | 1 second |
| 2nd | 2 seconds |
| 3rd | 4 seconds |
| 4th | 8 seconds |
The general formula is:
delay = base_delay * 2^retry_count
This is called Exponential Backoff.
Add Jitter
Imagine thousands of clients all retrying after exactly 4 seconds.
They could overwhelm the service again.
To avoid synchronized retries, add a random delay called Jitter.
Retry Delay = Random(0, Exponential Backoff Delay)
This spreads retry attempts over time.
In practice, use a maximum delay and a total retry/time budget as well.
3. Retry Only When It Makes Sense
Not every failure is retriable.
Consider these examples:
| Failure | Retry? |
|---|---|
| Temporary network error | Usually |
| Request timeout | Sometimes |
| HTTP 429 — Rate Limited | Usually, respect Retry-After |
| HTTP 503 — Service Unavailable | Often |
| HTTP 400 — Bad Request | No |
| Invalid payment details | No |
The exact decision depends on the API and the error semantics.
Retrying an invalid request will not make it valid.
4. The Hidden Danger: Duplicate Operations
This is especially important for payment systems.
Suppose the Order Service sends a payment request.
The Payment Service processes the payment successfully, but the response gets lost due to a network issue.
Order Service Payment Service
| |
|------ Charge $100 ------->|
| |
| Payment Success
| |
|<---- Response Lost -------X
|
|------ Retry $100 -------->|
| |
| Payment Again?
The Order Service doesn't know whether the first request succeeded.
If it retries without protection, the customer could be charged twice.
Solution: Idempotency
Use an idempotency key for operations that create side effects.
{
"order_id": "ORD-123",
"amount": 100,
"idempotency_key": "payment-ORD-123"
}
The Payment Service stores the result associated with this key.
If the same request arrives again, it returns the previous result instead of charging the customer again.
This makes retries safer.
5. Retry Is Not a Complete Reliability Strategy
Retry helps recover from temporary failures, but it has limitations.
What if the Payment Service is completely down?
Retrying 3 times might still fail. And if every request keeps retrying, the Order Service continues spending resources on an unhealthy dependency.
This is where another pattern becomes useful: Circuit Breaker.
The Retry Pattern gives a failed request another chance.
The Circuit Breaker Pattern decides when to stop making calls to a failing service.
You can explore it in the next article
TL;DR
Retry helps recover from transient failures by attempting an operation again. Use retry limits, exponential backoff, jitter, and idempotency to avoid turning temporary failures into bigger problems.
Key takeaway: Retry is useful when a failure might be temporary. It should never be an excuse to retry blindly.
Top comments (0)