In this episode, we're diving into Laravel's new concurrency functionality. If you're dealing with multiple slow-running tasks in your app, you'll love what Laravel now lets you do—run things concurrently to save a whole lot of time.
We’ll start by mocking up a couple of long-running processes using some simple sleep
calls to illustrate the problem, showing how (without concurrency) you have to wait for each to finish, which just stacks up the total wait time. Then, we flip the switch and run the same tasks with Laravel’s concurrency features, showing how everything is done at the same time and your code waits only for the slowest task. We work with the Concurrency
facade, toss our closures in an array, and watch as they get handled in the background—all without manual queue setup.
After getting the basics down, we talk about configuring concurrency. We explore the different drivers (like process
, fork
, and sync
), chat about how you might want different behavior in your local, production, or testing environments, and show how you can publish and edit the concurrency config.
Next, we look at a more practical scenario by grabbing a collection of users and deleted users from the database—asking the question: is concurrency always the answer? Spoiler: not really! We compare performance with and without concurrency and show that for short or simple queries, running them concurrently actually makes things slower.
To wrap up, we talk through the real sweet spot for concurrency: grouping up long-running, independent tasks (like generating reports) where the savings are significant. But remember, don’t overuse it; always check if concurrency actually brings a speed boost for your particular situation!
Key takeaway: With Laravel's concurrency, you’ve now got an easy, elegant way to run truly independent, time-consuming tasks side by side—but make sure you use it where it makes sense.