# Enqueue Multiple Jobs Together with perform_all_later in Rails

*Published on December 23, 2023*

---


Sometimes, you have a large number of jobs to dispatch. Maybe you're sending hundreds of thousands of emails at once, or processing a large file containing thousands of records and dispatching a job for each.

Agreed, you're already using a background job, so your end users don't have to wait for the whole job to finish. However, enqueuing each job still takes some work, right? You loop over each job and enqueue it&nbsp;individually. That means your application has to talk to the job backend (database, Redis, etc.) once for each job.

Wouldn't it be nice if there was a method that could let you enqueue all the jobs together, all in one go, without the overhead of multiple round-trips to the queue datastore?

Turns out, there is!

**Rails 7.1 shipped with a handy**&nbsp;[**`perform_all_later`**](https://edgeapi.rubyonrails.org/classes/ActiveJob.html#method-c-perform_all_later)&nbsp;**method that lets you enqueue multiple jobs to be executed at once.**

At this time, as far as I know, it's only properly supported by Sidekiq and GoodJob.

- Sidekiq's&nbsp;`push_bulk`&nbsp;method cuts down on Redis round-trip latency by pushing multiple jobs with Redis'&nbsp;[`lpush`](https://redis.io/commands/lpush/)&nbsp;command.
- GoodJob's bulk-enqueue feature buffers and enqueues multiple jobs at once using a single INSERT statement on the PostgreSQL database.

For other backends like&nbsp;**Delayed Job**&nbsp;and&nbsp;**Resque**&nbsp;it still loops over the jobs.

However, the nice thing about using&nbsp;`perform_all_later`&nbsp;is, as other job backends implement this functionality ([here is Ben Sheldon, creator of GoogJob&nbsp;trying to implement it in recently released Solid Queue](https://island94.org/2023/12/solid-queue-first-impressions)&nbsp;), your code won't have to change if you're using this method.

**Note:**&nbsp;This method won't trigger any callbacks for each job. However, it fires a&nbsp;`enqueue_all.active_job`&nbsp;event which you can subscribe to and get the adapter name and the list of jobs to be enqueued.

To learn more about the instrumentation API in Rails, check out the following article.

[Understanding the Instrumentation API in Rails](/rails-instrumentation-api)

Queue adapters can communicate the enqueue status of each job by setting&nbsp;`successfully_enqueued`&nbsp;and/or&nbsp;`enqueue_error`&nbsp;on the passed-in job instances.

Since you have access to the job classes in the event subscriber payload, you can query which jobs were enqueued successfully using the&nbsp;`successfully_enqueued?`&nbsp;method on the job.

```ruby
enqueued_jobs = jobs.select(&:successfully_enqueued?)
```

[Sidekiq has already supported this functionality](https://www.mikeperham.com/2021/11/07/whats-new-in-sidekiq-6.3/)&nbsp;for a while now, first with&nbsp;`Sidekiq::Client.push_bulk`&nbsp;method and later with a high-level wrapper method called&nbsp;`perform_bulk`, which you can directly call on your job class.

## How It Works

The&nbsp;`perform_all_later`&nbsp;method accepts an array of job instances. You can use it as follows:

```ruby
reminder_jobs = users.map do |user|
  ReminderJob.new(user).set(wait: 1.day)
end

ActiveJob.perform_all_later(reminder_jobs)
```

Here's a highly simplified implementation of the&nbsp;`perform_all_later`&nbsp;method.

```ruby
# lib/active_job/enqueuing.rb

def perform_all_later(*jobs)
  if queue_adapter.respond_to?(:enqueue_all)
    queue_adapter.enqueue_all(jobs)
  else
    jobs.each do |job|
      queue_adapter.enqueue(job)
    end        
  end
end
```

As you can see, it first checks if the queue adapter, i.e. Resque, Sidekiq, etc. responds to the&nbsp;`enqueue_all`&nbsp;method. If yes, then it calls&nbsp;`enqueue_all`&nbsp;passing the list of jobs. Otherwise, it simply loops over each job and enqueues it individually.

Now let's see what a sample&nbsp;`enqueue_all`&nbsp;implementation does. We'll look at the Sidekiq adapter.

```ruby
# lib/active_job/queue_adapters/sidekiq_adapter.rb

def def enqueue_all(jobs)
  Sidekiq::Client.push_bulk(
    "class" => JobWrapper,
    "wrapped" => job_class,
    "queue" => queue,
    "args" => jobs.map { |job| [job.serialize] },
  )
end
```

So behind the scenes, the&nbsp;`enqueue_all`&nbsp;uses the&nbsp;`push_bulk`&nbsp;feature in Sidekiq to dispatch all the jobs at once.

I couldn't stop myself from going one step further and taking a peek at the Sidekiq's implementation of the&nbsp;`push_bulk`&nbsp;method, which is quite interesting. But that's a topic for a separate post. So stay tuned!

**P.S. If you'd like to learn more about how Active Job is implemented, check out the following post**&nbsp;👇

[Rails Internals: A Deep Dive Into Active Job Codebase](/how-active-job-works-rails)

