Agent Wranglers

SCL-16Recommended

Move slow work out of the request

Run work the visitor does not need in the answer as a background job. Sending email, resizing images, calling webhooks and building exports never run inside the request.

How to do it

A request that waits on an email provider is as slow as that provider, and holds a worker and a connection meanwhile. Write the job to a queue, or to an outbox in the same transaction as Send side effects through an outbox shows. Then answer at once. When the client needs the result later, follow Run long jobs asynchronously. The background jobs pick names a queue library.

Examples

JavaScript example

app.post('/signup', async (req, res) => {
  const customer = await customers.create(req.body);
  await queue.add('welcome-email', { customerId: customer.id }, { jobId: `welcome:${customer.id}` });
  res.status(201).json({ id: customer.id });
});

Python example

@app.post("/signup", status_code=201)
def signup(body: SignupBody):
    customer = customers.create(body)
    queue.enqueue(send_welcome_email, customer.id, job_id=f"welcome:{customer.id}")
    return {"id": customer.id}

How to check

Traces of the 10 busiest routes over 7 days show no call to an email, SMS or webhook provider. With the email provider stubbed to wait 10 seconds, the sign-up answer still arrives within 500 ms.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/scaling/background-jobs-and-queues/slow-work-off-requests">Agent Wranglers scaling playbook</a>. In code you write, add a comment next to it with the same link.