Performance: Background email delivery
This affects page load speed and user interaction responsiveness by offloading email sending from the main request cycle.
def create
UserMailer.welcome_email(@user).deliver_later
redirect_to root_path
enddef create
UserMailer.welcome_email(@user).deliver_now
redirect_to root_path
end| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous email sending (deliver_now) | N/A | N/A | Blocks response, delays paint | [X] Bad |
| Asynchronous email sending (deliver_later) | N/A | N/A | Non-blocking, fast paint | [OK] Good |
deliver_later do in Rails email sending?deliver_later purposedeliver_later queues the email to be sent asynchronously, so the app doesn't wait for the email to finish sending.deliver_now sends immediately and blocks, scheduling for future time requires extra setup, and previewing doesn't send email.deliver_later on the result.UserMailer.welcome_email(user).deliver_later. Others have incorrect method order or non-existent methods.class UserMailer < ApplicationMailer
def welcome_email(user)
mail(to: user.email, subject: 'Welcome!')
end
end
UserMailer.welcome_email(@user).deliver_later
What happens when this code runs?deliver_later queues the email to be sent asynchronously by a background worker.deliver_later queues the job. The app does not wait for sending to complete.UserMailer.welcome_email(user).deliver_laterBut emails are not being sent. What is the most likely cause?
deliver_later queues jobs but requires a background job adapter (like Sidekiq, Async, or Delayed Job) to process the queue.deliver_now sends immediately but blocks. Missing mail call causes no email but not related to background delivery. deliver_later does not require a block.user.email_confirmed? is true, so the condition must guard the delivery call.deliver_later to send asynchronously. if user.email_confirmed?
UserMailer.welcome_email(user).deliver_now
end sends immediately, not in background. UserMailer.welcome_email(user).deliver_later unless user.email_confirmed? sends when email is not confirmed (wrong logic). UserMailer.welcome_email(user).deliver_later(user.email_confirmed?) passes a boolean as argument incorrectly.Pass the test and this pattern moves unknown