Asynchronous work and scaling costs
🏠The everyday picture
A restaurant takes your order and payment at the counter in a minute, while the slow cooking happens in the kitchen. If every customer had to wait at the counter while their food cooked, the queue would stretch down the street.
📘What it really is
- Keep the request path short: do only what the user must wait for (validate, save, reserve); move everything else to background jobs (Module 94):
- emails and SMS, reminders;
- image resizing for profile photos;
- generating PDFs (invoices, certificates);
- AI calls that aren't needed for the immediate answer (Level 7);
-
analytics and report building.
-
Scale workers separately from the API: more workers when queues grow; different machine sizes for heavy jobs.
- Rate-limit outward calls to providers (SMS, AI, payment) so bursts don't exceed their limits; queues make this easy.
- Scaling and cost go together (Module 68):
- measure cost per unit: per booking, per active user, per thousand requests;
- scale down when traffic drops (nights), and right-size servers to measured use;
- caching and query fixes often save more money than adding servers;
- avoid premature scaling: an architecture built for a million users costs much more to run and change than one built for ten thousand with a clear growth path.
- Capacity planning (Module 96) uses growth forecasts and load-test limits to decide when to scale before users notice.
🖱️Try it
Move image resizing and invoice PDF generation in the booking platform into background jobs, and scale workers separately in Compose. Re-run the load test and compare booking latency. Calculate the cost per thousand bookings for your current set-up, and for a set-up that handles 10 times the traffic, using your provider's prices.
✅Quick check
What belongs in the request path, and what doesn't?
Show the answer
Only what the user must wait for (validation, saving, reservation); slower side work goes to background jobs.
Why measure cost per unit, such as per booking?
Show the answer
It shows whether growth is affordable and which changes actually save money.
