Reliability
Retry
Retry a call or an operation by annotating it with @Retry: attempts, backoff, jitter, which failures are retried, and when a retry is not applied.
@Retry runs an operation again when it fails, so a call that fails now and then, such as a payment provider that times out or an API that answers 503, succeeds without any loop in your code. It is the annotation version of the retry loops you would otherwise write yourself.
@Injectable
open class PaymentClient {
@Retry(attempts = 5, delayMillis = 200, multiplier = 2.0, maxDelayMillis = 5_000, statuses = [429, 503])
open fun charge(order: Order): Receipt = gateway.charge(order)
}The method runs once. If it throws, Kest waits and runs it again, up to attempts times in total. Here the waits are 200 ms, 400 ms, 800 ms and 1.6 s, each within a 5 s ceiling and with a little random spread so that many clients that failed together do not retry together.
Where it applies
@Retry works on any open function of a class that Kest creates: a service, a repository method, or a client wrapper. Two rules follow from how it is built:
- The class and the function must be
open. Kest wraps the method at compile time with a subclass, so afinalmethod is not wrapped and runs once. - A call from inside the same class is not wrapped. If
chargecallsretryingChargeonthis, the retry does not happen. Call the annotated method through the injected service.
An annotation on the class applies to each public method of the class that has no aspect annotation of its own, so a method with its own @Retry keeps it.
Suspend functions are retried in the same way, and the waits suspend the coroutine rather than blocking a thread.
The options
| Option | Default | What it does |
|---|---|---|
attempts | 3 | The total number of runs, including the first. 1 means no retry. A value below 1 is treated as 1. |
delayMillis | 100 | The wait before the first retry. |
multiplier | 2.0 | Each later wait is the previous one times this number. |
maxDelayMillis | 10_000 | The longest any single wait may be. |
jitter | 0.2 | Each wait changes by up to this fraction, up or down. 0 makes the waits exact. |
on | empty | Retry only these exception types (and their subtypes). Empty means every exception. |
stopOn | empty | Never retry these exception types, even if on matches. |
statuses | empty | Retry only failures that carry one of these HTTP status codes, such as [429, 503]. Empty means no status check. |
Use on or statuses to avoid retrying errors that will never succeed. Retrying a 400 Bad Request only repeats the same mistake, so list the transient statuses instead:
@Retry(attempts = 4, statuses = [408, 429, 500, 502, 503, 504])
open fun fetchRates(): Rates = api.rates()An error that is never retried, whatever the options say: the circuit-open error from @CircuitBreaker, and any exception that implements NotRetryable.
What happens when every attempt fails
The last exception is thrown to the caller, unchanged. Kest does not wrap it, so the same error handling applies as for a single call. Each retry is logged as a warning, with the method, the attempt number, the error and the wait.
Combining with other annotations
Annotations on one method wrap each other from the outside in, in the order they are written. The first one written is the outermost. A common order is:
@TimeLimit(millis = 5_000) // outermost: every attempt has to finish within 5 s
@Retry(attempts = 3) // the attempts
@CircuitBreaker("payments") // innermost: a failing circuit stops the retries at once
open fun charge(order: Order): Receipt = gateway.charge(order)Put @CircuitBreaker inside @Retry, so that an open circuit ends the retries straight away rather than being retried. See Resilience for the breaker options, the bulkhead, the time limit and fallbacks.
Retrying a message handler or an HTTP call
@Retry applies to any Kest-created class, so it works the same on a microservice message handler or on a method that calls a named HTTP client. For a whole request to a remote service, prefer a retry around the client call: the handler then decides what to do with the final failure.
Deprecated: @RetryWith
@RetryWith from the resilience package still works, and it behaves the same way. It is deprecated in favour of @Retry, which has every option it had. Replace it with @Retry when you next touch the code.