Add Redis storage for job batch results and preserve counts across delivery failures
Why change batching?
Laravel’s database batch driver locks the same row for every job that finishes, even when Redis delivers the jobs. Each success also decodes and rewrites the list of failed IDs. With many workers processing one batch, that row becomes a bottleneck. Laravel 13.x implementation · Original performance report
Hypervel already moved individual outcomes out of that row and fixed duplicate success counting. Its SQL driver still updates the shared row on each completion. This PR adds Redis results for that workload and fixes several ways dispatch could leave a batch inconsistent. Previous batching PR
- External jobs now wait until the transaction adding them to the batch commits. A rollback also prevents
dispatchAfterResponse()from publishing jobs for a batch that no longer exists. - If a later send fails, jobs already accepted keep their batch. Hypervel withdraws only jobs known to be unsent. A lost acknowledgement leaves the job counted so the batch cannot finish early.
- If an addition reopens a finished batch and the queue rejects those jobs, Hypervel restores the original finish time and runs only newly earned callbacks. A
finallycallback on a sync queue can also delete its batch without makingdispatch()throw. - If a queue confirms it stored a batched job and a listener then throws, failover propagates the error and stops. When an acknowledgement is lost, failover copies with different queue IDs still count as one batch job. Failover also propagates a SQL error when the caller’s transaction can no longer commit, including an aborted PostgreSQL transaction or a MySQL or MariaDB deadlock.
- A job timeout now unwinds nested transactions on the batch database before recording failure, including when failed jobs use another store.
Redis batch results
Set QUEUE_BATCHING_DRIVER=redis to enable Redis results for new batches. SQL owns the batch, its callbacks and the total number of jobs added. Redis records individual outcomes and running counts. An ordinary completion records its result in one Redis call. The first new result after jobs are added, and a result that could finish the batch, recheck the committed total under a short SQL lock. That keeps a concurrent addition from making the batch finish early.
The queue connection and results driver are independent. Jobs staged in the SQL outbox commit together with their batch enrollment, and workers can still record completions in Redis. Each batch retains its original driver when the default changes.
Performance
Median completions per second across five runs with 50 workers and 1,000 successes. Both drivers are Hypervel implementations. The benchmark times batch lookup and accounting, including the SQL check that confirms a new Redis batch’s size. Queue transport and job execution are outside the timer. The failure-heavy and progress-callback workloads start with an already confirmed batch size. Redis flushed its append-only file on every write.
| SQL engine | One shared batch, SQL | One shared batch, Redis | Speedup | 1,000 one-job batches, SQL | 1,000 one-job batches, Redis |
|---|---|---|---|---|---|
| PostgreSQL | 352 | 1,711 | 4.9× | 1,686 | 1,040 |
| MySQL | 84 | 2,278 | 27.1× | 787 | 591 |
| MariaDB | 928 | 2,179 | 2.3× | 1,289 | 856 |
The shared Redis batch needed one SQL write for 1,000 completions, to mark it finished. The default SQL driver keeps its ordinary per-job statement count and saves one lookup at batch creation. Both drivers release the enrollment lock before external sends.
SQL was faster when each job had its own batch, because each completion also had to finish a batch. In a separate PostgreSQL control, adding 20 jobs to an existing batch took 3.90 ms with SQL and 10.40 ms with Redis when the addition invalidated a cached total. The full results include ranges, failure-heavy batches, progress callbacks and reproduction instructions. Benchmark report · Raw measurements
Compatibility and operations
Bus::batch(), callbacks and Horizon keep their application APIs. Horizon search replaces up to 50 individual batch lookups with one grouped lookup after finding the IDs, and accepts0as a search term. Horizon and Telescope load batch progress before displaying it.- On a Redis-backed batch, the first read of a progress property loads the counts and finish time together. Custom
BatchFactory::make()andBatchconstructor overrides must accept nullable pending and failed counts. A positiveincrementTotalJobs()on a missing batch throwsBatchNotFoundException. - The migrations add the results driver and four
previous_*columns tojob_batchesso rejected additions can restore completion state. The newjob_batch_deletionstable tracks pending external cleanup. The existingqueue:prune-batchescommand retries interrupted deletions. - Select the Redis connection with
REDIS_BATCHING_CONNECTION. Use persistent Redis storage with eviction disabled. Inline sync queues and failover routes that can execute inline use SQL results. Redis results can be recorded once the batch database transaction has ended. - Inside a transaction on the batch database, omit
beforeCommit()for jobs that must wait. If a separate jobs database has its own open transaction, dispatch direct database jobs after it commits. An external send can raise an error from the caller’s commit after SQL has committed. Use the transactional outbox when publication must survive a process crash after commit.
[!NOTE]
Add Redis storage for job batch results and preserve counts across delivery failures
- Batch records stay in SQL, but job results (successes and failures) can now be stored in Redis, configured via
queue.batching.driver and queue.batching.redis_connection. A new RoutingBatchRepository picks the right repository from each batch's stored driver column.
RedisBatchRepository uses Lua scripts (SNAPSHOT, RECORD) to record job results and read progress counts; batch counts load lazily because Batch now accepts null pending/failed counts.
- Batch enrollment and delivery were reworked across all queue drivers (database, Redis, SQS, Beanstalkd, failover, sync, pool) to classify each job as accepted, uncertain, or rejected via
Queue.bulkForBatch. Rejected jobs are withdrawn from the batch so pending counts stay correct when delivery fails.
- New
job_batch_deletions table and driver column in job_batches, generated by BatchesTableCommand from the new stubs; PendingBatch cleanup uses deleteIfEmpty so batches with enrolled jobs are not deleted.
- Docs updated in queues.md and queue-outbox.md; benchmark harness added under
tests/Benchmarks/JobBatches/RedisResults/.
- Risk: behavioral changes — positive enrollment on a missing batch now throws
BatchNotFoundException (DatabaseBatchRepository.updateEnrollment); Job.fail with a DatabaseBatchRepository rolls the connection back to transaction level zero; BatchDispatched is skipped when progress cannot load (PendingBatch.dispatch); batched jobs sent to external queues now wait for the enrollment transaction to commit, so beforeCommit() on such jobs inside a transaction on the batch database throws LogicException.
Macroscope summarized 2129f5c.