Get expert guidance in a few days with a Laravel code review

Fair Queue Distribution with Laravel Balanced Queue

Last updated on by

Fair Queue Distribution with Laravel Balanced Queue image

Laravel Balanced Queue, created by YanGusik, manages queue distribution across user groups with per-user concurrency control and multiple partition strategies to prevent queue monopolization.

How It Works

Default Laravel queues use FIFO processing. If User A dispatches 10,000 jobs while User B dispatches 5, User B waits for all of User A's jobs to complete first. Laravel Balanced Queue divides your queue into partitions—typically one per user or tenant—and rotates through them, giving each user fair access to workers.

Getting Started

Add the BalancedDispatchable trait to your job classes:

use Illuminate\Contracts\Queue\ShouldQueue;
use YanGusik\BalancedQueue\Jobs\BalancedDispatchable;
 
class GenerateAIImage implements ShouldQueue
{
use BalancedDispatchable;
 
public function __construct(
public int $userId,
public string $prompt
) {}
 
public function handle(): void
{
// AI generation logic
}
}

Dispatch jobs using the balanced connection:

GenerateAIImage::dispatch($userId, $prompt)->onConnection('balanced');

The package automatically detects partition keys from properties like $userId, $user_id, or $tenantId, grouping each user's jobs into their own partition.

Partition Strategies

The package offers three rotation strategies in config/balanced-queue.php: round-robin (default) cycles through partitions in strict order for equal distribution, random selects probabilistically for high-throughput scenarios, and smart prioritizes smaller queues to prevent starvation when job volumes vary between users.

Concurrency Limiting

This package lets you set the max_concurrent in config/balanced-queue.php to limit how many jobs from a single partition can run simultaneously:

'limiters' => [
'simple' => [
'max_concurrent' => 2,
],
],

This prevents any user from consuming all workers and helps manage API rate limits when jobs make external requests.

Monitoring

Monitor partition status with php artisan balanced-queue:table --watch for live updates showing pending jobs and active workers per partition. The package also includes commands to clear queues and a programmatic metrics API for custom dashboards.

Horizon Integration

The package works with Laravel Horizon. Configure a supervisor in config/horizon.php using the balanced connection, and use php artisan balanced-queue:table for accurate metrics since Horizon's pending counts don't reflect partitioned queue structures.

Learn More

To learn more about Laravel Balanced Queue and view the source code, visit the GitHub repository. The package requires PHP 8.0+ and Laravel 9.0+, with Redis as the queue driver.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Inertia.js v3.0.0 Is Here with Optimistic Updates, useHttp, and More image

Inertia.js v3.0.0 Is Here with Optimistic Updates, useHttp, and More

Read article
Laravel Boost v2.4.0 Adds Security Audits and a Laravel Best Practices Skill image

Laravel Boost v2.4.0 Adds Security Audits and a Laravel Best Practices Skill

Read article
Building Transaction-Safe Multi-Document Operations in Laravel image

Building Transaction-Safe Multi-Document Operations in Laravel

Read article
Ship AI with Laravel: Building Your First Agent with Laravel 13's AI SDK image

Ship AI with Laravel: Building Your First Agent with Laravel 13's AI SDK

Read article
OG Kit: Generate Dynamic Open Graph Images with HTML and CSS image

OG Kit: Generate Dynamic Open Graph Images with HTML and CSS

Read article
Prism Workers AI — A Cloudflare Workers AI Provider for Prism PHP image

Prism Workers AI — A Cloudflare Workers AI Provider for Prism PHP

Read article