Video in progress

What is a retry, and in which cases should you not retry?

A retry repeats a call a few more times after a transient error. But only for transient errors (timeout, 503) and only for idempotent operations; retrying on 400, 404 or on operations that are dangerous to repeat, like a payment, does harm.

📖 Answer

Retries are very useful for short network blips and the momentary 503 during a deploy.

Cases where a retry makes sense:

  • a connect timeout, a refused connection;
  • 503 Service Unavailable, 429 Too Many Requests (but respect Retry-After);
  • safe requests like GET.

Cases where it's pointless or dangerous:

  • 400, 401, 404, 422: no matter how many times you send it, the result is the same;
  • POST /payments after a read timeout: the first request may actually have gone through, and the second charges the money twice.
resilience4j:
  retry:
    instances:
      catalog:
        max-attempts: 3
        wait-duration: 200ms
        retry-exceptions:
          - java.net.ConnectException

One more rule: keep the retry count small (2-3) and put a pause between attempts. Otherwise you pile even more requests onto a service that's already struggling.