Performance: Why background processing improves performance
Background processing improves page load speed and interaction responsiveness by moving slow tasks off the main request cycle.
def create
User.create!(user_params)
UserMailer.welcome_email.deliver_later
render :show
enddef create
User.create!(user_params)
UserMailer.welcome_email.deliver_now
render :show
end| Pattern | Server Blocking | Response Time | User Perceived Speed | Verdict |
|---|---|---|---|---|
| Synchronous processing | Blocks server thread | High (500ms+ delay) | Slow page load and interaction | [X] Bad |
| Background processing | No blocking | Low (fast response) | Fast page load and interaction | [OK] Good |
perform_later to enqueue jobs for background processing.perform_now runs job immediately, not in background; other options are invalid.MyJob.perform_later is called?class MyJob < ApplicationJob
def perform
sleep 5
puts 'Job done'
end
end
MyJob.perform_laterperform_later queues the job to run asynchronously in background.class ReportJob < ApplicationJob
def perform
generate_report
end
end
ReportJob.perform_later()perform_later() with or without parentheses is valid Ruby syntax.generate_report inside perform is allowed and common.deliver_now sends email immediately, blocking user; deliver_later queues email to send in background.deliver_later sends email asynchronously, so user signup is fast and responsive.Pass the test and this pattern moves unknown