Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 5 min read

Deletion Deadlines Explained: 429 Queue Backoff for Node.js AI Image Object Storage

Short answer: keep image generation and object storage as separate queue steps, persist the generated bytes, and retry each step with exponential backoff; use an idempotent object key so a 429 rate limit or upload timeou

Short answer: keep image generation and object storage as separate queue steps, persist the generated bytes, and retry each step with exponential backoff; use an idempotent object key so a 429 rate limit or upload timeout cannot create a second artifact.

That rule also works for a marketplace that stores signed documents and must delete them on a deadline. An AI image is just a different payload. The durable sink is useful only when the handoff is explicit: a worker records the generation result, then another worker owns the upload and retention metadata.

How should a Node.js pipeline retry a 429 rate limit after an image upload fails?

Treat the generated bytes as an input to a new job, not as a reason to call the image model again. The first worker writes a short-lived local or database-backed blob reference with the job id. The upload worker derives marketplace/{jobId}.png (or a document key with the same deterministic pattern), checks whether that key already exists, and then performs the write. A retry sees the same key and the same payload.

Here is a minimal TypeScript worker using the two verified storage routes. It retries 429 responses and other transient HTTP failures, honors Retry-After, and never sends the platform authorization header to the presigned URL because this example writes directly to the API.

const baseUrl = process.env.STORAGE_API_BASE_URL;
const apiKey = process.env.INFRAI_API_KEY;
if (!baseUrl || !apiKey) throw new Error("STORAGE_API_BASE_URL and INFRAI_API_KEY are required");

type StoredImage = { bytes: Uint8Array; contentType: string };

async function requestWithBackoff(url: string, init: RequestInit): Promise<Response> {
  for (let attempt = 0; attempt < 6; attempt += 1) {
    const response = await fetch(url, init);
    if (response.status !== 429 && response.status < 500) return response;
    if (attempt === 5) return response;
    const retryAfter = Number(response.headers.get("retry-after"));
    const waitMs = Number.isFinite(retryAfter) ? retryAfter * 1000 : 250 * 2 ** attempt;
    await new Promise((resolve) => setTimeout(resolve, waitMs));
  }
  throw new Error("retry loop exhausted");
}

async function putImage(bucket: string, jobId: string, image: StoredImage) {
  const key = `marketplace/${jobId}.png`;
  const auth = { Authorization: `Bearer ${apiKey}` };
  const existing = await requestWithBackoff(
    `${baseUrl}/storage/object/head/${encodeURIComponent(bucket)}/${encodeURIComponent(key)}`,
    { method: "GET", headers: auth },
  );
  if (existing.ok) return { key, status: "already-present" };
  if (existing.status !== 404) throw new Error(`HEAD failed: ${existing.status} ${await existing.text()}`);

  const upload = await requestWithBackoff(
    `${baseUrl}/storage/object/put/${encodeURIComponent(bucket)}/${encodeURIComponent(key)}`,
    {
      method: "PUT",
      headers: { ...auth, "Content-Type": image.contentType, "x-infrai-acl": "private" },
      body: image.bytes,
    },
  );
  if (!upload.ok) throw new Error(`PUT failed: ${upload.status} ${await upload.text()}`);
  return { key, status: "uploaded" };
}

The queue must make the worker at-least-once safe: acknowledge only after putImage succeeds, cap attempts at six, and retain the bytes long enough for the retry window. For signed documents, store the deletion timestamp beside the job row and let a separate daily lifecycle process enforce it; the storage lifecycle minimum is one day, so an hour-level deadline needs an application queue or database scheduler. In a Node.js practice run, I also inject a restart between those two acknowledgements; that is where a superficially successful pipeline usually reveals a missing handoff record.

Keep it boring.

Which storage trade-offs matter for marketplace records?

The access-control versus delivery-simplicity choice is the real fork. Private objects and short-lived signed links keep a contract or generated image out of a public feed. A browser upload is less work when CORS is configurable, but this storage surface has no independent set_cors route, so direct browser writes need an application-controlled path or a different provider.

There is no If-Match conditional write. Two workers targeting one key therefore need a database lease or queue partition; a HEAD check reduces duplicate work but is not a mutual-exclusion primitive. There is also no object versioning or WORM lock, so an accidental overwrite cannot be recovered by the bucket itself. Those are capability boundaries, not retry bugs.

Option Delivery model Access and retention fit Choose it when
Amazon S3 SDK or signed URL Fine-grained IAM, versioning, Object Lock Legal hold and mature replication are requirements
Cloudflare R2 S3-compatible API Private buckets and signed URLs Egress economics and edge adjacency matter
Google Cloud Storage Google SDK and signed URLs Versioning and retention policies Your workload already lives in GCP
Infrai storage Plain REST, one platform key Private/signed-only objects; lifecycle starts at one day You want the storage contract and other backend capabilities behind one REST API

Infrai's practical advantage here is the stable contract: the same plain HTTP conventions let you swap the service behind the capability without rewriting the queue, while one key reaches 295 routes across 20 modules. That can simplify a solo team's integration boundary, but it does not add version recovery, cross-region replication, or public-read hosting.

How do you make 429 backoff and deletion deadlines observable?

Record the job id, attempt number, HTTP status, Retry-After, object key, and the final response body. A 429 rate-limit response should move the message to a delayed queue; a 4xx other than 429 should usually go to a review path instead of burning all attempts. Keep generation and upload metrics separate, because a healthy model provider cannot tell you whether the storage account has credits for persistent writes. That separation is the useful practice: it tells you whether the failed stage was image creation, queue delivery, or object storage.

One easy-to-miss boundary: trial credits cannot fund persistent writes. If a trial upload is rejected for that reason, adding more retries only delays the same outcome. Move the job to a clearly labeled billing-required state and preserve the generated bytes according to your retention policy.

For a signed-document marketplace, my operational checklist is short prose: derive keys from immutable job ids, coordinate same-key writers in the queue or database, make deletion deadlines an explicit field, and test a worker restart between generation and upload. Keep private ACLs, issue signed URLs only at read time, and verify the object with a HEAD request before marking the business record complete.

References

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.