In Rails, what is the difference between calling perform_later and perform_now on an Active Job class, and what does each one return?
answer
- one goes through the adapter, one does not
- arguments serialized only on the queued path
- perform_later: job instance or false
- perform_now: whatever perform returns
- :async fallback, :solid_queue in production
basics
~20 sperform_later serializes the arguments and hands the job to the configured queue adapter, returning the job instance (or false if enqueuing failed); perform_now runs perform at once in the calling process and returns perform's own return value.
solid answer
~40 s`perform_later` builds the job, runs the enqueue callbacks and passes it to the queue adapter, which serializes the arguments (a record becomes a GlobalID); a worker later rebuilds the job and calls `perform`. It returns the job instance, carrying its `job_id`, or `false` when a `before_enqueue` callback throws `:abort` or the adapter raises `ActiveJob::EnqueueError`. `perform_now` never touches the adapter: it runs the perform callbacks and `perform` on the caller's thread, blocks until it finishes and returns whatever `perform` returns, and an exception that no `rescue_from` handler takes propagates to the caller. Where `perform_later` goes depends on configuration: Rails falls back to `:async`, an in-process thread pool that loses pending jobs on restart (`:test` in tests), and a new app's `production.rb` is switched to `:solid_queue` by the Solid Queue installer.
code
ruby · 9 linesjob = GenerateCertificateJob.perform_later(enrollment) do |enqueued|
Rails.logger.warn("not enqueued") unless enqueued.successfully_enqueued?
end
job # => #<GenerateCertificateJob ...> or false
job && job.job_id
blob_id = GenerateCertificateJob.perform_now(enrollment)
# => the value perform returned; any unhandled error is raised herego deeper
Know which method queues and which runs inline, and what each returns: the job instance or false versus perform's own value.
Explain the path perform_later takes: enqueue callbacks, adapter, serialization through GlobalID, and a worker that rebuilds the job and calls perform_now.
Show you check which adapter each environment really uses, never ship :async to production, and keep slow work out of perform_now in request paths.
Be ready to argue where inline execution is acceptable and where every side effect must cross a durable queue, and how that choice shapes failure handling.
## Two entry points to the same `perform` An Active Job class defines one instance method, `perform`, that holds the work. Rails gives you two class methods that end up calling it, and they differ in *where* and *when* the work happens: - **`perform_later`** puts the job on a queue so a separate worker runs it later. - **`perform_now`** runs it immediately, inline, in the code that called it. In an e-learning app that issues a PDF certificate when a learner finishes a course, the difference is whether the learner's request waits for the PDF to render: ```ruby class GenerateCertificateJob < ApplicationJob queue_as :certificates def perform(enrollment) pdf = CertificatePdf.new(enrollment).render enrollment.certificate.attach(io: StringIO.new(pdf), filename: "certificate.pdf") enrollment.certificate.blob.id end end GenerateCertificateJob.perform_later(enrollment) # => #<GenerateCertificateJob ...> or false GenerateCertificateJob.perform_now(enrollment) # => the blob id perform returned ``` ## What `perform_later` does 1. It instantiates the job with your arguments; the queue name and priority come from the class (`queue_as`, `queue_with_priority`). 2. It runs the **enqueue callbacks** (`before_enqueue`, `around_enqueue`, `after_enqueue`) in the caller. 3. It calls the adapter's `enqueue`, or `enqueue_at` when a delay was set with `set(wait:)`. The adapter calls `serialize` on the job, which turns every argument into JSON-safe data: an Active Record object becomes a **GlobalID** string, and an unsupported type raises `ActiveJob::SerializationError` right there, in the caller. 4. It returns the **job instance** when the adapter accepted it, or **`false`** when a `before_enqueue` callback threw `:abort` or the adapter raised `ActiveJob::EnqueueError` (kept on `job.enqueue_error`). With a block, `perform_later` also yields the job after the attempt, so `job.successfully_enqueued?` can be checked inline. The worker later calls `ActiveJob::Base.execute` with the stored payload: it rebuilds the job, and that rebuilt job's `perform_now` is what finally runs `perform`. The value `perform` returns there is never sent back to the caller. ## What `perform_now` does `perform_now` skips steps 2 to 4 entirely. It increments `executions`, runs the **perform callbacks** (`before_perform`, `around_perform`, `after_perform`) and `perform` on the current thread, and returns `perform`'s value. Because nothing is serialized, the arguments are the very objects you passed, including an unsaved record that `perform_later` would refuse. If `perform` raises, Active Job first offers the exception to the class's `rescue_from` handlers; anything they do not handle is re-raised to the caller. ## Side by side | | `perform_later` | `perform_now` | |---|---|---| | Where `perform` runs | a worker, later | the calling thread, now | | Arguments | serialized (GlobalID for records) | passed as-is | | Callbacks in the caller | enqueue callbacks | perform callbacks | | Returns | the job instance, or `false` | `perform`'s return value | | An exception in `perform` | stays on the worker side | raised to the caller unless handled | ## Which adapter receives the job `perform_later` is only as durable as the adapter behind it: - Without any configuration Rails uses **`:async`**, a Concurrent Ruby thread pool inside the same process. Jobs do run in the background, but pending and scheduled ones vanish when the process restarts. A new app's `development.rb` sets no adapter, so development uses it. - The test environment falls back to **`:test`**, which records jobs instead of running them. - `rails new` runs the Solid Queue installer, which sets `config.active_job.queue_adapter = :solid_queue` in **`production.rb`** only. ## When to reach for which - Use **`perform_later`** for anything slow or failure-prone that the user should not wait for: rendering the certificate PDF, calling a payment API, sending a batch of notifications. - Use **`perform_now`** where blocking is the point: a console fix-up, a rake task, one job composing another inline, or reproducing a failing job by hand. - Do not treat `perform_now` as a cheap `perform_later`: it holds the request thread for the full duration and surfaces the job's errors to the caller. ## Common mistakes - **Reading `perform_later`'s return value as the job's result.** It is the job object. Whatever `perform` returns on the worker is discarded, so anything the caller needs later must be written somewhere it can read, such as the attached certificate or a status column on the enrollment. - **Wrapping `perform_later` in `rescue` to catch job failures.** An error raised inside `perform` happens on the worker and never reaches the caller. Only enqueue-time problems reach it, such as `ActiveJob::SerializationError` for an argument Active Job cannot store. - **Passing bulky data as arguments.** Every argument is written into the payload. Pass the enrollment, which travels as a short GlobalID string, not the rendered PDF bytes or a large hash of attributes. - **Assuming a call inside a transaction waits for the commit.** In Rails 8.1 it does not unless the job opts in, so the worker can run before the data it needs is visible.
- Why can perform_now take an unsaved Active Record object when perform_later raises?`perform_now` never serializes its arguments; the object reaches `perform` unchanged in the same process. `perform_later` serializes each argument, and an Active Record object becomes a GlobalID built from its id. An unsaved record has no id, so Active Job raises `ActiveJob::SerializationError` ("Maybe you forgot to call save?") in the caller, before anything is queued.
- What happens to perform_later in development if nobody configured a queue adapter?It uses `:async`, Rails' fallback outside the test environment: a thread pool inside the running server. The job does run in the background, so the request does not wait, but nothing is persisted. Restarting the server drops every pending or scheduled job, which is why production gets `:solid_queue` instead.
- How does the caller find out that perform_later did not enqueue the job?The return value is `false` instead of the job instance. That happens when a `before_enqueue` callback throws `:abort`, or when the adapter raises `ActiveJob::EnqueueError`, which Active Job stores on `job.enqueue_error`. The block form yields the job after the attempt, so `job.successfully_enqueued?` can be checked in place.
- perform_now still goes through the queue adapter, just with no delay
- perform_later returns the value perform computes once the worker is done
- perform_later makes the request wait until the job has finished
- The :async adapter persists jobs, so it is fine for production
- perform_now serializes and reloads its arguments exactly like perform_later