Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Retry, Backoff, and Jitter for NHTSA DecodeVinValues Without Thundering Herds

Public NHTSA vPIC endpoints do not give you a private SLA. When your free VIN tool, a marketplace embed, and a cron job all refresh at once, naive retries make outages worse. Every client wakes up, fails, sleeps the same 500ms, and hits DecodeVinValues together again. This post covers a small TypeScript retry helper with exponential backoff and jitter, plus product rules so retries do not lie to…

Public NHTSA vPIC endpoints do not provide a private Service Level Agreement (SLA). When your free VIN tool, a marketplace embed, and a cron job all attempt to refresh simultaneously, naive retries can exacerbate outages. Every client wakes up, encounters a failure, waits the same 500 milliseconds, and then hits DecodeVinValues again simultaneously.

This article outlines a TypeScript retry helper with exponential backoff and jitter, along with product rules to prevent retries from misleading users or triggering a stampede to the upstream API. What warrants a retry? Transient failures include network resets and timeouts, HTTP 429 (rate limited), HTTP 502/503/504, and empty bodies or parse errors that appear transient (exercise caution; consider them carefully).

Do not treat as transient local validation failures (length, I/O/Q, check digit), clear 400-style "bad VIN" responses if you can classify them, or HTTP 200 responses with sparse fields (success but with incomplete data). Business logic you devise (Make was blank, try again) should also be considered. Blindly retrying sparse 200s with incomplete data wastes quota and delays honest empty-field UI.

Implement single-flight before retrying, as collapsing duplicate in-flight requests for the same VIN is more crucial than complex backoff curves. If ten React components mount and all attempt to decode 1HG..., you want only one upstream call. Utilize a `singleFlight` function to accomplish this. Wrap the entire `fetchWithRetry` in `singleFlight(vin, ...)` instead of applying it to each attempt individually.

Use exponential backoff with full jitter, following AWS-style implementation. Full jitter picks a random delay uniformly between 0 and the current cap, spreading load better than everyone sleeping exactly 2^attempt seconds. Define a `RetryOpts` interface with `maxAttempts` (inclusive of the first try), `baseMs` (default 200 ms), `maxMs` (default 8,000 ms), and `retryOn` (function to determine if an error should be retried).

Implement a `sleep` function using `setTimeout` to pause execution for a specified number of milliseconds. Create a `fullJitter` function that returns a random delay between 0 and the cap by multiplying the random number between 0 and 1 with the cap and flooring the result. Finally, implement a `withRetry` function that retries a given function up to `maxAttempts` times, with an exponential backoff and jitter delay between attempts.

Start with `attempt = 1` and increment it until it reaches `maxAttempts`. Use a try-catch block to invoke the provided function `fn()`. If an error occurs, store it in `lastErr` and check if the `attempt` equals `maxAttempts` or if `retryOn(err)` returns false; if so, rethrow the error. Calculate the cap as the minimum value between `maxMs` and `baseMs * 2 ** (attempt - 1)`, then await a sleep with `fullJitter(cap)` before retrying. If all attempts fail, throw the last error encountered.

Rewritten by urgent.news; may contain errors — check the original.

Read the original at dev.to →

Report a problem with this story

More in Tech

RepoDNA v1.2.2

RepoDNA v1.2.2: A Small Follow-Up for Better Mobile Tables and npm Releases RepoDNA v1.2.2 is now available. This release is intentionally small.

  • Improved table scrolling on mobile devices with consistent shading across entire edge
  • Predictable npm releases by checking if older version should be published after newer
  • RepoDNA remains unchanged, open-source repository intelligence tool with versioned artifact

The Cache Stampede Problem: Why a Popular Cache Key Can Take Down Your Backend

Caches are supposed to protect your backend from load. Most of the time they do. But there is a specific failure mode where the cache itself becomes the trigger for an outage: the cache stampede, also…

  • Cache stampede occurs when multiple requests hit backend simultaneously after cache miss.
  • Probabilistic early expiration minimizes chance of many requests served from cache at stale moment.

More from Wednesday 7 October →