Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uniqueginun's avatar

timeout issue when running queued jobs in production

I am working on a project and I recently added a feature that a user can click on a link then a 3K database with lots of relations and calculations are made and then export that into excel file and email it to the user. I decided to use queued jobs for that task. but I am facing issue that when I send the job to the queue worker it fails and the error is:

Fatal Error: Allowed Memory Size of Bytes Exhausted

when I modify the query everything is working fine.

$results = Result::query()->take(20)-> // the rest of the work

I tried this but same error was happening

php artisan queue:work --memory=120 --queue=excel-export

how to resolve this issue??

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like you're running into a memory limit issue when processing a large amount of data for your Excel export. The --memory=120 option you're using with php artisan queue:work sets the memory limit to 120MB, which might still not be enough for the amount of data you're trying to process.

Here are a few strategies you can try to resolve this issue:

  1. Increase the memory limit: You can increase the memory limit for your queue worker by passing a higher value to the --memory option. However, be cautious with this approach as setting the memory limit too high can cause issues with your server if you don't have enough available RAM.
php artisan queue:work --memory=256 --queue=excel-export
  1. Optimize your query: If you're loading a lot of data into memory, consider optimizing your query to reduce the memory footprint. This could involve using eager loading for relationships (with()) if you're not already doing so, or using chunk() to process the data in smaller sets.
Result::query()
    ->with('relatedTable') // Eager load relationships
    ->chunk(100, function ($results) {
        foreach ($results as $result) {
            // Process each result
        }
    });
  1. Increase PHP memory limit: You can also increase the memory limit in your PHP configuration (php.ini). This is a more global setting that will affect all PHP processes.
memory_limit = 256M
  1. Use a generator: If you're using Laravel's Eloquent ORM to fetch the data, consider using lazy() which uses a PHP generator to process large datasets in a memory-efficient manner.
$results = Result::query()->lazy()->each(function ($result) {
    // Process each result
});
  1. Optimize your job: If your job is doing a lot of heavy lifting, try breaking it down into smaller jobs if possible. This way, each job will consume less memory.

  2. Configure your environment: Ensure that your production environment has enough RAM to handle the memory requirements of your jobs. If you're on a shared host or a server with limited resources, you might need to upgrade your hosting plan.

  3. Monitor and log: Implement monitoring and logging to get more insight into memory usage. This can help you identify the exact point at which the memory limit is reached and adjust your strategy accordingly.

Remember to test these changes in a staging environment before deploying to production to ensure that they resolve the issue without introducing new ones.

Braunson's avatar

A solution really depends on your code. Obvious it's running out of memory. Try optimizing your query/logic. Leverage chunking, batching and lazy loading. Check your PHP memory limit is more or the same as the one you've specified in your queue worker CLI line.

Please or to participate in this conversation.