Design a Text-to-Image Generation Service: GPUs, Queues and Safety

System design for a text-to-image service like Midjourney: diffusion inference on GPUs, job queues and priorities, prompt and image safety, storage and cost.

Advanced 3 min read · Updated

The question: design a text-to-image service like Midjourney: users type a prompt and receive several generated images in seconds to a minute; they can upscale, vary or edit them, and browse a gallery. Unlike text LLMs, each request is a heavy, fixed-cost GPU job rather than a stream of tokens, so the design centres on job queues and scheduling, GPU efficiency, safety on both prompts and images, and storage and delivery of the results.

1. Clarify requirements

QuestionAnswer
Features?Generate a grid of 4 images from a prompt; upscale, vary, edit; personal gallery and public feed
Scale?20M generation jobs per day
Latency?Fast tier: results in ~10-20 s; relaxed tier can wait minutes
Plans?Paid tiers with GPU-time quotas; priority for faster tiers
Safety?Block disallowed prompts and images (sexual content involving minors, real-person abuse, etc.)

2. Estimates

Jobs        : 20M/day ≈ 230/s avg, ~600/s peak
GPU time    : say ~4 GPU-seconds per 4-image grid on a modern GPU with an
              optimised diffusion model (illustrative)
Load        : 600 jobs/s × 4 s = 2,400 GPUs busy at peak; plan ~3,000 with headroom
              → GPU cost is the business; batching and step reduction matter
Storage     : 80M images/day × ~1.5 MB (PNG) ≈ 120 TB/day before thumbnails/WebP
Delivery    : images served via CDN; gallery thumbnails are most of the reads

3. How generation works (briefly)

Diffusion models start from random noise and denoise it over a number of steps, guided by an embedding of the text prompt. Latent diffusion runs those steps in a compressed latent space and decodes to pixels at the end, which makes it far cheaper than working on full-resolution pixels. Cost scales with resolution and number of steps; distilled models that need fewer steps cut GPU time substantially.

4. Architecture

ClientAPIauth · quotaPrompt safetyJob queuesfast · relaxedSchedulerpriority · batchingGPU workersdiffusion modelImage safetyObject storage + CDNNotifyWebSocket / poll
An asynchronous job pipeline: the API checks the prompt and quota, enqueues a job by priority, GPU workers generate and safety-check images, results go to object storage behind a CDN, and the client is notified.

5. Queues and scheduling

  • Generation is asynchronous: the API returns a job id immediately; progress and results arrive over a WebSocket or polling. See message queues.
  • Separate queues per tier; the scheduler gives fast-tier jobs priority and lets relaxed jobs use spare capacity, a form of load shedding.
  • Batching compatible jobs (same model, resolution and step count) on one GPU improves throughput.
  • Quotas in GPU-seconds per user per period, deducted when a job starts and refunded on failure.
  • Idempotent job ids so client retries don't create duplicate jobs; see idempotency.

6. GPU fleet

  • Workers keep models loaded in GPU memory; loading a multi-gigabyte model takes far longer than a generation, so route jobs to workers that already hold the right model (and LoRA styles) warm.
  • Autoscale on queue depth and wait time; GPU capacity takes minutes to add, so forecast daily peaks and keep a buffer.
  • Use cheaper interruptible GPUs for the relaxed queue, with retries on preemption.
  • Speed-ups: fewer sampling steps with distilled models, lower-precision inference, compiled kernels; see model compression.

7. Safety

StageChecks
PromptClassifier and blocklists for disallowed content, real people in sexual or abusive contexts, and known jailbreak phrasing; rewrite or refuse
GenerationSafety-tuned model and negative guidance where supported
Output imageImage classifiers for nudity, violence, minors; hash matching against known abuse material; block before storage
ProvenanceEmbed metadata or watermarks marking images as AI-generated
AccountRate limits, repeat-offender detection, human review of reported images

These are the same per-harm classifier and review-queue ideas as in harmful content detection and LLM guardrails.

  • Store originals in object storage and generate WebP thumbnails; serve through a CDN.
  • Metadata (prompt, parameters, seed, model version, user, visibility) in a database; the seed and parameters make variations reproducible.
  • Lifecycle policies move old, rarely viewed originals to cheaper storage tiers.
  • The public feed is a ranking problem (recency, engagement, quality) built on the metadata; see recommendation systems.

9. Metrics

  • Queue wait and generation time per tier (p50, p95), GPU utilisation, cost per image.
  • Safety block rates and false positives (appeals), reports per thousand images.
  • Quality: upscale and download rates as implicit signals; periodic human preference studies when changing models.

Where it’s used

Creative tools

Prompt-to-image with variations and edits, quotas by plan and priority queues.

Marketing and e-commerce

Product imagery generated in batch on relaxed queues with brand-style adapters.

Video generation

The same queue and GPU-worker design with much heavier jobs and longer waits.

In an interview

  • Make generation asynchronous with job queues, priorities and quotas in GPU-seconds.
  • Estimate GPU fleet size from jobs per second and GPU-seconds per job.
  • Keep models warm and route jobs by model; batch compatible jobs.
  • Check safety on prompts and on images before storage, and add provenance.

Frequently asked questions

How would you design a text-to-image service like Midjourney?

Accept prompts asynchronously, check prompt safety and quota, enqueue jobs by tier, schedule them onto GPU workers that keep models warm and batch compatible jobs, check generated images for safety, store them in object storage behind a CDN, and notify the client.

What drives the cost of image generation?

GPU time per image, which grows with resolution and the number of diffusion steps. Distilled models with fewer steps, batching and lower-precision inference reduce it.

Further reading