Laravel 504 gateway timeout

Laravel 504 gateway timeout

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

Stefan Bogdanescu

Founder & Senior Architect

Resolving Laravel 504 Gateway Timeouts in Long-Running Batch Jobs

When a script processes thousands of records and hits a 504 Gateway Timeout, the immediate instinct is often to adjust server-level settings like max_execution_time or memory limits in php.ini. While those configurations matter, they address the symptom rather than the root cause: your application is attempting to perform too much work within a single HTTP request lifecycle.

In the scenario described, an endpoint /updateAllKeywords fetches prices for thousands of items from an external eBay API. The logic uses Keywords::chunk(100) to iterate through records, but inside that closure, it executes heavy database operations and network calls without yielding control back to the server. This accumulation of processing time causes the web server (nginx) or PHP-FPM to terminate the request before completion, resulting in a timeout.

The Problem with Synchronous Blocking

The provided code attempts to handle concurrency using chunk(), which is excellent for preventing memory exhaustion. However, the internal loop creates a blocking bottleneck:

Keywords::chunk(100, function ($keywordCollection) use($request,$keywordCollection,$defaultStartDate,$ignoredItems,$response) {
    foreach ($keywordCollection->toArray() as $keyword_result) {
        // Heavy API call and database insert happens here synchronously
        $response = $this->apirequest(...); 
    }
});

Every iteration of the foreach loop forces PHP to wait for the external API response before proceeding to the next record. As the total processing time exceeds the timeout threshold (often 30–60 seconds depending on configuration), the connection drops. The error "405 nginx server not allowed" seen in the logs often indicates that the upstream PHP process was killed or crashed, leaving nginx with an invalid state or a misrouted request, but the core issue remains the excessive execution time.

Refactoring for Asynchronous Processing

To handle thousands of records without triggering timeouts, you must decouple the long-running task from the HTTP request-response cycle. The most robust pattern in Laravel is using the queue system. Instead of calling an external API and updating the database inside a web request, push each batch to a queue job.

Here is how you would refactor the entry point:

// In your Controller or Route
public function updateAllKeywords(Request $request)
{
    $keywords = Keywords::all();
    
    // Push the entire collection to a 'price_update' queue
    foreach ($keywords as $keyword) {
        PriceUpdateJob::dispatch($keyword->id, $request->user());
    }
}

The PriceUpdateJob would then contain the logic for fetching prices and updating the database. This allows you to configure the worker process (php artisan queue:work --max-time=3600) to run indefinitely, processing thousands of items without ever hitting a web server timeout.

Optimizing Database and Memory Usage

Even with queuing, memory can still become an issue if you are not careful with how data is handled within the chunk or job closure. In the original code, $response is overwritten in every iteration of the loop inside chunk(). While this doesn't strictly cause a timeout, it creates unnecessary variable churn. More critically, the Items::insert() call accumulates all rows for the batch in memory before flushing to the database. For very large batches, this can spike RAM usage and trigger fatal errors unrelated to timeouts.

To mitigate this, ensure you are not accumulating massive arrays in memory:

// Instead of building a huge array first
$insertItems = []; 
foreach ($results as $item) {
    // Process item...
    // Add to collection
}
// Then insert
Items::insert($insertItems);

// Better approach for large batches: flush periodically or use upserts in smaller chunks if necessary

Handling External API Latency

The specific error of "1-500 records works fine, but 500+ fails" strongly suggests that the cumulative latency of the external eBay API is the trigger. Network calls are non-deterministic; some requests take longer than others. If you have a batch size of 500 and each request takes an average of 1 second with high variance, the total time could easily exceed 60 seconds.

Reducing the chunk size (e.g.,

Our network