Skip to content
Shahariar Niloy, home

Why your retries are a memory leak

Per-attempt abort signals, timers that always clear, and why one-time actions should never retry.

Performance2 min readBy Shahariar Niloy

Retries feel like resilience. But a retry loop with a long timeout is also a promise to keep a request — and everything it references — in memory for a long time. Multiply that by a slow dependency and a busy server, and retries become the leak.

Clear the timer on every path

The most common bug: the timeout is cleared when the request succeeds, but not when it fails. Each failed attempt leaves a live timer holding its closure. Put the cleanup in a finally block so it runs no matter how the attempt ends.

One abort signal per attempt

Give each attempt its own AbortController. Reusing one across attempts means an old timer can cancel a new request — or worse, never cancel the old one.

Retry less, and politely

Never auto-retry one-time actions

Payments, bookings, cancellations: anything that must happen exactly once should not be retried automatically. A slow response doesn’t mean it failed — retrying can do it twice.

Case study: Taming memory
Next postThe fastest code doesn’t run on first load