PHP & Laravel Sanjay Gupta June 14, 2026 8 views
Laravel Queues for Background AI Processing

AI API calls can take seconds or minutes. Laravel's queue system keeps your application responsive while processing work in the background.

Why Queue AI Work?

  • HTTP timeouts — browsers and load balancers won't wait 60+ seconds
  • Rate limits — spread requests across workers
  • Retries — transient API failures recover automatically

Job Design

Create focused jobs: GenerateEmbeddingJob, SummarizeDocumentJob, ProcessAiImageJob. Pass only IDs, not large payloads — reload models inside the job.

Horizon Dashboard

Laravel Horizon provides real-time monitoring for Redis queues: throughput, failed jobs, and worker scaling. Essential when AI traffic spikes.

Failure Handling

Use failed() hooks to notify admins, log token usage, and mark records as failed in the database so users can retry manually.

Scaling Pattern

// Dispatch after user uploads a document
ProcessDocumentAiJob::dispatch($document->id)->onQueue('ai');

Dedicate an ai queue with fewer concurrent workers to respect provider rate limits while keeping default queues fast.