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.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with 𝙈𝙖𝙧𝙞𝙖𝙣 𝙋𝙤𝙥 🐘

𝙈𝙖𝙧𝙞𝙖𝙣 𝙋𝙤𝙥 🐘 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mvpopuk

Jan 6
🔥 Epic Laravel Tips Thread 🔥 [ Part 1 ] I decided to create weekly threads and share epic @laravelphp tips that will lead to a cleaner and more performant code!

Let's go 🧵 #epiclaravel 👇🏻

1/11
🔥 Epic Laravel 🔥 - Tip No. 1 [ Eloquent Global Query Scopes ] Implementing your own eloquent global query scopes can be a quick and straightforward approach to ensure that all queries for a given model are subject to certain limitations:

2/11
🔥 Epic Laravel 🔥 - Tip No. 2 [ Conditional Relationships ]
Relationships allow for powerful method chaining and querying. However, for consistency reasons consider creating a separate relationship method that includes the desired chaining or querying:
3/11
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(