Understanding Rails · Season 1: The trip of one click
Work that can wait
Episode 9 · overview · about 2 minutes · Rails 8.1 · by Tommy A. Caruso
How was this episode?
The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.
Not endorsed yet. Are you a Rails developer? What's an endorsement?
Endorse this version To endorse: create an account, confirm your email, then link GitHub.
What changed
- Version 2, November 20, 2026: Solid Queue keeps jobs in a database table only in production, so the narration now says so.
Background jobs, Solid Queue, mailers, and the closing replay.
Free to watch. If it helped, support the series.
Transcript
0:00 Work that can wait
Part nine. Work that can wait.
Say the followers of Eagle Ridge should get an email about each new note. Sending mail is slow: a mail server can take seconds to answer. Our click shouldn't wait for that.
0:18 Background jobs
So the work goes into a background job. The controller says perform later, and answers at once. The job runs a moment after, on its own.
A job is a small class with one method, perform. This one finds each follower of the trail, and sends them an email.
0:37 Solid Queue
Where does the job wait? In production, Rails 8's Solid Queue keeps it in a table in your database. A worker process picks it up, runs it, and marks it done.
0:48 Global IDs
The job doesn't carry the whole note. It carries a reference, a global ID, and finds the note again when it runs, so it always sees the latest version.
0:59 Retries
Mail servers fail sometimes. One line, retry on, tells the job to try again a little later, a few times, before it gives up.
1:08 Mailers
The email itself comes from a mailer, which works like a controller for mail. It has actions, and each action has its views, in HTML and in plain text.
And there's a shortcut. Deliver later puts the email on the queue by itself, as a job.
1:27 Solid Queue in Puma
In production, it doesn't even need a server of its own. A new Rails 8 app can run Solid Queue right inside Puma.
1:35 The whole trip
So, one last time. Someone clicks Post note. The request crosses the internet to Thruster and Puma, through Rack and the middleware, to the router. The notes controller checks it, and asks the model to save. Active Record writes one insert. A one line view answers with a turbo stream, and Turbo puts the note at the top of the list.
All in about the time of a blink. And now you know every step.
Check your understanding
-
The controller calls NotifyFollowersJob.perform_later(note). When does the job's perform method run?
- Right away, before the controller answers
- Never, until you call perform_now
- Later, when a worker picks the job off the queue
- When the follower opens their email
Show answer
Later, when a worker picks the job off the queue. perform_later puts the job on a queue and returns. perform_now is the one that runs it right away. Source: Calling the perform_* Methods, Active Job Basics (Rails Guides 8.1).
-
A note is passed to perform_later. Before the job runs, its author edits the body. Which body does the job see?
- The edited body, because the job finds the note again when it runs
- The old body, copied into the job when it was queued
- No body: jobs can't take Active Record objects
- Whichever body was saved first
Show answer
The edited body, because the job finds the note again when it runs. The job stores a GlobalID, not a copy. When perform is called, it uses that ID to load the note from the database. Source: Missing Records, Active Job Basics (Rails Guides 8.1).
-
The job says retry_on Net::OpenTimeout, with no other options, and the mail server keeps timing out. What happens?
- It retries forever
- It retries once, right away, then drops the job
- It retries after a short wait, up to 5 attempts, then lets the error through
- Nothing: retry_on only logs the error
Show answer
It retries after a short wait, up to 5 attempts, then lets the error through. By default retry_on waits 3 seconds between tries and makes 5 attempts. After that the error goes up to the queue. Source: ActiveJob::Exceptions::ClassMethods, retry_on (Rails API 8.1).
-
What does NoteMailer.with(note:).new_note.deliver_later do?
- Sends the email before the controller answers
- Puts a job on the queue to send the email, so the controller doesn't wait
- Saves the email as a draft
- Sends the email at a set time each day
Show answer
Puts a job on the queue to send the email, so the controller doesn't wait. deliver_later is backed by Active Job. It queues the email, and the action carries on without waiting. Source: Call the Mailer, Action Mailer Basics (Rails Guides 8.1).
Sources
- Calling the perform_* Methods, Active Job Basics (Rails Guides 8.1)
- Default Backend: Solid Queue, Active Job Basics (Rails Guides 8.1)
- Missing Records, Active Job Basics (Rails Guides 8.1)
- ActiveJob::Exceptions::ClassMethods, retry_on (Rails API 8.1)
- Call the Mailer, Action Mailer Basics (Rails Guides 8.1)