Skip to content
RY

The Real Story Behind Delayed Extensions Removal from Sidekiq

Ram Laxman Yadav12 min read

If you’ve worked on an older Rails app, you’ve probably seen code like this somewhere in the codebase:

user.delay.send_welcome_email

That .delay call used to be Sidekiq’s easiest trick. You could call .delay on almost any object, and it would quietly run that method later, in the background, without you writing a single job class. It felt like magic.

Sidekiq disabled it by default in version 5, and removed it completely in version 7. A lot of people assume this happened because of a security bug — a scary CVE, a hacker exploit, something dramatic. That’s not actually true. There’s no public CVE for this. The real reason is quieter, and honestly more interesting: .delay had a bug built into its design that could silently make your app act on old, wrong data — and nobody would even get an error message when it happened.

This post explains that real reason in plain English, with a working example, and tells you exactly what to do if your own Rails app still has .delay calls hiding in it somewhere.

1. Summary

  • .delay, .delay_for, and .delay_until were a feature called Delayed Extensions. They let you call .delay on any class or object in your app to run one of its methods later, in the background.
  • They worked by turning the object you called .delay on into text (using YAML) and saving that text in Redis, to be turned back into a real Ruby object later when the job finally ran.
  • The real problem: that saved text is a snapshot of the object at the moment you called .delay. If the real record in your database changes before the job runs — which happens all the time in a busy app — the job still works with the old snapshot. No error. No warning. Just wrong behavior.
  • A second, smaller problem: .delay added new methods to Ruby’s built-in Class, meaning every single class in your app — and in every gem you use — silently gained three new methods it never asked for.
  • The fix Sidekiq’s own documentation has recommended for years, which is now the only way to do it, is simple: pass a plain ID into the background job, not the object itself, and re-fetch the fresh record from the database when the job actually runs.
  • If your app still uses .delay anywhere, there’s a short-term gem (sidekiq-delay_extensions) that keeps it working while you fix things properly — covered in section 5.

2. The real root cause — explained with a simple example

How .delay actually worked under the hood

When you wrote user.delay.send_welcome_email, here’s what Sidekiq did, step by step:

  1. It took the user object and converted it into a YAML string — basically, a big block of text that describes the object’s class and all its current attribute values.
  2. It saved that YAML text, plus the method name (send_welcome_email) and any arguments, as a job in Redis.
  3. Later, a background worker picked up that job, read the YAML text back, turned it into a Ruby object again (this step is called deserializing), and called send_welcome_email on it.

That sounds reasonable at first. The problem is step 3. The object that gets recreated isn’t a fresh copy of the current database row — it’s a photograph of the row as it looked at the exact second you called .delay.

A concrete example of the bug

Imagine a simple subscription app. A user can be active or cancelled. You only want to send a “we miss you” email to users who are still cancelled three days after they cancel — if they resubscribe in the meantime, you obviously don’t want to email them.

Here’s the kind of code that used to be written with Delayed Extensions:

class SubscriptionsController < ApplicationController
def cancel
@subscription = current_user.subscription
@subscription.update!(status: 'cancelled')
# "Check back in 3 days and email them if they're still cancelled"
current_user.delay_for(3.days).send_win_back_email
end
end
class User < ApplicationRecord
def send_win_back_email
return unless subscription.status == 'cancelled'
WinBackMailer.win_back_email(self).deliver_now
end
end

This looks correct. send_win_back_email even has a safety check — it only sends the email if the subscription is still cancelled. Here’s the bug: that safety check is checking the wrong copy of the data.

When current_user.delay_for(3.days) ran, Sidekiq immediately converted current_user into YAML — attributes and all — and saved that snapshot. Three days later, when the job runs, it doesn’t load the current user from the database and check their live status. It rebuilds the user from that three-day-old YAML snapshot, where the subscription might already say cancelled baked right into the frozen copy, depending on how the association was loaded at snapshot time. If the user resubscribed on day 2, the live database correctly says active — but the job is working from a photograph taken on day 0, and that photograph never changes.

Depending on exactly how the object was serialized, the user could easily get a “we miss you, please come back” email a day after they already came back. Nothing crashes. No exception gets logged. The job runs “successfully.” It’s just quietly wrong — which is the worst kind of bug, because nobody notices it until a customer complains, and by then it looks unrelated to any recent code change.

Warning

This is the actual, documented reason Delayed Extensions got disabled and removed — not a security hole, a staleness bug. Sidekiq’s own wiki says it plainly: delaying a method on an object “stores object state in Redis which can get out of date, causing stale data problems,” and strongly recommends never delaying instance methods for exactly this reason.

The second problem: monkey-patching every class in your app

On top of the staleness bug, .delay worked by adding three new methods — delay, delay_for, and delay_until — directly onto Ruby’s built-in Class. That means every class in your entire application, and every class in every gem you’ve installed, silently gained these three methods the moment Sidekiq loaded.

This is called a monkey patch, and it’s risky for a simple reason: if any other gem you use also happens to define a method called delay on Class (several job-related gems historically did, for the exact same reason), the two definitions collide, and whichever one loaded last silently wins. You’d get confusing, hard-to-explain bugs with zero obvious connection to Sidekiq.

3. The fix, explained

The fix Sidekiq settled on isn’t a clever trick — it’s the boring, obviously-correct pattern that Rails’ own background job system (ActiveJob) has recommended from day one: never pass a whole object into a background job. Pass its ID, and load a fresh copy inside the job.

Here’s the same win-back email, written the correct way, with a real Sidekiq job class:

class WinBackEmailJob
include Sidekiq::Job
def perform(user_id)
user = User.find_by(id: user_id)
return unless user # the user might have been deleted by now — handle that too
return unless user.subscription.status == 'cancelled'
WinBackMailer.win_back_email(user).deliver_now
end
end

And enqueuing it:

class SubscriptionsController < ApplicationController
def cancel
@subscription = current_user.subscription
@subscription.update!(status: 'cancelled')
WinBackEmailJob.perform_in(3.days, current_user.id)
end
end

Why this actually fixes the bug: perform runs User.find_by(id: user_id) at the moment the job executes, three days later — not at the moment it was scheduled. If the user resubscribed on day 2, user.subscription.status correctly reads active, the return unless guard stops the job, and no email goes out. The check is finally checking live data instead of a frozen photograph.

If you’re using ActiveJob instead of a plain Sidekiq job (common in most Rails apps), the same idea looks like this:

class WinBackEmailJob < ApplicationJob
queue_as :default
def perform(user_id)
user = User.find_by(id: user_id)
return unless user
return unless user.subscription.status == 'cancelled'
WinBackMailer.win_back_email(user).deliver_now
end
end
WinBackEmailJob.set(wait: 3.days).perform_later(current_user.id)

Same fix, same reasoning — only an ID crosses into Redis, and the real record gets loaded fresh when the job actually runs.

4. Benefits of removing Delayed Extensions

  • No more stale-data bugs. Passing an ID and reloading the record forces every job to always work with current data. The entire class of “the job ran on old information” bugs simply can’t happen anymore, because there’s no old snapshot to work from in the first place.
  • Smaller, faster jobs in Redis. A plain integer ID takes a few bytes. A YAML dump of a whole ActiveRecord object — including every column and sometimes loaded associations — can be hundreds of times bigger. Smaller job payloads mean less memory used in Redis and faster enqueue/dequeue times, especially at high job volume.
  • No more surprise methods on every class. Removing the monkey patch means Class goes back to being exactly what Ruby and your other gems expect it to be — one less thing that can silently clash with another library.
  • One consistent pattern across the Rails ecosystem. .delay was a Sidekiq-only trick. perform_later(id)/perform_async(id) is the same pattern ActiveJob has always recommended, and the same pattern Resque, Delayed::Job, and every other mainstream Ruby job system uses too. Code written this way doesn’t depend on which background job library you happen to be using.
  • Bugs show up immediately, not three days later. If a record genuinely doesn’t exist anymore when a job runs, find_by returns nil right there in perform, where you can handle it on purpose (log it, skip it, retry it) — instead of a corrupted or half-stale object silently doing the wrong thing with no error at all.

5. How to update an older Rails app that still uses .delay

Step 1 — find every place it’s used. Search your whole codebase, including views and mailers:

Terminal window
grep -rn "\.delay\b\|\.delay_for\|\.delay_until" app lib

Step 2 — if you need breathing room, add the compatibility gem first. sidekiq-delay_extensions is the original feature, pulled out of Sidekiq itself into its own gem, so old code keeps working exactly as before while you migrate piece by piece:

# Gemfile
gem 'sidekiq-delay_extensions'

This buys you time — it does not fix the staleness bug. Treat it as a stepping stone, not a destination.

Step 3 — convert each call site, one at a time. For every .delay call you found in step 1:

  1. Create a real job class (Sidekiq::Job or ApplicationJob) if one doesn’t already exist for that action.
  2. Change the method’s arguments to IDs and simple values only — never pass the object itself, and never pass a whole ActiveRecord association.
  3. Inside perform, reload the record from the database using find_by (not find, so a missing record returns nil instead of raising) and add a guard clause (return unless record) before doing anything else.
  4. Re-check whatever condition mattered when the job was first created — don’t assume it’s still true.

Step 4 — write a test for the “it changed in the meantime” case specifically. This is the bug you’re actually fixing, so test for it directly: enqueue the job, change the record’s state as if time had passed, run the job, and assert it does the right thing for the new state — not the state that existed when the job was created.

Step 5 — once every call site is converted, remove the compatibility gem. At that point you’re fully off Delayed Extensions, on the same pattern every modern Rails background job uses, and the staleness bug class is gone for good.

FAQ

Why was .delay removed from Sidekiq? Not for security — there’s no public CVE behind it. It was removed because it serialized a full snapshot of an object into Redis at the moment you called .delay, and ran the job against that frozen snapshot later. If the real record changed in the meantime, the job silently acted on stale data with no error at all.

Is .delay, .delay_for, or .delay_until still available in Sidekiq? No. They were disabled by default in Sidekiq 5 and fully removed in Sidekiq 7. Code that still calls them will raise a NoMethodError on Sidekiq 7+ unless you add the sidekiq-delay_extensions gem.

What should I use instead of .delay in a Rails app? Pass a plain ID (not the object itself) into a real background job — either a Sidekiq::Job with perform_async/perform_in, or an ActiveJob with perform_later. Reload the record from the database inside perform, so the job always works with current data instead of a stale snapshot.

Is sidekiq-delay_extensions safe to use long-term? It restores the old .delay behavior exactly as it was, including the staleness bug described in this post. Treat it as a temporary bridge while you migrate call sites, not a permanent fix.

Get new posts in your inbox

One email whenever I publish something new. No spam, unsubscribe anytime.

← Back to all posts