Marian Pop ⚡ Laravel Magazine Profile picture
🧙🏻‍♂️ Full-stack (Laravel, Vue, React) 🥷🏻 Creator of https://t.co/28Hx4ltzAl 🛰️ Sharing Laravel insights & 🎙 Podcast at @LaravelMagazine 👤 Pronouns: vi/vim

Jan 19, 2022, 19 tweets

Laravel makes it simple to create background-processing queued jobs. Your application can reply to web requests with lightning speed and give your clients a better user experience by transferring time-consuming operations to a queue.

Laravel Queues [ Pocket Guide ] 🧵 👇🏻

1️⃣ Dispatching and running jobs
You can create a new job running the following artisan command:

2️⃣ Queues to database
In order for the queues to use your database, you have to configure the queue connection in queue.php to point to your database. Once that is configured, you have to create two tables in your database: jobs and failed_jobs:

3️⃣ Configuring jobs
ℹ️ A worker can process a single job at a time. To process jobs faster, you need to start more workers. The number of workers you can start, depends on the server resources as well as how many services or processes are running on the server at the same time.

4️⃣ Prioritizing and Defining Custom Queues
When dispatching jobs from a custom / higher priority queue you can use ->onQueue(‘queue-name’);. When starting the worker, you can specify to dispatch jobs from ‘queue-name’ first and then carry on with the default queue:

5️⃣ Handling Attempts and Failures

6️⃣ Chains
A chain is a queued workflow (a group of jobs) that run one after another. If we implement a deployment workflow, we dispatch 3 jobs that are processed one after another: PullRepo, RunTests, Deploy.

7️⃣ Batches
A batch is a group of jobs that can run in parallel.

8️⃣ catch()
If any of the jobs within a batch or a chain fail, we can catch the exception by running code inside of a closure using ->catch()

9️⃣ Chains within batches
We can dispatch a chain within a batch. An array inside of the $batch array represents a chain. In this case, when the batch will be dispatched, both chains will be executed in parallel.

1️⃣0️⃣ Dispatch a batch inside a chain
We can dispatch a batch inside a chain by putting the batch inside of a closure:

1️⃣1️⃣ Race Condition
When two or more workers are trying to process the same job at the same time. To avoid this behaviour we can lock the job:

1️⃣2️⃣ Redis concurency limiter

1️⃣3️⃣ Redis throttle
Control the amount of locks that can be aquired with the given key durring a given period of time:

1️⃣4️⃣ Without overlapping middleware

1️⃣5️⃣ Should be unique interface -
Laravel uses the job class name as the key for checking if a job is already in process by a worker. To overwrite the default key, we can create a function called uniqueId().

1️⃣6️⃣ ThrottlesExceptions Middleware
This will prevent the job to be put back in the queue if it fails 10 times. This might be useful when working with 3rd party API’s.

1️⃣7️⃣ BONUS: afterCommit()
Dispatching the job only after the logic of your code finishes the execution. If something went wrong in your code logic, the job will not be dispatched. We can enforce this behaviour globally by setting the “after_commit” to “true” in queue.php

1️⃣8️⃣ BONUS: ShouldBeEncrypted interface
Will hash the object passed in the __construct() method so will not be visible as a string inside the payload field in your database.

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling