Gearman
Gearman is a distributed job queue system that allows you to distribute work to multiple machines or processes. It's particularly useful for offloading time-consuming tasks from web requests to background workers.
How It Works
Client Job Server Worker
| | |
|-- Submit job ----------->| |
| |-- Distribute job ------->|
| | |
| |<-- Return result --------|
|<-- Return result --------| |
Key Concepts
- Client: Submits jobs to be processed
- Job Server: Coordinates work between clients and workers
- Worker: Executes jobs and returns results
- Jobs: Can be synchronous (blocking) or asynchronous (fire-and-forget)
Example: PHP Worker
<?php
$worker = new GearmanWorker();
$worker->addServer('127.0.0.1', 4730);
$worker->addFunction('resize_image', function($job) {
$data = json_decode($job->workload(), true);
$imagePath = $data['path'];
// Perform time-consuming image resize
$result = resizeImage($imagePath, $data['width'], $data['height']);
return json_encode(['success' => true, 'path' => $result]);
});
while ($worker->work());
Example: PHP Client
<?php
$client = new GearmanClient();
$client->addServer('127.0.0.1', 4730);
// Synchronous (wait for result)
$result = $client->doNormal('resize_image', json_encode([
'path' => '/images/photo.jpg',
'width' => 800,
'height' => 600,
]));
// Asynchronous (fire and forget)
$client->doBackground('send_email', json_encode([
'to' => 'user@example.com',
'subject' => 'Welcome!',
]));
Common Use Cases
- Image/video processing
- Email sending
- Report generation
- Data import/export
- Search indexing
- Any CPU-intensive task
What We Like
- Language agnostic: Workers can be written in any language
- Simple protocol: Easy to understand and implement
- Reliable: Job persistence options available
- Scalable: Add workers as needed
What We Don't Like
- Dated: Less actively developed than modern alternatives
- Limited features: No built-in retry, scheduling, or priorities
- Monitoring: Limited visibility without additional tools
- Memory management: Job server can consume significant memory
Modern Alternatives
| Alternative | Best For |
|---|---|
| SQS + Lambda | AWS serverless workloads |
| Redis + Bull/BullMQ | Node.js applications |
| Celery | Python applications |
| Sidekiq | Ruby applications |
| RabbitMQ | Complex routing needs |
Gearman remains useful in legacy PHP environments, but for new projects, we typically recommend cloud-native alternatives like SQS or Redis-based queues.