DeveloperJoy Logo DeveloperJoy

Simplifying Laravel Queue Management just with MySQL - You don't need SQS, RabitMQ, etc.

April 8, 2024
Simplifying Laravel Queue Management just with MySQL - You don't need SQS, RabitMQ, etc.

Laravel's queue feature is extensively used in various projects, both big and small. However, setting up queues often involves complex steps like using Redis for job management and Supervisor for monitoring queue processes, which may not always be convenient.

Let's consider an alternative to traditional queue management with Laravel Horizon for larger projects. While Laravel Horizon offers a comprehensive view of queues and workers, it still relies on Supervisor for continuous operation. But what about scenarios where minimal events occur, like a simple blog?

Running Queue Workers as Cronjobs

An efficient way to handle queue workers is by running them as cronjobs. This method, utilized on this site for tasks such as form submissions, eliminates the need to wait for background processes to complete.

By setting a maximum runtime for each worker and allowing it to stop once the job is done, you can streamline the queue process. Moreover, storing jobs at a database level simplifies the setup without the complexities of using Redis.

Setting Up Migrations

Laravel provides convenient CLI commands to create database tables for organizing queued jobs. Execute the following commands to generate the required migrations:

php artisan queue:table
php artisan queue:failed-table

Configuring Environment Variables

Inform Laravel that you are using the database for queue processing by setting the QUEUE_CONNECTION variable to database in your environment settings.

QUEUE_CONNECTION=database

Configuring Cron Schedule

For production environments, configure the cron schedule to run the queue worker effectively. Adjust the path based on your project setup and PHP binaries. A sample command could be:

* * * * * /usr/bin/php [PATH]/artisan queue:work --max-time=60 > /dev/null

Define the path by navigating to your Laravel project directory and running pwd to determine the absolute path. This cron schedule ensures continuous queue processing every minute.

Considerations

While this cron-based approach suits projects with moderate job volumes, it may not be ideal for handling extensive queues. Additionally, ensure your PHP configuration allows executions exceeding sixty seconds for seamless operation. Verify this by checking the max execution time using /usr/bin/php --info | grep max_execution_time. If the response is unlimited (0) or above sixty, you're all set!

← Go back to the blog

Work with me

Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.

Get 1 month free with yearly plan during checkout.

Technical Advisor

$ 3 ,995

/m

What's included:

  • Lead your team to a better code architecture, testing mentality, clean code, and more.
  • Lead knowledge-sharing sessions depending on current company status and requirements.
  • Help with product and technical decisions.
  • Pause or cancel anytime.

Integrated Senior Developer

$ 5 ,995

/m

Not available

What's included:

  • Includes all Technical Advisor services.
  • Engages as a team member in daily tasks.
  • Participates actively in day-to-day development.
  • Direct communication:
    Slack, and meetings included.
  • Pause or cancel anytime.

Want to talk first?

Ok, just book a call now.

FAQ