An API accepts uploads, puts a job on a queue and returns 202. During a spike the queue fills and jobs wait over ten minutes. Design the behaviour under overload.
Sketch your own answer first, then compare it with the one below.
A strong answer
Start with the wait budget. If a result is useless after two minutes, the queue should never hold more than two minutes of work. At a drain rate of 50 jobs a second, that is 6,000 jobs, and anything beyond it only creates stale work.
So bound the queue by that number and make admission explicit. When depth or age passes the threshold, the API should refuse new work quickly, with a 429 or 503 and a Retry-After header, instead of accepting jobs that will expire. Rejecting is cheaper than queueing something that will be discarded later, and it tells clients to slow down.
Then protect what is already accepted. Put a deadline on each job and have consumers skip expired ones. Separate queues or priorities keep paying customers or small jobs from waiting behind bulk ones. Retries from clients need backoff with jitter, or they turn the spike into a sustained overload.
On the capacity side, add autoscaling keyed to queue age, with a ceiling so a spike cannot take down the database the workers hit. Finally, record how many jobs were refused or expired, so the capacity discussion is based on numbers.
Sources
- Betsy Beyer, Chris Jones, Jennifer Petoff, Niall Richard Murphy (eds.) (2016). Handling Overload.
- Mor Harchol-Balter (2013). Performance Modeling and Design of Computer Systems.