Skip to content
GenAI Learn/AI Architecture Patterns
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Asynchronous Task Processing for AI

4 min readChapter 4 of 5

You'll learn to

  • -Decide which AI jobs should run in the background instead of in the request
  • -Describe the accept, enqueue, work, report pattern with a job id
  • -Use retries with a limit, an idempotency key and a dead letter list safely
  • -Estimate how many connections a slow synchronous job would hold open

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.

Some AI work is too slow to do while the user stares at a spinner: summarizing a 300 page contract, embedding a whole drive, generating a video, running an eval suite, or an agent that works for ten minutes. Some work is simply not urgent, and a provider will give a discount for waiting. Asynchronous processing means accepting the request now, doing the work later, and telling the user how to get the result.

An Analogy: The Order Rail

A waiter does not stand at the stove until your steak is done. The order goes on a rail, a cook takes it when free, and you get a table number. If a cook drops a pan, the order is made again. If an order is clearly impossible, it goes to the manager instead of being tried forever. The rail is the queue, the cooks are workers, the table number is the job id, and the manager's pile is the dead letter list.

The Pattern

  • -Accept: validate the request, store a job record, and answer immediately with a job id. HTTP calls this 202 Accepted.
  • -Enqueue: put the job on a queue. The queue holds bursts that arrive faster than workers can handle.
  • -Work: workers take jobs, do the slow part, and write the result to a store.
  • -Report: the client polls a status endpoint, receives a webhook, or gets a push when the job is done.

The code has two parts. The first applies Little's law, which says the number of things in the system equals the arrival rate times the time each one spends there. The second is a small queue with the three safety rules every background system needs: a limit on tries, an idempotency key so a duplicate submission is ignored, and a dead letter list for jobs that will never work.

Press Run: how many connections a slow job holds, then a queue with retries

At 20 requests a second and 40 seconds a job, a synchronous design holds 800 connections open just waiting. An asynchronous design holds none. In the queue, the flaky job failed twice and succeeded on the third try, so it ended with a result and 3 attempts. The broken job also used 3 attempts, then went to the dead letter list for a human. The duplicate submission of job a was refused at the door, so a ran once.

Rules That Keep It Safe

  • -Cap retries and wait longer between them, with a little randomness, so a struggling provider is not hit harder. Retries can turn a small outage into a large one.
  • -Make jobs idempotent. A worker can crash after finishing and before reporting, so the job will run again.
  • -If a worker dies mid-job, the job should reappear for another worker after a timeout, often called a visibility timeout.
  • -Watch the age of the oldest job and the queue depth. A queue that only grows means you need more workers or must shed load.

Providers often sell a batch interface at a discount for work that can wait up to a day, as the economics chapter showed. A background queue is the natural way to feed it. The Dead Letter Queues chapter in the HLD course goes deeper on poison messages and redrive.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!