Design a scalable text-to-image generation platform
A text-to-image platform (Stable Diffusion) serves GPU-heavy generation requests that each take seconds. Because latency is high, the design is an async job queue feeding a GPU worker pool, with results delivered via CDN.
AI & Machine Learning, 7 components, about 35 min
1. Scope
In scope
- • Generate images from prompts
- • Async job handling
- • Result storage + delivery
- • Generation parameters
Out of scope
- • Model training
- • Fine-tuning/LoRA pipeline
- • Content moderation deep-dive
2. Functional requirements
Must have
- • Submit a prompt, get an image
- • Handle multi-second generation async
- • Store + serve results
- • Control params (seed, guidance)
Nice to have
- • Negative prompts
- • Upscaling
- • Img2img / inpainting
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • 10K generation requests/min peak
- • ~5 s/image on A100
- • Image ~1 MB
- • Results cached on CDN
Derived numbers
5. API design
{ prompt, params } → { jobId }
Poll status / get result URL (or WS/webhook)
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
Why async
Generation takes seconds — far too long to hold an HTTP request open at scale. The API enqueues a job and returns immediately; workers pull from the queue, generate, and notify the client (poll, WebSocket, or webhook). The queue also smooths bursts: requests buffer when GPUs are saturated instead of failing.
Latent diffusion
Running diffusion directly on pixels is expensive. Latent diffusion compresses the image into a smaller latent space (via a VAE), runs the iterative denoising steps there, then decodes back to pixels — drastically cutting compute per image while keeping quality.
GPU autoscaling & batching
GPUs are the bottleneck and the cost center. Autoscale the worker pool on queue depth, and batch multiple generations per GPU step where latency budget allows. Keep model weights warm in GPU memory to avoid reload latency.
Delivery & params
Results go to S3 and are served via CDN (immutable, cacheable). Expose generation controls — seed (reproducibility), guidance scale (prompt adherence), negative prompts, and optional multi-stage upscaling for high-res output.
9. Bottlenecks
- GPU capacity → queue + autoscale, batch where possible
- Cold model load → keep weights warm
- Spiky demand → async absorbs bursts
10. Trade-offs
Sync or async API
Choose: Async: submit returns a job id; clients poll or receive a webhook or stream of progress.
Instead of: Synchronous HTTP: simple, but 5–10 s requests tie up connections and fail on any network blip.
GPU work takes seconds and queues fluctuate.
Quality versus cost
Choose: Fewer diffusion steps with a distilled model for drafts; full steps for final renders.
Instead of: Full quality always.
Step count is roughly linear in GPU time.
Safety checks
Choose: Prompt filtering before generation and image classification after.
Instead of: Only one side: prompts can be phrased innocently and still yield harmful images.
Both checks catch different failures.
11. Failure modes
GPU fleet saturated
Queue times of minutes.
Mitigation: Priority queues by tier, autoscaling on queue depth, and honest wait estimates.
Worker dies mid-job
Job lost.
Mitigation: Visibility timeout requeues the job; results written idempotently by job id.
Abuse generating harmful content
Legal and reputational harm.
Mitigation: Layered filters, account-level enforcement, and provenance metadata or watermarking.
12. What interviewers probe
Answer each one yourself before opening it.
How many GPUs for 10K requests per minute at 5 s each?Show a strong answer
10K × 5 s = 50K GPU-seconds per minute, so ~830 GPUs busy at once; add headroom for peaks and batching effects.
How do you batch diffusion requests?Show a strong answer
Group requests with the same resolution and step count into one forward pass per step.
Where are results stored and for how long?Show a strong answer
Object storage with CDN delivery, lifecycle rules to expire unsaved results after days.
How do you report progress?Show a strong answer
Workers publish step counts to a pub/sub channel per job; the client receives them over SSE or WebSocket.
Sign in to track this guide in your curriculum progress.