Design a Text-to-Image Generation System
Understanding the Problem
🎨 What is a text-to-image generation system? A text-to-image system turns a written prompt ("a lighthouse on a cliff at dusk, oil painting") into one or more images in seconds. Midjourney, DALL·E, Imagen, Stable Diffusion and Flux are the well-known models; the products around them add prompt help, variations, upscaling, editing and a gallery. Under the hood, nearly all current systems are diffusion models: they start from random noise and remove it step by step, guided by the text, until an image appears.
Interviewers ask this as "design Midjourney," "design DALL·E," or "design an image generation API." It's one problem with two halves that you need to cover together: the model (how text becomes pixels, how it's trained and evaluated) and the serving system (a fleet of expensive GPUs, a queue of users who all want their images now, and a safety layer that must stop harmful content before anyone sees it). Most candidates do one half well. Strong answers connect them: the number of denoising steps is both a quality knob and the biggest line in the GPU bill.
Functional Requirements
We're building a consumer image generation product with a public API, at the scale of a popular service.
Core Requirements
- Users should be able to type a prompt and get a grid of 4 images at 1024×1024, with options for aspect ratio and style.
- Users should be able to make variations of an image and upscale one to a higher resolution.
- The system should rewrite short or vague prompts into detailed ones that the model renders well, while respecting what the user asked for.
- The system should block prompts and images that violate policy (sexual content involving minors, non-consensual imagery of real people, graphic violence, and so on), and mark every output as AI-generated.
- Paying users should get faster generation than free users, and everyone should see progress while they wait.
Below the line (out of scope)
- Inpainting, outpainting and region editing in depth (we'll note where they plug in).
- Video generation. That's Design a Text-to-Video Generation System.
- Billing, the social gallery and community features.
Non-Functional Requirements
Ask about volume, the tiers and what "fast" means. Our numbers: 10 million generation requests a day, each producing 4 images, so 40 million images a day. That averages 116 requests per second, with a peak around 350 requests per second (1,400 images per second) in the evening of the largest regions.
Core Requirements
- Latency for the fast tier: first preview within 2 seconds, the finished grid within 10 seconds at p50 and 30 seconds at p95. The slow (free or "relaxed") tier is best effort, minutes at peak.
- Quality: images should follow the prompt (right objects, counts, colors, text), look good, and match the requested style. We'll measure this; we can't guarantee it.
- Safety: no output in the most severe categories should ever reach a user. We accept some over-blocking of borderline prompts in exchange.
- Cost: GPU cost per image should be tracked and pushed down, since it dominates the unit economics.
- Availability: a GPU node failing should cost a retry, not a lost job.
Below the line (out of scope)
- Training compute planning for the base model (we'll discuss training, not cluster procurement).
- Copyright law. We'll cover the technical controls (training data opt-outs, filters on named artists and trademarks) but not the legal analysis.
Now the arithmetic that shapes the design. Take an SDXL-class latent diffusion model (a denoiser of a few billion parameters) at 1024×1024, 30 denoising steps, with classifier-free guidance. On an H100-class GPU, one image takes on the order of 3 seconds. Then:
- 40M images × 3 GPU-seconds = 120M GPU-seconds a day, and 120M ÷ 86,400 ≈ 1,400 GPUs busy around the clock. With a 2.5× evening peak, we'd need about 3,500 GPUs at peak if we did nothing clever.
- At an illustrative $2 per GPU-hour, that's 1,400 × 24 × $2 ≈ $67,000 a day, about $2M a month, and $0.0017 per image.
- Cutting 30 steps to 8 with a distilled model cuts the step cost by almost 4×. Removing the doubled guidance pass halves it again.
Say this early: "GPU-seconds per image is the number that drives everything. The number of denoising steps, the guidance passes and the resolution set it, so model choices are serving choices." It frames the whole interview.