---
layout: pattern
title: Retry
folder: retry
permalink: /patterns/retry/
categories: Other
tags:
- Java
- Difficulty-Expert
- Performance
---
## Retry / resiliency
Enables an application to handle transient failures from external resources.
## Intent
Transparently retry certain operations that involve communication with external
resources, particularly over the network, isolating calling code from the
retry implementation details.

## Explanation
The `Retry` pattern consists retrying operations on remote resources over the
network a set number of times. It closely depends on both business and technical
requirements: how much time will the business allow the end user to wait while
the operation finishes? What are the performance characteristics of the
remote resource during peak loads as well as our application as more threads
are waiting for the remote resource's availability? Among the errors returned
by the remote service, which can be safely ignored in order to retry? Is the
operation [idempotent](https://en.wikipedia.org/wiki/Idempotence)?
Another concern is the impact on the calling code by implementing the retry
mechanism. The retry mechanics should ideally be completely transparent to the
calling code (service interface remains unaltered). There are two general
approaches to this problem: from an enterprise architecture standpoint
(**strategic**), and a shared library standpoint (**tactical**).
*(As an aside, one interesting property is that, since implementations tend to
be configurable at runtime, daily monitoring and operation of this capability
is shifted over to operations support instead of the developers themselves.)*
From a strategic point of view, this would be solved by having requests
be redirected to a separate intermediary system, traditionally an
[ESB](https://en.wikipedia.org/wiki/Enterprise_service_bus), but more recently
a [Service Mesh](https://medium.com/microservices-in-practice/service-mesh-for-microservices-2953109a3c9a).
From a tactical point of view, this would be solved by reusing shared libraries
like [Hystrix](https://github.com/Netflix/Hystrix)[1]. This is the type of
solution showcased in the simple example that accompanies this *README*.
In our hypothetical application, we have a generic interface for all
operations on remote interfaces:
```java
public interface BusinessOperation
[1] Please note that *Hystrix* is a complete implementation of the *Circuit
Breaker* pattern, of which the *Retry* pattern can be considered a subset of.
## Applicability
Whenever an application needs to communicate with an external resource,
particularly in a cloud environment, and if the business requirements allow it.
## Presentations
You can view Microsoft's article [here](https://docs.microsoft.com/en-us/azure/architecture/patterns/retry).
## Consequences
**Pros:**
* Resiliency
* Provides hard data on external failures
**Cons:**
* Complexity
* Operations maintenance
## Related Patterns
* [Circuit Breaker](https://martinfowler.com/bliki/CircuitBreaker.html)