Back to results for “Resources”
PHP › Performance Optimization & OPcache

How does lazy loading versus eager loading of resources and how minimizing unnecessary work within a request's critical path help improve overall PHP application response times?

Intermediate

Answer

Beyond database queries specifically, general performance optimization involves ensuring expensive operations, such as calling an external API or performing a complex calculation, only happen when their result is genuinely needed rather than unconditionally on every request, deferring non essential work to a background job queue whenever the user does not need to wait for it to complete immediately, and being mindful about loading only the specific classes, configuration, or data actually required for handling a given request rather than unconditionally initializing everything upfront.
// Deferring non essential work to a background queue
Order::create($data);
SendOrderConfirmationEmail::dispatch($order); // queued, not blocking the response
Real-world example An order processing endpoint immediately returns a response to the customer once the order is successfully created, dispatching the confirmation email sending as a background queued job rather than making the customer wait for that email to actually be sent before receiving their confirmation.

Common follow-ups: What criteria help decide whether a specific task should be deferred to a background job?;How does this general principle of minimizing critical path work relate specifically to caching strategies discussed elsewhere?