The incident: the API slowed down for a couple of minutes, which happens. But it didn't recover when the cause was fixed. Traffic stayed at several times the normal level until someone rate limited the mobile clients at the edge. The extra traffic came from installed apps retrying.

Retries are meant to hide short failures from users. Written carelessly, they multiply load exactly when the server can least handle it, and every installed copy of the app does it at once. Here's how that happens and what a well-behaved client does instead.

How retries amplify load

  • Immediate retries. A request fails, the app retries at once, maybe three times. During an outage every failing request becomes four requests. The server that was struggling with normal traffic now gets several times as much.
  • Retries at several layers. The networking library retries, the repository wrapper retries, and the screen has a "retry on error" effect. Three retries at each of three layers is up to 64 attempts for one user action.
  • Everyone retries at the same moment. Fixed delays ("retry after 5 seconds") keep all clients in step. When the server comes back, it gets a spike from every client at once, falls over again, and the cycle repeats.
  • Timeouts that are retried. A request that timed out may still be running on the server. Retrying it adds a second copy of the expensive work, not a replacement.
  • App launch after an outage. Users who saw errors reopen the app, and a cold start fires every startup request again. If those also retry, launch traffic can be the biggest spike of all.

What to retry, and what not to

Retry only when another attempt can plausibly succeed and is safe to repeat:

  • Usually retryable: connection failures before the request was sent, 502, 503, 504, and 429 (after waiting as told).
  • Not retryable: 400, 401, 403, 404, 409, 422. Another identical request will get the same answer. For 401, refresh the token once and retry once; don't loop.
  • Careful with non-idempotent requests. A POST that timed out may have succeeded. Retry it only with an idempotency key the server honors, or not at all.

Also check what your library already does. OkHttp, for example, can silently retry some connection failures on its own (retryOnConnectionFailure, on by default), so adding your own retry on top doubles it.

Back off, add jitter, cap it

A reasonable default for a mobile client:

  1. Exponential backoff: wait roughly 1 s, 2 s, 4 s, 8 s between attempts.
  2. Jitter: randomize each delay (for example, a random value between zero and the backoff), so clients spread out instead of retrying in sync.
  3. A small cap: two or three attempts for a user-facing request, then show the error with a retry button. A person tapping "Retry" is a far better signal than an automatic loop.
  4. One layer only: pick the place where retries happen (an interceptor, or the repository) and make every other layer pass errors through.

Listen to the server

  • Retry-After: on 429 and 503, the server can say how long to wait, either as seconds or as an HTTP date. Honor it, and treat it as a minimum, not a suggestion.
  • Back off globally, not per request. If one request gets a 503 with Retry-After: 30, the others to the same host will almost certainly fail too. Pausing that host for 30 seconds across the app saves a lot of pointless traffic.
  • Circuit breaker: after several consecutive failures to the same host, stop sending for a while and fail fast locally, then let a single request through to test whether it's back.
  • A server-side switch: a remote config value or a response header that tells clients to reduce polling or turn off non-essential calls is worth having before the next incident, not during it.

Background work

Background sync is where retry storms hide, because nobody is watching. Use the platform schedulers, which already add backoff and respect battery and network state: WorkManager with setBackoffCriteria on Android, BGTaskScheduler on iOS. Don't run your own tight retry loop inside a background task.

Testing it

  • Fake an outage. Point a debug build at a server or proxy rule that returns 503 for every request for two minutes, then recovers. Count the requests the app sends during and right after the outage. A well-behaved client sends few requests during it and doesn't spike when it ends.
  • Check Retry-After. Return 429 with Retry-After: 20 and confirm nothing goes to that host for at least 20 seconds.
  • Count the layers. Make one request fail with a connection error and count the attempts that actually leave the device. If the number is larger than your configured retry count, another layer is retrying too.
  • Kill the app mid-outage and reopen it. Watch the launch requests: they should respect the same backoff, not start from zero.

This article was created with the help of AI.