Back to code Java

Reintentar una llamada que falla, esperando cada vez más

Reintentos con espera exponencial y un poco de azar, para no golpear a un servicio que ya está caído.

This post is not in your language yet; you are seeing it in Spanish. The code is the same.

The problem

Una llamada a un servicio externo falla de vez en cuando. Reintentar de inmediato y sin límite empeora la caída; no reintentar convierte un parpadeo en un error para el usuario.

Java
import java.util.concurrent.Callable;

public class Reintento {
    public static <T> T conBackoff(Callable<T> tarea, int intentos) throws Exception {
        long espera = 200;                       // milisegundos
        for (int i = 1; ; i++) {
            try {
                return tarea.call();
            } catch (Exception e) {
                if (i >= intentos) throw e;      // se acabaron: que suba el error real
                System.out.printf("Intento %d falló (%s). Espero %d ms%n", i, e.getMessage(), espera);
                Thread.sleep(espera + (long) (Math.random() * 100));
                espera *= 2;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        String r = conBackoff(() -> {
            if (Math.random() < 0.7) throw new RuntimeException("servicio caído");
            return "respuesta";
        }, 5);
        System.out.println(r);
    }
}

Why it is written this way

  1. La espera se duplica en cada intento: 200, 400, 800 ms. Le da aire al servicio para recuperarse.
  2. El azar sumado (jitter) evita que cien clientes reintenten todos en el mismo milisegundo.
  3. Cuando se agotan los intentos se relanza la excepción original, no una genérica: el que investiga ve la causa real.
  4. El método es genérico: sirve para cualquier tarea que devuelva algo.
How to try it

Guardalo como Reintento.java y corré: java Reintento.java. Cada corrida falla una cantidad distinta de veces.

Watch out

Solo se reintenta lo que es seguro repetir. Reintentar un cobro o un envío de correo sin una clave de idempotencia duplica la operación.

Adapt it to your case with AI

The button copies this prompt with the code pasted below it. Replace the brackets with your own details and paste it into ChatGPT, Claude, Gemini or Copilot.

The prompt
Tengo este reintento con backoff en Java. Adaptalo para que solo reintente ante estas excepciones: [EXCEPCIONES REINTENTABLES], tenga un tope de espera de [TOPE] segundos y registre cada intento con [MI LIBRERÍA DE LOGS]. Es para llamar a [QUÉ SERVICIO]. Decime también si esa operación es segura de reintentar y qué haría falta si no lo es.

Example written and reviewed by hand.

Share

Comments

No comments yet. Yours can be the first.

Plain text only, no links. No email is requested. Comments are reviewed before they are published.

Did you adapt it to something of yours?

Tell me what you solved with it, or what it was missing.

Keep trying

More code

See all
Darinel Ortega Online · I reply during the day
Today
Hello. I'm not selling anything here: this is for exchanging knowledge about technology.
Write whatever you like — you can send text, images or documents. Messages reach my console and I reply from there.
You are writing outside working hours. I will read you as soon as the day starts.

An open conversation to share knowledge. Messages reach my console and I reply from there.

Let us book a conversation

Pick the day and time that work for you. Thirty minutes, no sales pitch.

Video call

For a video call, just ask for one here and I'll send you the session link.