Design an Asynchronous Random Image Generation Service
Accept slow image-generation requests, expose durable progress, notify clients after completion, and deliver immutable results safely.
Problem statement
Design the backend for a button that generates a random image.
Generation can be slow, so the request must complete asynchronously: pressing the button durably creates one generation job and returns immediately, while the client can observe progress and receive a completion notification that leads to the generated image.
For this exercise, the service chooses and persists one random seed and normalized generation specification when the request is admitted. Exact retries with the same idempotency key return the original job; conflicting reuse is rejected.
A successful job publishes one immutable image object before it becomes READY, and delivery uses a short-lived authorized reference rather than embedding image bytes in a notification. Failed jobs expose a stable failure outcome and may be retried under a bounded policy without changing job identity or random seed.
Polling remains a recovery path when notification delivery is delayed or duplicated. Model training, prompt-model internals, image editing, social feeds, and billing are outside scope. Numeric workload, latency, and retention values below are practice assumptions rather than source-reported facts.
What to build
- A signed-in client can idempotently submit a random-image generation request and immediately receive a stable job identifier.
- A client can read QUEUED, RUNNING, READY, FAILED, CANCELLED, or EXPIRED state and a monotonic status version.
- A READY or terminal failure transition emits a deduplicable notification while polling remains available for recovery.
- A READY job exposes its immutable generated image through a short-lived authorized delivery reference.
At scalePractice assumption
- Accept 500 new jobs per second at peak and retain up to 5 million nonterminal jobs.
- Generation normally completes within five minutes at p95, while submission and status reads complete within 300 milliseconds at p95.
- Each output is at most 20 MB, READY images remain available for 24 hours, and job metadata remains for 30 days.
- Worker delivery and notification delivery are at least once, so job transitions and notification consumption must be idempotent.