When a client fires 50,000 orders at your webhook in ten minutes
A queue adds no speed, but competing consumers and back-pressure let you set the pace at which the work gets done.
In brief
- Put a queue between the client's system and your workers to absorb bursts, then have multiple workers compete for the messages.
- Back-pressure belongs on the consumer side: cap unacknowledged messages, match timeouts to real processing times and scale on queue depth.
- Order is not guaranteed and messages can arrive twice, so consumers must be idempotent and backed by a DLQ.
Fifty thousand orders in ten minutes: say that is what a client’s ERP system fires at your webhook when it runs its end-of-day sync. Each order needs a model call, a database write and a push of the result to the CRM. Your API starts timing out, the client’s side retries automatically, and the next morning you are cleaning up orders that were written twice.
Anyone who integrates with client systems should prepare for this kind of situation. You do not control the client’s system and cannot tell it to slow down. You do control how your own system takes in work, and that skill rests on three ideas: pub/sub, competing consumers and back-pressure.
A queue lets you choose your speed
The mistake in the example is a webhook that receives and processes in the same step. The fix is to split the two: the webhook only writes the message to a queue and returns 200 immediately, while a group of workers pulls messages out and processes them at its own pace.
Microsoft’s architecture documentation describes this role: the queue is a buffer between the sender and the processing instances, smoothing out traffic as it rises and falls.
When several workers read from the same queue, you have the competing consumers pattern: the workers compete, and each message goes to only one of them. With pub/sub, every subscriber receives every message. The core difference is who receives each message, and it determines how you design the topology.
In practice you often need both. A “new order” event is published once; the “risk scoring” subscriber and the “CRM sync” subscriber each receive their own copy. Inside each subscriber is a group of competing workers. There is a further benefit: a failed subscriber does not bring down the publisher or the other subscribers, and the broker holds its messages until it recovers.
Where does back-pressure live?
A queue absorbs the burst, but if greedy workers pull too many messages at once, the pressure simply moves from the webhook to the workers’ RAM.
Microsoft’s pub/sub guidance sets a clear order of priority: first use the broker’s flow control to limit the number of unacknowledged messages per subscriber, and scale out with competing consumers only when flow control is not enough.
Each broker puts this control in a different place, under a different name:
| Broker | Where to set back-pressure | What to remember |
|---|---|---|
| RabbitMQ | prefetch (basic.qos) |
A value of 0 means no limit on unacknowledged messages |
| Celery | worker_prefetch_multiplier |
For long-running tasks, set it to 1 so workers do not hoard work |
| Amazon SQS | visibility timeout | Defaults to 30 seconds; standard queues have a ceiling of about 120,000 in-flight messages |
| Kafka | pull model + consumer group | Plan the partition count from the start: each partition belongs to only one consumer in the group, so the partition count caps parallelism |
Kafka deserves separate mention. Because consumers pull the data themselves, Kafka’s design documentation points out that a slow consumer simply falls behind and catches up when it can. Back-pressure is almost built in. The trade-off is that if a topic has 6 partitions, the seventh consumer in the same group sits idle, so work out the parallelism you need when you design the topic.
A worked example from start to finish
Return to the 50,000 orders. Assume each takes 2 seconds on average, and 45 seconds at worst when the model is slow to respond. The total workload is 100,000 worker-seconds; with 20 workers, the queue drains in about 5,000 seconds, or more than 83 minutes. The first question for the client: do the results need to be ready in less than 83 minutes? If so, you can calculate at once how many workers you need.
Now for the trap. If you use SQS with its default visibility timeout of 30 seconds, any order that takes 45 seconds will reappear in the queue before its worker can delete it, and another worker will pick it up.
The mechanism was designed to rescue work from crashed workers, but here it creates duplicates. Set the timeout above the slowest processing time you have actually measured.
Even with the right timeout, AWS states clearly that SQS delivers at least once, so there is no absolute guarantee that a message will not arrive twice. Microsoft also notes that the order in which consumers receive messages does not necessarily reflect the order in which they were created. Consumers must therefore be idempotent:
def handle(msg):
key = f"{msg['order_id']}:{msg['version']}"
with db.transaction():
if db.exists("processed_keys", key):
return # duplicate, skip
if db.current_version(msg["order_id"]) > msg["version"]:
return # stale version arrived late, skip
upsert_order(msg)
db.insert("processed_keys", key)
The two conditions solve different problems. The idempotency key blocks duplicate messages; the version comparison blocks an old message that arrives after a newer one. If the client’s system does not send a version, ask during discovery which field always increases, such as updated_at.
The last step is handling poison messages: an order with corrupt data will fail forever. Do not let it circulate endlessly; after a set number of deliveries, move it to a dead-letter queue for someone to review later. You can autoscale the worker pool on queue depth, even down to 0 when the queue is empty, while capping concurrency so you do not choke the database behind it.
What order should you work in?
Measure first: the client’s peak traffic, average and slowest processing times, and the business deadline. Then separate receiving from processing with a queue, and decide where you need pub/sub (several systems care about the same event) and where competing consumers alone will do.
Next, set the controls: a small prefetch, a visibility timeout above p99, and a partition count calculated in advance for your target parallelism. Finally, write idempotent consumers, configure the DLQ and set alerts on queue depth and the number of in-flight messages.
The most common mistakes
The most common mistake is leaving prefetch at 0 in the belief that more is faster, only for one worker to hoard thousands of messages while others sit idle. Next is keeping the default visibility timeout for long-running model calls. Another is adding Kafka consumers beyond the partition count, then wondering why throughput does not rise.
The remaining two are more dangerous because they are silent: assuming messages arrive in the right order, and having no DLQ, so that one corrupt record blocks the whole queue.
The chain of decisions in the 50,000-order example is what to bring to an FDE interview: peak traffic, the worker calculation, why the timeout must exceed the slowest processing time, the idempotency key and where the DLQ goes. Walking through it once with your own real numbers is far more persuasive than a line on your résumé that says “event-driven experience.”
The next time a client asks, “How many requests per second can your system handle?”, skip the single number. The best answer is that your system can accept any volume and will process it at the rate it can sustain.
Was this article useful?
Thanks for the feedback!