
How defer() in Laravel 11 Made Our API Responses 3x Faster Without Touching a Single Queue
TL;DR: Laravel 11 introduced defer() which runs code after the HTTP response is sent to the user. No queues, no job classes, no workers. Just wrap your fire-and-forget logic in defer() and your API becomes instantly faster. I spent two days last year trying to figure out why our order API endpoint was taking 1.2 seconds to respond. The order itself was being created in about 80ms. So where was the rest of the time going? Turns out we were sending a confirmation email, tracking an analytics event, syncing inventory with a third-party service, and clearing a cache key. All of that happened synchronously before the response was sent back to the user. The user did not care about any of those things completing before they saw their order confirmation. They just wanted to know their order went through. The Old Way: Queues for Everything The typical advice is to push these tasks onto a queue. Create a job class, dispatch it, run a queue worker, set up monitoring, handle failed jobs. For a lar
Continue reading on Dev.to Webdev
Opens in a new tab
