Queue in laravel with redis

Queue in laravel with redis

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your Laravel Queue Isn't Actually Asynchronous

You are experiencing a common misconception about how job dispatching works in Laravel. You've correctly set up Redis as your driver, implemented the ShouldQueue interface, and dispatched the job via GetFromDocumentsAPI::dispatch(). However, your application is still timing out because the HTTP request handling the API calls has not actually completed yet when you are trying to render the response.

The core issue lies in the flow of execution within your handle() method. When you call Issue::Create() inside a loop that processes 650 items, you are performing database writes synchronously. The job is indeed queued, but the handle() method itself is blocking until every single record is inserted into the database.

Until handle() returns to the dispatcher, the request lifecycle remains active. Laravel waits for the job to finish before it can send the HTTP response back to your browser. This means that even though you are using a queue, the current process must wait for all 650 API calls and subsequent database inserts to complete before the page loads. The result is exactly what you described: a timeout error identical to running the code without the queue at all.

To achieve true asynchronous behavior where the page loads immediately with an initial state while processing occurs in the background, you need to decouple the job execution from the response rendering. This usually involves shifting to a "fire and forget" pattern for heavy batch operations or using Laravel's built-in mechanisms to acknowledge completion without waiting for it.

The specific bottleneck in your handle() method is the combination of fetching comments, parsing IDs, and then iterating through them to call the external API and save data. The line $url = file_get_contents(...) performs a synchronous HTTP request. Doing this 650 times inside a single job execution blocks the worker thread for a significant duration.

If your goal is to start the process and immediately return control to the user, consider changing the controller logic. Instead of dispatching the job and expecting it to finish instantly (which it won't), you might want to save an initial state indicating that the import has started. Alternatively, if you must wait for completion but need better performance, you should focus on optimizing the job itself rather than relying on the queue to magically bypass the blocking operations inside handle().

One optimization path is to batch your database inserts. Currently, you are calling Issue::Create() inside a loop with one insert at a time. This prevents Laravel's connection pooling from being utilized effectively and increases network overhead. Switching to Issue::insert() or Issue::insertOrIgnore() allows you to pass an array of data in a single query execution.

Here is how you can refactor the insertion logic within your job:

$batchData = [];

foreach ($commentIDs as $key => $commentID) {
    foreach ($commentID as $item) {
        // ... existing logic to fetch issue details ...
        
        if ($issue === null) {
            try {
                $url = file_get_contents('https://documents.calibrate.be/issues/' . $item . '.json?key=' . $apiKey);
                $json = json_decode($url, true);
                
                // Accumulate data for batch insert instead of immediate save
                $batchData[] = [
                    'id' => $item, 
                    'name' => $json['issue']['subject'], 
                    'projectId' => $json['issue']['project']['id'], 
                    'priority' => $json['issue']['priority']['id']
                ];
            } catch (Exception $e) {
                // Log error or handle gracefully without breaking the loop
            }
        }
    }

    // Perform batch insert periodically or at the end of the job
    if (!empty($batchData)) {
        Issue::insertOrIgnore($batchData);
        // Clear memory to prevent issues with large datasets
        $batchData = []; 
    }
}

Additionally, consider using the delay() method in your controller as shown in your snippet. This allows you to push the heavy lifting off immediately but schedule it for later, preventing potential spikes in queue processing time if the system is under load. However, this still does not solve the immediate timeout issue because the job itself remains synchronous until completion.

For truly non-blocking behavior where the user needs to see a result instantly, you might need to move away from Issue::Create inside the loop entirely and instead rely on event-driven architecture or separate background processes that update the UI once the job is marked as successful in Redis. If you require real-time updates during the process, consider using Laravel Reverb or Pusher alongside your queues.

Remember that queues are designed to handle concurrency and prevent overload, not to bypass synchronous blocking code within a single job worker. The worker thread must finish handle() before the request is considered done.

Our network