Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 37 ยท Testing, Jobs and Deployment

Background Jobs and Caching

Move slow work out of requests with Active Job and queue backends, retries and idempotency, then speed up pages with Rails.cache, fragment caching and cache keys. Includes runnable queue and cache stand-ins.

A web request should finish in a fraction of a second. Sending an email, resizing an image, calling a slow payment API or building a report can take much longer. If you do that work inside the request, the user waits and a slow third party can take your site down with it. The fix is a background job: the controller puts a small description of the work on a queue and answers immediately, and a separate worker process does the work.

The second half of this lesson is about the opposite problem: work that is fast enough but repeated thousands of times with the same answer. Caching stores a result so you compute it once.

Real Active Job and Rails.cache need Rails, a queue backend and a cache store, which the site's runner does not have. So the Rails code is read-only, and the runnable parts are plain Ruby stand-ins of the mechanisms (a queue with a worker thread, retries, a cache with expiry).

Active Job

Active Job is a common interface over queue backends. You write a job class once:

Ruby
# app/jobs/welcome_email_job.rb
class WelcomeEmailJob < ApplicationJob
  queue_as :default

  retry_on Net::OpenTimeout, wait: :polynomially_longer, attempts: 5
  discard_on ActiveJob::DeserializationError   # the record was deleted: give up

  def perform(user)
    UserMailer.welcome(user).deliver_now
  end
end

and enqueue it from anywhere:

Ruby
WelcomeEmailJob.perform_later(user)                 # as soon as a worker is free
WelcomeEmailJob.set(wait: 10.minutes).perform_later(user)
WelcomeEmailJob.set(queue: :low, priority: 10).perform_later(user)

Notes:

  • Pass records or simple values. Rails serializes an Active Record argument to a global ID (gid://app/User/42) and loads it again in the worker. Never pass big objects.
  • Rails 8 ships Solid Queue, a database-backed backend, as the default for new apps, so you need no Redis. Others: Sidekiq (Redis), GoodJob (Postgres), Resque. You switch with config.active_job.queue_adapter.
  • bin/jobs starts Solid Queue workers; in development the :async adapter runs jobs in threads in the same process.
  • perform_now runs the job inline, which is handy in the console and in tests.

Stand-in: a queue and a worker

Ruby Stand-in: a job queue with retries

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The important design rule is visible here: a job can run more than once (retries, crashes, deploys), so write jobs to be idempotent: running them twice must have the same effect as once. Check "was the email already sent?" before sending, use unique keys with payment APIs, and prefer "set the status to paid" over "add 10 to the balance".

Mail in the background

UserMailer.welcome(user).deliver_later is a shortcut that wraps deliver_now in an Active Job. Always prefer deliver_later in controllers so the request does not wait for SMTP.

Caching

Rails has several layers. From cheapest to think about:

  1. HTTP caching. fresh_when(@post) or stale?(@post) send an ETag; when the browser asks again, Rails answers 304 Not Modified without rendering anything.
  2. Fragment caching stores rendered HTML pieces:
ERB
<% cache post do %>
  <article>
    <h2><%= post.title %></h2>
    <%= render post.comments %>
  </article>
<% end %>

The cache key includes the record's updated_at, so when the post changes the key changes and the old entry is simply never used again. This is called key-based expiration, and it is the core Rails idea: instead of deleting stale entries, make a new key. To make a comment refresh its post's fragment, use belongs_to :post, touch: true. Nested cache calls give the "Russian doll" pattern.

  1. Low-level caching with Rails.cache.fetch:
Ruby
def self.top_tags
  Rails.cache.fetch("top_tags/v2", expires_in: 15.minutes) do
    Tag.joins(:posts).group(:id).order("count(*) desc").limit(10).to_a
  end
end

fetch returns the stored value or runs the block, stores the result and returns it. Cache stores: :memory_store (one process), :file_store, Redis or Memcached, and Rails 8's :solid_cache_store (database).

In development caching is off unless you run bin/rails dev:cache; in production it is on.

Stand-in: Rails.cache.fetch with expiry and versioned keys

Ruby Stand-in: a cache with fetch, expiry and key versions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Caching rules of thumb

  • Measure first. Cache the slow, popular thing, not everything.
  • Never cache anything user-specific under a shared key (a fragment showing "Hello, Ana" would be shown to everyone). Put the user in the key, or cache only the common part.
  • Give cache keys a version in the name so you can change the data shape (top_tags/v2).
  • Fix N+1 queries before adding caches, since a cache on top of a slow query hides the problem.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should slow work such as sending email run in a background job?

    The job queue decouples the user's response from slow or unreliable work.
  2. 2. What does it mean for a job to be idempotent?

    Jobs may be retried or run twice after a crash, so repeating them must be safe.
  3. 3. How does key-based expiration work for fragment caching?

    When the record changes, its updated_at and so the key change, and the old entry is no longer read.
  4. 4. What does Rails.cache.fetch(key) { ... } do on a hit?

    The block runs only on a miss; its result is stored and returned.

Summary

  • Active Job gives one API (perform_later) over queue backends such as Solid Queue and Sidekiq.
  • Jobs can run twice, so make them idempotent; use retry_on and discard_on.
  • Cache with HTTP validators, fragment caching (key-based expiration, touch: true) and Rails.cache.fetch.
  • Never share user-specific content under one key, and fix slow queries before caching them.

Next: Mailers and file uploads