Back to blog

Photo resizes pile up: what takes the load off your API?

Thắng Đoàn
Thắng Đoàn

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

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.