Lazy Resque
Move Resque enqueues out of the Rails request cycle to help decrease time to first byte.
Install
From gem:
gem 'lazy_resque'
From source:
gem 'lazy_resque', git: '[email protected]:samuelgiles/lazy_resque.git'
Use
Trigger the actual enqueue of the jobs after the request in an after_action
block, this can be done
by including LazyResque::ControllerEnqueue
which will automatically add the after_action
for you.
class ApplicationController < ActionController::Base
include LazyResque::ControllerEnqueue
def index
Resque.lazy_enqueue(MyResqueJob, 1234, 'some_job_data')
end
end
Why?
Though moving a long running task to a Resque job is beneficial the actual enqueuing process isn't free:
- Any
before_enqueue
hooks are run - A new job instance is initialized and validated
- Arguments are dumped/encoded into JSON
- Redis
rpush
takes places requiring communication with the Redis server
This is fine for 1-2 enqueues in a request cycle but can add up to a measurable amount of time if your enqueuing several jobs.
You don't lose anything by moving the actual enqueuing outside the request cycle and it saves precious milliseconds.