Photo resizes pile up: what takes the load off your API?
A FotoFlow upload does real work after the bytes land: six image sizes, a metadata scan, a virus check, three store updates. Done inside the request, that is 30 seconds of spinner on a good day. At 200,000 users one busy Monday morning filled every API thread with resizes, and the site that only shows photos stopped entirely.
Do the work later, confirm now
The fix is to stop doing slow work inside the request. If work takes minutes, do not make the user's request carry it. The API writes the job onto a queue and answers in milliseconds: "uploaded, processing." A worker picks up the job, does the 30 seconds of work, and the phone polls or gets a push when its photo is ready.
This is why almost every image, video, email and report system you use has a queue behind it.
Spikes become backlogs
A celebrity posts, and uploads jump 10x for an hour. Without a queue, that spike is 10x the servers for 60 minutes of the day. With one, the spike becomes a backlog: a queue turns a spike into a backlog, and a backlog is a choice you can see and manage.
The depth of the queue is a dial. Add workers and it drains faster; watch its age, not just its length, because a 5-minute-old job is late even when the queue looks short. This is load levelling: buy capacity for the average, not the peak.
Workers scale on queue length
One queue, many workers: each takes the next job, so ten workers drain ten times faster. Autoscaling watches queue length and adds workers at breakfast, removes them at night. Competing consumers also heal: a worker dies mid-job, and after a visibility timeout the queue hands the job to another.
That timeout is also the honest truth about delivery: delivery is at-least-once, so the worker, not the queue, must make repeats harmless. A resize run twice writes the same thumbnail twice; a "send welcome email" job run twice needs a check, not hope.
What this does not solve
A poison job that crashes every worker would loop forever if the queue kept redelivering it. After three failures it goes to a dead letter queue, and you alert on that queue's size, not its silence.
Order is the other cost. Two jobs for one account, taken by two workers, finish in either order: "create account" then "upload photo" can arrive reversed. Route events for the same key to one partition with one worker when order matters. A single global order for everything means one worker, no parallelism, the exact bottleneck you left.
And a queue only buys time. If the average arrival rate is above what your workers can ever drain, the backlog grows forever: the real fixes are more workers, cheaper work, or saying "try later."
Your longest request takes 40 seconds of CPU. What is the user actually waiting to see during it?
If your queue is growing faster than your workers drain it all week, what is the queue hiding?
Resizes are off the request path. Next in the series: One upload, five features watching: who tells them all?
Sources
RabbitMQ
Apache Kafka
Google SRE Book
Recommended for you
- Craft & Skills
One server, 10 million users: how does a photo app survive that?
A 40-post series that follows FotoFlow, a photo app, from one server and 100 users to 10 million users with direct messages. One problem, one fix, one honest cost at a time.
- Craft & Skills
Ten million users want direct messages: could you design the chat system?
The final design: 20 million open sockets and a promise that every message arrives, in order, exactly once. Dumb gateways, a connection registry, store-first durability and sync by cursor.
- Craft & Skills
Five million home feeds: how do you build the page everyone opens?
The home feed must load in 200 ms from two hundred followed accounts. Fan-out on read melts at 4.6 million lookups a second; fan-out on write melts on celebrities; the hybrid survives both.
- Craft & Skills
FotoFlow wants short share links: could you design one today?
The first full design walkthrough: turn a one-line brief into numbers, the simplest system, then fix codes, caching, storage and abuse one bottleneck at a time. The method is the point.
Enjoyed this article?
Subscribe for new articles. No spam. Unsubscribe anytime.
Typed live, never pasted.