Unit 11.04: When the work belongs in a real queue
Between best-effort and must-happen there is a queue.
Six kinds of work, four destinations
What belongs in a background task, a queue, a scheduler, or the request itself.
The code sorts them.
WORK = [
("send a best-effort notification", "background task", "loss is acceptable"),
("resize an uploaded image", "queue", "must happen; may be slow"),
("charge a card", "queue", "must happen exactly once"),
("write an audit record", "inline", "must happen before the response"),
("nightly report generation", "scheduler", "not request-driven at all"),
("call a model taking 30 seconds", "queue", "the client should not wait"),
]
print(f"{'work':36} {'belongs in':16} why")
for work, where, why in WORK:
print(f"{work:36} {where:16} {why}")
print("""
A queue buys durability, retries, and isolation from your web workers. It costs
a broker to run, a worker to deploy, and a way to report status back.
The audit row is the exception worth noting: if it must be true that the record
exists before you answer, it cannot be deferred at all.
""")
A queue buys durability, retries and isolation from your web workers. It costs a broker to run, a worker to deploy, and a way to report status back to the caller - which is why it is not the default.
The audit row is the exception worth noticing. If it must be true that the record exists before you answer, it cannot be deferred at all - it belongs inline, in the request.
The mistake this prevents
The mistake is reaching for a queue for everything asynchronous, or for nothing. Both are wrong: cheap best-effort work does not justify a broker, and work that must happen cannot survive without one.
Takeaway
Best-effort work goes in a background task, must-happen work goes in a queue, and must-happen-before-responding stays inline.
