Laravel Safe Dispatcher: Resilience in dispatching queue messages

·

2 min read

Hi guys,

Using Laravel is super cool. A lot of built-in stuff that we can use for our mission-critical apps. Help us a lot in our app development, right?

One of the things from Laravel - Queue, is super cool to use too. But it is missing a key point to make your app become more stable.

I will cover today how we are going to dispatch our queue msgs in a confident and resilient way.

Problem: Normal Job Dispatch / Send Queue Msg

Laravel gives us the simplest way to send a Queue Msg.

class SendVerificationEmailToUser implements ShouldQueue
{
    use SerializesModels;
    use Queueable;
    use Dispatchable;

    public function __construct(public User $user) {};
}

$user = User::create([...]);

Queue::push(new SendVerificationEmailToUser($user));
// or
SendVerificationEmailToUser::dispatch($user);

But, what if there was an error while dispatching a job?

You will end up:

  • 500 error (on HTTP) or failure job (on background job processing)

  • Lose the queue's payload/message.

  • Take you a lot of time to figure out what was the payload and resend the damn Queue's msgs.

  • and so much more...

Solution: Dispatch the Job in a Safer way

Check out my latest work, meet: Laravel SafeDispatcher

A simple diagram will help you understand how it works:

How does Laravel SafeDispatcher works?

SafeDispatcher will:

  • Stores the failed to dispatch msgs and helps you to retry them.

  • You can even change the connection driver or the name on retry.

    • Would really come in handy when you have a SQSException (size > 256kb), then you can resend using redis/database driver.
  • Ensure that your processing/flow is still working properly.

    • Super useful and a must-have for mission-critical apps.

SafeDispatcher also ships some useful RESTFul API endpoints to:

  • Listing the failed-to-dispatch jobs

  • View a single failed-to-dispatch job

  • Retry

    • Same queue driver or another queue driver

Conclusion

With SafeDispatcher, it provides more confidence and resilience when dispatching your queue's msgs.

Don't lose your queue's messages. Keep your app working stably. And love your life!

Thanks for reading and enjoy!