public final class WaitStrategies extends Object
WaitStrategy
.
These factories return wait strategies that can be used with the library Guava Retrying.
There are three wait strategies documented below: ExponentialJitterWaitStrategy
,
ExponentialWaitStrategy
and CompositeJitterWaitStrategy
.
ExponentialJitterWaitStrategy
WaitStrategy
is helpful whenever there are competing clients that
are trying to call a remote server at the same time.
The main benefits of this strategy is that it avoids overwhelming the server by multiple requests by slowing clients down using the classic exponential backoff. Moreover, since the reason of an impaired server might be due to the wave of requests coming from the clients, this strategy introduce an element of randomness (aka jitter) that ensures the requests are spread out along a randomized interval that grows exponentially for each attempt. The retry is capped to a maximum timeout in order to avoid the interval to grow indefinitely.
The interval for the i-th retry is calculated in the following way:
min_value = exponential_wait * (1 - randomization_factor)
max_value = exponential_wait
Where exponential_wait
is the exponential time calculated for the i-th attempt and
randomization_factor
is a double belonging to the interval [0.0, 1.0].
Example: Let's suppose that the randomization factor is 0.5, the base timeout is one second and the max timeout is 10 seconds. For five retries the sequence will be (values in seconds):
Attempt | Interval |
1 | [0.5, 1] |
2 | [1, 2] |
3 | [2, 4] |
4 | [4, 8] |
5 | [5, 10] |
Choose the randomization factor wisely according to your use case. The more the randomization factor tends to zero the more the retry behavior will look like the classic exponential backoff. This can be useful whenever we know that the cause of an impaired server is not due to the competing clients. Conversely, the more the randomization factor tends to one the more the requests will be spread along the interval, allowing the server to digest the request more gradually and reducing contention between competing clients.
ExponentialWaitStrategy
WaitStrategies.exponentialWait()
class based on
Duration
instead of long for declaring the delays.
The main benefits of this strategy is that it avoids overwhelming the server by multiple requests by slowing clients down using the classic exponential backoff. The retry is capped to a maximum timeout in order to avoid the interval to grow indefinitely.
The timeout for the i-th attempt is calculated in the following way:
exponential_wait = min(max_timeout, 2**(attempts-1))
Where attempt
is the number of current attempts and max_timeout
is the capped value for the
calculated timeout.
Example: Let's suppose that the base timeout is one second and the max timeout is 10 seconds. For five retries the sequence will be (values in seconds):
Attempt | Timeout |
1 | 1 |
2 | 2 |
3 | 4 |
4 | 8 |
5 | 10 |
This strategy is equivalent to ExponentialJitterWaitStrategy
with randomization factor equals to zero.
WaitStrategy
helps you combining an existing waiting strategy with
a randomization factor.
The interval for the i-th retry is calculated in the following way:
min_value = wait * (1 - randomization_factor)
max_value = wait
Where wait
is the wait time calculated for the i-th attempt by the existing waiting strategy and
randomization_factor
is a double belonging to the interval [0.0, 1.0].
Retryer
,
WaitStrategies
Modifier and Type | Method and Description |
---|---|
static com.github.rholder.retry.WaitStrategy |
compositeJitterWait(com.github.rholder.retry.WaitStrategy waitStrategy,
double randomizationFactor)
Return an instance of
CompositeJitterWaitStrategy . |
static com.github.rholder.retry.WaitStrategy |
exponentialJitterWait()
Return an instance of
ExponentialJitterWaitStrategy with the default base timeout (1 second), the default
randomization factor (0.5) and without a max timeout. |
static com.github.rholder.retry.WaitStrategy |
exponentialJitterWait(Duration maxTimeout)
Return an instance of
ExponentialJitterWaitStrategy with the default base timeout (1 second) and
the default randomization factor (0.5). |
static com.github.rholder.retry.WaitStrategy |
exponentialJitterWait(Duration maxTimeout,
double randomizationFactor)
Return an instance of
ExponentialJitterWaitStrategy with the default base timeout (1 second). |
static com.github.rholder.retry.WaitStrategy |
exponentialJitterWait(Duration baseTimeout,
Duration maxTimeout)
Return an instance of
ExponentialJitterWaitStrategy with the default randomization factor (0.5). |
static com.github.rholder.retry.WaitStrategy |
exponentialJitterWait(Duration baseTimeout,
Duration maxTimeout,
double randomizationFactor)
Return an instance of
ExponentialJitterWaitStrategy . |
static com.github.rholder.retry.WaitStrategy |
exponentialWait(Duration maxTimeout)
Return an instance of
ExponentialWaitStrategy . |
static com.github.rholder.retry.WaitStrategy |
exponentialWait(Duration baseTimeout,
Duration maxTimeout)
Return an instance of
ExponentialWaitStrategy . |
public static com.github.rholder.retry.WaitStrategy exponentialJitterWait()
ExponentialJitterWaitStrategy
with the default base timeout (1 second), the default
randomization factor (0.5) and without a max timeout.ExponentialJitterWaitStrategy
public static com.github.rholder.retry.WaitStrategy exponentialJitterWait(Duration maxTimeout)
ExponentialJitterWaitStrategy
with the default base timeout (1 second) and
the default randomization factor (0.5).maxTimeout
- the calculated time will be capped to this value.ExponentialJitterWaitStrategy
public static com.github.rholder.retry.WaitStrategy exponentialJitterWait(Duration baseTimeout, Duration maxTimeout)
ExponentialJitterWaitStrategy
with the default randomization factor (0.5).baseTimeout
- the base time delay.maxTimeout
- the calculated time will be capped to this value.ExponentialJitterWaitStrategy
public static com.github.rholder.retry.WaitStrategy exponentialJitterWait(Duration maxTimeout, double randomizationFactor)
ExponentialJitterWaitStrategy
with the default base timeout (1 second).maxTimeout
- the calculated time will be capped to this value.randomizationFactor
- the randomization factor for creating a range around the retry interval.ExponentialJitterWaitStrategy
public static com.github.rholder.retry.WaitStrategy exponentialJitterWait(Duration baseTimeout, Duration maxTimeout, double randomizationFactor)
ExponentialJitterWaitStrategy
.baseTimeout
- the base time delay.maxTimeout
- the calculated time will be capped to this value.randomizationFactor
- the randomization factor for creating a range around the retry interval.ExponentialJitterWaitStrategy
public static com.github.rholder.retry.WaitStrategy exponentialWait(Duration baseTimeout, Duration maxTimeout)
ExponentialWaitStrategy
.baseTimeout
- the base time delay.maxTimeout
- the calculated time will be capped to this value.ExponentialWaitStrategy
public static com.github.rholder.retry.WaitStrategy exponentialWait(Duration maxTimeout)
ExponentialWaitStrategy
.maxTimeout
- the calculated time will be capped to this value.ExponentialWaitStrategy
public static com.github.rholder.retry.WaitStrategy compositeJitterWait(com.github.rholder.retry.WaitStrategy waitStrategy, double randomizationFactor)
CompositeJitterWaitStrategy
.waitStrategy
- the wait strategy used for combining with the random interval
generated by the randomization factor.randomizationFactor
- the randomization factor for creating a range around the retry interval.CompositeJitterWaitStrategy
Copyright © 2016. All rights reserved.