Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stratboy's avatar

Why and how exactly should I setup a queue when using Scout/Meilisearch?

Hi, from the docs:

While not strictly required to use Scout, you should strongly consider configuring a queue driver before using the library. Running a queue worker will allow Scout to queue all operations that sync your model information to your search indexes, providing much better response times for your application's web interface.

Ok. I've kinda understood, but not really well. A queue of what? I mean, what should I put in queue and how? What is exactly the process/job I should queue and when?

0 likes
3 replies
martinbean's avatar

@stratboy Well as the docs say, you can optionally have Scout queue operations to updates records in your search index:

https://github.com/laravel/scout/blob/991056204a410fc7a9c22b1ec9bbbae6bb675241/config/scout.php#L34-L45

If you’re using a service like Algolia, then updating an index involves an API request. If you don’t queue, then the user has to wait for a response from your application as it makes a HTTP request to Algolia with the update request, and wait for Algolia to respond. If Algolia is running slowly or is completely down at that time, then your application will be slow or time out as well.

stratboy's avatar

@martinbean Ok but still I don't exactly understand why I should use queues with meilisearch, since currently in my installation, it auto updates the index every time a new db record is created or deleted. I'm not calling an API to update the index. I only loaded the records the first time.

Well, I never built anything with full text search engines so probably I'm missing something.

LaryAI's avatar
Level 58

To understand why and how to set up a queue when using Laravel Scout with Meilisearch, let's break it down step by step.

Why Use a Queue?

When you use Laravel Scout to sync your models with Meilisearch, every time you create, update, or delete a model, Scout will perform an operation to update the search index. If you do this synchronously (i.e., in real-time), it can slow down your application, especially if you have a large number of operations. By using a queue, you can offload these operations to be processed in the background, which will improve the response times of your application's web interface.

How to Set Up a Queue

  1. Configure Queue Driver: First, you need to configure a queue driver in your Laravel application. Laravel supports several queue drivers like database, redis, sqs, etc. For simplicity, let's use the database driver.

    In your .env file, set the queue driver:

    QUEUE_CONNECTION=database
    
  2. Create a Queue Table: If you are using the database driver, you need to create a table to store the queued jobs. Run the following Artisan command to create the migration:

    php artisan queue:table
    

    Then, run the migration:

    php artisan migrate
    
  3. Run the Queue Worker: To process the queued jobs, you need to run a queue worker. You can do this using the following Artisan command:

    php artisan queue:work
    
  4. Enable Queues in Scout: By default, Scout will perform operations synchronously. To enable queues, you need to set the queue option to true in your config/scout.php configuration file:

    'queue' => true,
    

Example Code

Here is an example of how you might set up a model to be indexed by Scout and Meilisearch, and how the queue will handle the operations:

  1. Install Laravel Scout and Meilisearch:

    composer require laravel/scout
    composer require meilisearch/meilisearch-laravel
    
  2. Configure Scout and Meilisearch: In your config/scout.php file, set the driver to meilisearch:

    'driver' => env('SCOUT_DRIVER', 'meilisearch'),
    

    In your .env file, add the Meilisearch configuration:

    SCOUT_DRIVER=meilisearch
    MEILISEARCH_HOST=http://127.0.0.1:7700
    
  3. Add the Searchable Trait to Your Model:

    use Laravel\Scout\Searchable;
    
    class Post extends Model
    {
        use Searchable;
    
        // Define the data that should be indexed
        public function toSearchableArray()
        {
            return [
                'id' => $this->id,
                'title' => $this->title,
                'content' => $this->content,
            ];
        }
    }
    
  4. Queue the Operations: With the queue option enabled in config/scout.php, Scout will automatically queue the operations. You don't need to manually queue anything; Scout handles it for you.

Summary

By setting up a queue, you offload the indexing operations to be processed in the background, which improves the performance of your application. The steps involve configuring a queue driver, creating a queue table (if using the database driver), running a queue worker, and enabling queues in Scout's configuration.

This setup ensures that your application's web interface remains responsive while the indexing operations are handled asynchronously in the background.

Please or to participate in this conversation.