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
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
PhpStorm logo

PhpStorm

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

PhpStorm
Shift logo

Shift

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

Shift
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
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
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Tinkerwell logo

Tinkerwell

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

Tinkerwell
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
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

The latest

View all →
Bulk Job Dispatching with Bus::bulk() in Laravel 13.13 image

Bulk Job Dispatching with Bus::bulk() in Laravel 13.13

Read article
Audit Laravel Apps for Security Issues with Checkpoint image

Audit Laravel Apps for Security Issues with Checkpoint

Read article
In-Memory Eloquent Models with Truffle image

In-Memory Eloquent Models with Truffle

Read article
Detect and Resolve Laravel Schema Drift with MigrAlign image

Detect and Resolve Laravel Schema Drift with MigrAlign

Read article
Laravel Cloud Adds Scale-to-Zero and Spending Limits image

Laravel Cloud Adds Scale-to-Zero and Spending Limits

Read article
Shift + AI = Fully Automated Laravel Upgrades image

Shift + AI = Fully Automated Laravel Upgrades

Read article