0
0
Ruby on Railsframework~8 mins

Action Mailer setup in Ruby on Rails - Performance & Optimization

Choose your learning style9 modes available
Performance: Action Mailer setup
MEDIUM IMPACT
This affects page load speed indirectly by how email sending tasks are handled, impacting user interaction responsiveness and server resource usage.
Sending emails when a user submits a form
Ruby on Rails
class UserMailer < ApplicationMailer
  def welcome_email(user)
    mail(to: user.email, subject: 'Welcome!')
  end
end

# In controller
UserMailer.welcome_email(@user).deliver_later
Emails are sent asynchronously using background jobs, freeing the web request to respond immediately.
📈 Performance GainNon-blocking request, reduces INP by 300-500ms, improves user interaction speed.
Sending emails when a user submits a form
Ruby on Rails
class UserMailer < ApplicationMailer
  def welcome_email(user)
    mail(to: user.email, subject: 'Welcome!')
  end
end

# In controller
UserMailer.welcome_email(@user).deliver_now
Sending email synchronously blocks the web request until the email is sent, causing slow response times.
📉 Performance CostBlocks rendering for 500ms+ depending on mail server latency, increasing INP and hurting user experience.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous email sending (deliver_now)N/AN/ABlocks server response delaying paint[X] Bad
Asynchronous email sending (deliver_later)N/AN/ANon-blocking server response, faster paint[OK] Good
Rendering Pipeline
Synchronous email sending blocks the server response, delaying the browser's ability to paint and respond to user input. Asynchronous sending offloads this work to background jobs, allowing the server to quickly send the response and the browser to render faster.
Server Response
Interaction to Next Paint (INP)
⚠️ BottleneckServer Response time due to blocking email send
Core Web Vital Affected
INP
This affects page load speed indirectly by how email sending tasks are handled, impacting user interaction responsiveness and server resource usage.
Optimization Tips
1Never send emails synchronously during web requests to avoid blocking response.
2Use deliver_later with background jobs to send emails asynchronously.
3Monitor server response times to ensure email sending does not delay user interactions.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with using deliver_now to send emails in a Rails controller?
AIt blocks the web request until the email is sent, slowing response time.
BIt increases the size of the JavaScript bundle.
CIt causes layout shifts in the browser.
DIt reduces the number of database queries.
DevTools: Performance
How to check: Record a performance profile while submitting the form that triggers email sending. Look at the server response time and main thread blocking.
What to look for: Long tasks blocking the main thread during request indicate synchronous email sending. Shorter tasks and faster response indicate asynchronous sending.