Image Processing Microservice - HLD
Difficulty: Intermediate–Advanced · Prerequisites: Message Queues, Object Storage, and Scalability
Asked at: Amazon, Google, Cloudinary, imgix, Shopify, Flipkart
TL;DR
An async image processing service that accepts image transformation jobs (resize, compress, format convert, watermark, thumbnail), executes them through a scalable worker pool, and delivers results via webhook or polling. Presigned URLs handle large uploads without proxying bytes through the API.
💡 A presigned URL is a temporary link that carries its own permission to write one specific location in object storage. The client uploads straight to storage with it, so gigabytes of image never travel through our servers.
flowchart LR
CLIENT["Client<br/>submits job"]
API["Processing API"]
S3[("Object Storage<br/>S3")]
QUEUE["Job Queue<br/>SQS or Kafka"]
WORKERS["Worker Pool<br/>libvips"]
WEBHOOK["Webhook<br/>callback"]
CLIENT -->|"1. presigned URL"| API
CLIENT -->|"2. upload"| S3
CLIENT -->|"3. POST job"| API
API -->|"4. enqueue"| QUEUE
QUEUE -->|"5. pull"| WORKERS
WORKERS -->|"6. read"| S3
WORKERS -->|"7. write"| S3
WORKERS -->|"8. notify"| WEBHOOK
In 3 sentences: Clients upload images to S3 via presigned URLs, then submit a job describing which operations to chain (resize → compress → convert). The API enqueues to SQS/Kafka; stateless workers pull jobs, stream images through libvips, write outputs back to S3, and fire a webhook. Auto-scaling on queue depth keeps latency low without wasting compute.
Understanding the Problem
🖼️ What is an image processing service? Something uploads a photo — a seller listing a product, a user changing an avatar, a CMS ingesting a press shot — and the product needs it in half a dozen shapes: a thumbnail for the grid, a mid-size for the detail page, a WebP for modern browsers, a JPEG for old ones, maybe a watermarked copy. The service takes the original and produces those derivatives.
The work itself is a solved problem; a resize is one library call. What makes this a design question is everything around it. The jobs are CPU-bound and slow enough that you cannot do them in the request. The files are big enough that moving them through your own servers is wasteful. The load is spiky, because uploads follow human behaviour and bulk imports arrive all at once. And the same image gets submitted twice more often than you would guess, so doing the work twice is money burned.
Think Cloudinary, imgix, Shopify’s product images, or the derivative pipeline behind any marketplace listing.
Naive First Cut
A 30-second whiteboard sketch:
flowchart LR
CLIENT["Client"]
API["Upload and Resize API"]
DISK[("Local Disk")]
CLIENT -->|"POST image bytes"| API
API -->|"resize inline then save"| DISK
API -->|"return URLs"| CLIENT
What the sketch shows: one API server. The client POSTs the image bytes, the server resizes them inline while the client waits, writes the outputs to local disk, and returns the URLs in the response.
It works for a demo and breaks on contact with real traffic:
- The request times out. A 2 MB JPEG through a four-step pipeline takes seconds, not milliseconds. At 1000 images/sec every connection is held open for the duration, and the server runs out of sockets long before it runs out of CPU.
- The bytes go through the wrong box twice. Uploading into the API and then writing out again means every gigabyte crosses our servers for no reason — presigned URLs let the client talk to storage directly.
- A spike has nowhere to go. With no queue, a bulk import either drops requests or exhausts memory. There is no backlog to absorb it and nothing to scale on.
- Decoding is the real memory cost, not the file size. A 2 MB JPEG is perhaps 40 MB decoded, and a naive implementation holds all of it per concurrent job. Concurrency is capped by RAM in a way the file sizes do not hint at.
- One corrupt file takes down a worker, and with retries it takes it down repeatedly, forever.
- Local disk means the box is stateful. It cannot be replaced, cannot be scaled horizontally, and loses every derivative when it dies.
- The same image is processed again and again, because nothing recognises that this exact pipeline over this exact source already ran.
The rest of this doc evolves that sketch into an async pipeline that absorbs spikes, keeps bytes out of the API, bounds memory per worker, and never pays for the same transform twice.
Prior Art We’re Drawing From
- Facebook Haystack — the canonical paper on serving images at scale. It reports 260 billion images and over 20 PB, roughly a billion new photos a week, and about one million images served per second at peak; notably Facebook generated four sizes per photo at upload time rather than on demand. Its central finding is that per-photo metadata, not image bytes, was the bottleneck, and that the long tail of access defeats CDN caching — which is why there has to be a real storage tier behind the edge. (OSDI 2010 paper)
- Thumbor — open-source on-demand imaging service: crop, resize and flip computed per request from a URL, with feature and face detection choosing the crop rather than a blind centre cut. The reference point for the on-demand side of the build-now-or-build-later trade-off. (thumbor.org)
- imgproxy — a deliberately minimal resizing server built on libvips, chosen for throughput and low memory footprint, and explicit that caching belongs in a CDN rather than the service. Its v4 notes also document the decoder problem this design has to live with: HEIC and AVIF decoders seek around the source, so a non-seekable input forces libvips to read the whole image into memory before decoding. (imgproxy.net)
- Trendyol — a production writeup of putting imgproxy behind a multi-CDN setup, which is the shape most teams actually end up with: a small transform service that the edge calls and caches in front of. (Trendyol Tech)
- Presigned upload URLs — the standard way to keep large media out of the application path, letting the client write straight to object storage with a short-lived, single-purpose credential.
Content from these sources was rephrased for compliance with licensing restrictions.
Functional Requirements
Core (top 3)
- Submit a processing job — client uploads an image and specifies a pipeline of operations (resize, compress, format convert, watermark, thumbnail generation).
- Execute the pipeline — workers process operations in sequence, streaming the image through each step, and store the result in object storage.
- Retrieve results — client polls job status or receives a webhook callback with the output URL on completion.
💡 A webhook is an HTTP endpoint the client publishes so we can call them the moment the job finishes, rather than them asking us repeatedly. It only works if the client is a reachable server, which is why the polling option stays.
Below the line
- Real-time image transformations via URL path (like imgix/Cloudinary on-the-fly).
- AI-powered operations (background removal, super-resolution, face detection).
- Video processing or animated GIF pipelines.
- Multi-region replication of output assets.
- Batch ZIP download of multiple processed images.
Non-Functional Requirements
Core
- Throughput — sustain 1000 images/sec at steady state, avg 2 MB per image.
- Latency — P95 processing time < 5s for a standard resize+compress pipeline on a 2 MB JPEG.
💡 P95 = the figure 95% of jobs come in under, so it describes the unluckiest 1 in 20 rather than the average. Averages hide the slow tail that users actually complain about. Learn more → - Idempotency — identical jobs (same source + same operations) return cached results, never re-process.
💡 Idempotency = submitting the same work twice has the same effect as submitting it once. Here it is about money rather than correctness: a duplicate submission would cost us a second resize of an identical photo. - Availability — 99.9% uptime; no single component failure causes job loss.
Discussion
Newest first