0
0
Ruby on Railsframework~8 mins

Scheduled jobs in Ruby on Rails - Performance & Optimization

Choose your learning style9 modes available
Performance: Scheduled jobs
MEDIUM IMPACT
Scheduled jobs affect background processing speed and server resource usage, indirectly impacting user experience by offloading work from request cycles.
Running periodic data cleanup without blocking user requests
Ruby on Rails
class CleanupJob < ApplicationJob
  queue_as :default

  def perform(batch_size: 100)
    User.find_in_batches(batch_size: batch_size) do |users|
      users.each { |user| user.posts.destroy_all }
    end
  end
end

# Scheduled with cron and uses batch processing with Sidekiq concurrency
Processes users in small batches asynchronously, reducing memory spikes and freeing workers quickly.
📈 Performance GainReduces blocking time by 80%, improves worker availability, and lowers request latency
Running periodic data cleanup without blocking user requests
Ruby on Rails
class CleanupJob < ApplicationJob
  queue_as :default

  def perform
    User.all.each do |user|
      user.posts.destroy_all
    end
  end
end

# Scheduled with cron to run every hour synchronously
This job loads all users and deletes posts in a single run, causing high memory use and long execution blocking resources.
📉 Performance CostBlocks worker threads for minutes, increasing request latency and risking timeouts
Performance Comparison
PatternServer LoadBlocking TimeResource UsageVerdict
Synchronous large batch jobHighLong blockingHigh memory spikes[X] Bad
Asynchronous batched jobModerateShort blockingControlled memory use[OK] Good
Rendering Pipeline
Scheduled jobs run outside the browser rendering pipeline but affect server response times and resource availability, which influence interaction responsiveness (INP).
Server Processing
Request Handling
⚠️ BottleneckLong-running jobs block worker threads, delaying request processing.
Core Web Vital Affected
INP
Scheduled jobs affect background processing speed and server resource usage, indirectly impacting user experience by offloading work from request cycles.
Optimization Tips
1Avoid running large scheduled jobs synchronously to prevent blocking server threads.
2Use batching and asynchronous queues to reduce memory spikes and improve concurrency.
3Monitor job duration and queue latency to keep background processing efficient.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance risk of running large scheduled jobs synchronously in Rails?
AThey increase CSS rendering time in the browser
BThey block worker threads causing slower user request handling
CThey cause layout shifts on the page
DThey reduce JavaScript bundle size
DevTools: Rails logs and Sidekiq Web UI
How to check: Monitor job execution times and queue latency in Sidekiq Web UI; check Rails logs for request delays during job runs.
What to look for: Look for long job durations and increased request response times indicating blocking.