Queue work in Redis for a separate PHP worker to process outside the web request.
The examples focus on the operation itself, including the important distinction: A synchronous function call blocks a request; a queue decouples execution.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Before You Start
Read the input requirements before applying this pattern to production data.
Example 1: Basic Use
A worker can BLPOP from jobs:pending, but for production reliability use acknowledgement, retry tracking and a dead-letter queue.
<?php
$redis = new Redis();
$redis->connect(getenv("REDIS_HOST") ?: "127.0.0.1", 6379);
$task = json_encode(["job" => "thumbnail", "id" => 3], JSON_THROW_ON_ERROR);
$redis->rPush("jobs:pending", $task);
echo "queued";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
A worker can BLPOP from jobs:pending, but for production reliability use acknowledgement, retry tracking and a dead-letter queue.
<?php
$redis = new Redis();
$redis->connect(getenv("REDIS_HOST") ?: "127.0.0.1", 6379);
$task = json_encode(["job" => "thumbnail", "id" => 8], JSON_THROW_ON_ERROR);
$redis->rPush("jobs:pending", $task);
echo "queued";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
A worker can BLPOP from jobs:pending, but for production reliability use acknowledgement, retry tracking and a dead-letter queue.
<?php
$redis = new Redis();
$redis->connect(getenv("REDIS_HOST") ?: "127.0.0.1", 6379);
$task = json_encode(["job" => "thumbnail", "id" => 21], JSON_THROW_ON_ERROR);
$redis->rPush("jobs:pending", $task);
echo "queued";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
A naive BLPOP/RPUSH queue can lose work on worker crashes; production systems need acknowledgments, retries and dead-letter handling.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Implementing Background Jobs with Redis Queues | Queue work in Redis for a separate PHP worker to process outside the web request. |
| Related API or alternative | A synchronous function call blocks a request; a queue decouples execution. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | A naive BLPOP/RPUSH queue can lose work on worker crashes; production systems need acknowledgments, retries and dead-letter handling. |
| Confusing it with a related API | A synchronous function call blocks a request; a queue decouples execution. |
Key Takeaways
- Queue work in Redis for a separate PHP worker to process outside the web request.
- A naive BLPOP/RPUSH queue can lose work on worker crashes; production systems need acknowledgments, retries and dead-letter handling.
- A synchronous function call blocks a request; a queue decouples execution.
Use Implementing Background Jobs with Redis Queues when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.