Tinkerwell - The PHP Scratchpad

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
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
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
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 →
Fair Queue Distribution with Laravel Balanced Queue image

Fair Queue Distribution with Laravel Balanced Queue

Read article
Laravel News Is the Live Stream Partner for Laracon EU 2026 image

Laravel News Is the Live Stream Partner for Laracon EU 2026

Read article
Query Builder Expression Aliases in Laravel 12.48 image

Query Builder Expression Aliases in Laravel 12.48

Read article
Restrict User Actions with Time-Based Sanctions Using Laravel Prohibitions image

Restrict User Actions with Time-Based Sanctions Using Laravel Prohibitions

Read article
Laravel Invite Only Adds a Full User Invitation System with Tokens, Events, and Reminders image

Laravel Invite Only Adds a Full User Invitation System with Tokens, Events, and Reminders

Read article
Docker Support in Laravel VS Code Extension v1.4.2 image

Docker Support in Laravel VS Code Extension v1.4.2

Read article