Lesson 2 of 4 FM API Integration Patterns

Processing Generative AI Work Asynchronously with SQS and Batch Inference

This lesson covers queue-based processing, Bedrock batch inference and asynchronous invocation, which decouple callers from model latency and quotas for tasks that do not need an immediate answer.

Learning outcomes

After completing this lesson, you will be able to:

  • Process requests through SQS with controlled concurrency.
  • Run large jobs with Bedrock batch inference.
  • Choose the asynchronous pattern for a workload.

Queue-based processing with SQS

A producer sends each request as a message to an Amazon SQS queue and returns a job ID to the caller immediately. Workers, such as Lambda functions with an SQS event source mapping or containers polling the queue, call the model and store results in S3 or DynamoDB, then notify the caller through SNS, EventBridge, a WebSocket message or a status endpoint. The queue absorbs bursts, and the number of workers controls the request rate to stay within quotas: the event source mapping's maximum concurrency setting or reserved concurrency limits how many functions run at once.

The visibility timeout must exceed processing time so messages are not processed twice, failed messages go to a dead-letter queue after a number of attempts, and workers should be idempotent because SQS standard queues deliver at least once.

Scenario questions

Tuning queue-based processing

Exam skills 2.4.1 · 2.4.3

Practise configuring SQS workers that call a model.

Log in to try this activity Interactive activities are free with an account.

Bedrock batch inference

For large datasets, Bedrock batch inference is simpler and cheaper than a worker fleet. The application writes JSONL records (each with a recordId and modelInput) to S3 and starts a job with CreateModelInvocationJob, naming the model, the input and output locations and a service role. Bedrock processes the records, writes outputs and a manifest to S3, and reports job status, which EventBridge can use to trigger the next step. Supported models are priced at about half the on-demand rate, and batch quotas are separate from on-demand quotas.

aws bedrock create-model-invocation-job \
  --job-name nightly-ticket-classification \
  --model-id amazon.nova-lite-v1:0 \
  --role-arn arn:aws:iam::111122223333:role/BedrockBatchRole \
  --input-data-config '{"s3InputDataConfig": {"s3Uri": "s3://batch-in/tickets/"}}' \
  --output-data-config '{"s3OutputDataConfig": {"s3Uri": "s3://batch-out/tickets/"}}'

Choosing the asynchronous pattern

Queues suit continuous streams of independent requests that must complete within minutes, with custom logic per request. Batch inference suits large sets that can complete within hours at the lowest price. Step Functions with a Distributed Map suits large sets that need multi-step processing per item with controlled concurrency. Bedrock's StartAsyncInvoke runs long jobs for models that support asynchronous invocation, such as video generation with Luma AI Ray2 or embedding large video and audio files with Amazon Nova Multimodal Embeddings, and writes output to S3; GetAsyncInvoke reports the job status. The following table compares the patterns.

PatternLatencyBest for
SQS queue + workersSeconds to minutesContinuous independent requests; custom logic
Bedrock batch inferenceHours (job based)Large datasets at the lowest price
Step Functions Distributed MapMinutes to hoursMulti-step processing per item
StartAsyncInvokeMinutesLong-running jobs such as video generation or video embeddings

The following diagram shows the queue-based design.

Absorbing bursts of contract uploads with a queue. The API returns a job ID at once, an SQS queue holds the burst, and Lambda workers with a maximum concurrency keep tokens per minute under the quota. Results are stored and the user is notified when the analysis is ready.
Maximum concurrency on the event source mapping controls the request rate.
Sort into groups

Queue, batch job or Distributed Map?

Exam skills 2.4.1 · 4.2.3

Choose the asynchronous pattern for each workload.

Log in to try this activity Interactive activities are free with an account.
Worked example

Bursty uploads

Users upload contracts for analysis in bursts of thousands at month end. Synchronous calls hit ThrottlingException and users see errors. Results are needed within an hour.

Moving to an SQS queue with Lambda workers whose maximum concurrency keeps tokens per minute under quota absorbs the bursts; users receive a job ID and a notification when analysis is ready. If the deadline were the next morning, batch inference would be cheaper.

Check yourself

A caller submits work that is processed through SQS. What should the API return immediately?

Show answer

A job ID, which the caller uses to check status or receive the result later, so it does not wait for the model.

Exam Tips

Remember for the exam

  • Decouple callers from model latency with SQS and return a job ID.
  • Control throughput with the event source mapping's maximum concurrency.
  • Large offline datasets: batch inference with JSONL in S3 at a lower price.
  • Use DLQs and idempotent workers for at-least-once delivery.