Back to blog

Optimizing Large Data Exports in Laravel: How to Prevent Memory Exhaustion

Learn how to export large datasets in Laravel without memory limits and timeouts using batch queries, custom chunk sizes, and queued exports.

Oct 17, 2026
7
directions
notes from delivery practice
3
levels
strategy · process · tools
1
library
guides · checklists · cases
See cases

Why Large Report Generation Breaks Web Applications

As enterprise web applications, CRM systems, and e-commerce platforms scale, their operational databases expand dramatically. A frequent challenge arises when an Excel export feature that operated seamlessly in staging fails in production, triggering an Allowed memory size exhausted fatal error or timing out in the client browser.

The root cause is almost always how records are retrieved. Developers frequently query the database using Model::all() or Model::get() within the export logic. This instantiates thousands of heavy Eloquent model objects directly into RAM before the spreadsheet builder even starts assembling the workbook, quickly exceeding the server's memory budget.

Switching from FromCollection to FromQuery

The standard architectural solution when working with Laravel Excel is adopting the FromQuery concern instead of FromCollection. The fundamental distinction lies in passing an unexecuted query builder rather than an in-memory collection.

  • Deferred execution: The package orchestrates database queries under the hood, retrieving rows in batches instead of keeping the whole table in RAM.
  • Lean data hydration: Applying select() limits data extraction strictly to the columns needed for the spreadsheet, skipping unnecessary attributes.
  • Deterministic sorting: Incorporating orderBy('id') ensures consistent pagination across chunks and avoids row duplication while records are being queried.

Fine-Tuning Batches with WithCustomChunkSize

Default chunk parameters are rarely universal across diverse business domains. By implementing the WithCustomChunkSize contract, engineering teams can tailor the batch processing volume via the chunkSize() method.

Setting this value requires balancing database round-trips against server RAM availability:

  • Smaller batches (200–500 rows): Ideal when individual records include long text or when operating within stringent memory constraints.
  • Larger batches (1,000–5,000 rows): Minimizes overall query volume and reduces round-trip overhead, provided the PHP runtime has adequate headroom.

Manually wrapping queries inside custom Model::chunk() callbacks inside an export class is counterproductive; the framework's native contracts are specifically engineered to maintain stream integrity across workbook sheets automatically.

Mitigating HTTP Timeouts via Queued Exports

Optimizing database memory handles only one half of the performance equation. Building a massive workbook containing tens of thousands of rows still takes CPU time. If an export exceeds the web server or PHP-FPM execution timeout, the user is left with a 504 Gateway Timeout error.

For enterprise-grade reliability, large file creation must be offloaded to asynchronous background workers using Laravel Queues. By executing exports outside the synchronous request cycle, the application UI remains responsive. Once the worker finishes building the spreadsheet, the file is saved to storage, and the customer receives an in-app notification or an email with a secure download link.

ViAcore Engineering Best Practices

When developing scalable reporting modules for high-load platforms, our team adheres to the following principles:

  • Avoid treating memory_limit increases as a architectural solution—re-architect the query pipeline instead.
  • Leverage WithMapping to transform raw model data into spreadsheet cells explicitly without unnecessary entity relationships.
  • Monitor queue workers with tools like Laravel Horizon to track throughput, memory usage, and potential job failures in real time.

Services on this topic

Need this for your project?

Development turnkey: analysis → implementation → launch → support. Tell us about your task — we will reply with an estimate.

About the direction

Relevant articles