Resque

On this page 7

Resque is an open-source Ruby library for background job processing that stores jobs in Redis and executes them with separate worker processes. In job scheduling, Resque is a classic example of a queue-based worker system: an application pushes a small description of the work onto a named queue, and workers pull jobs and run them outside the web request cycle.

Resque was created at GitHub around 2009 to move slow tasks such as repository operations and notifications out of user-facing requests. Its defining design choice is the fork-per-job model, which trades throughput for strong isolation. Within the wider Automation subject, Resque belongs to the family of job queues alongside Sidekiq, Celery and BullMQ, and it influenced ports for PHP, Node.js and other languages.

How Resque queues and runs jobs

Resque works by serializing each job to JSON and pushing it onto a Redis list that represents a queue. A job is a Ruby class with a queue name and a class method named perform; enqueuing stores only the class name and its arguments, never the object itself.

A Resque worker is a long-running process that polls one or more queues in a defined priority order. When it pops a job, the parent process forks a child process, the child runs perform, and then exits. If the child crashes, leaks memory or is killed, the parent survives and continues with the next job.

The following table maps the main components to their responsibilities.

ComponentResponsibilityImplementation detail
Redis serverStores queues, worker state and failuresLists, sets and string keys
Job classDefines the work and target queueClass method perform with JSON arguments
Enqueuing codeAdds jobs from the applicationResque.enqueue call
Worker parentPolls queues and supervises childrenOne process per worker
Forked childExecutes a single jobExits after each job
Failure backendRecords exceptions and payloadsFailed list in Redis by default
Web dashboardShows queues, workers and failuresSinatra application, resque-web
Main components of a Resque deployment

Queues, priorities and worker configuration

Resque implements job priority through queue ordering rather than per-job priority values. A worker started with the queue list critical,default,low always empties critical before it checks default, so ordering is strict and lower queues can starve under sustained load.

Operators tune a small set of parameters that determine latency, throughput and resource use.

SettingTypical valueEffect
QUEUESOrdered list or wildcardDefines which queues a worker serves
INTERVAL1 to 5 secondsPolling delay when queues are empty
COUNT (with resque-pool)2 to 20 workers per hostSets parallelism on each machine
FORK_PER_JOBEnabled by defaultIsolation versus fork overhead
TERM_CHILDEnabled in productionGraceful shutdown of running jobs
Key Resque settings and their effects

Because concurrency comes from separate processes rather than threads, a host running ten workers needs memory for ten parent processes plus their children. Copy-on-write memory sharing after forking reduces this cost, but Ruby garbage collection gradually erodes the shared pages.

Scheduling, retries and the plugin ecosystem

Core Resque runs jobs as soon as a worker is free; it has no built-in timing, no automatic retries and no job dependencies. These features come from community plugins that hook into the job lifecycle.

  • resque-scheduler adds delayed jobs and recurring jobs defined with cron expressions, using a separate scheduler process.
  • resque-retry adds configurable retry counts and backoff before a job lands in the failed list.
  • resque-pool manages a pool of workers per host from a single configuration file.
  • Lifecycle hooks such as before_perform and around_perform enable locking, metrics and concurrency limits.

The scheduler process must be treated as a single point of coordination: running two schedulers without locking can enqueue each recurring job twice. Modern versions of resque-scheduler use a Redis-based master lock to elect one active instance.

Resque compared with other job queues

Resque is most often compared with Sidekiq, which uses the same Redis storage idea but runs jobs in threads inside one process. The comparison below highlights the trade-offs that usually decide between Ruby background job systems.

AspectResqueSidekiqDelayed Job
StorageRedisRedisRelational database table
Concurrency modelForked process per jobThreads in one processProcess polling the table
IsolationStrongShared memoryModerate
Throughput per hostLowerMuch higherLowest
Built-in retriesNo, plugin requiredYesYes
Thread safety neededNoYesNo
Resque and related background job systems

Resque also works as a backend for Active Job, the Rails abstraction for background work. This lets teams switch adapters without rewriting job classes, which is a common path when an application outgrows Resque throughput.

Operational risks and common mistakes

The most serious Resque risk is job loss during failures. When a worker pops a job from the list and the host dies before completion, the job is gone, because core Resque does not use reliable acknowledgment queues. Critical work therefore needs idempotent design and reconciliation checks.

SymptomLikely causeRemedy
Jobs vanish after a crashNo acknowledgment mechanismIdempotent jobs and periodic reconciliation
Low-priority queue never drainsStrict queue orderingDedicated workers for that queue
Growing failed listNo retry policyAdd resque-retry with backoff
Duplicate recurring jobsTwo active schedulersEnable the scheduler master lock
Jobs fail on stale recordsPassing objects instead of IDsPass identifiers and reload data
Typical problems, causes and fixes

Another frequent mistake is placing large payloads in job arguments. Because every argument is stored in Redis, jobs should carry identifiers rather than data, and Redis itself should be sized and persisted with care.

Put simply

Resque is a to-do list for a web application. When the application has a slow task, such as sending an email or resizing a photo, it writes a note on the list instead of making the visitor wait.

Separate helper programs, called workers, pick notes off the list one by one and do the work in the background. Each task runs in its own fresh copy of the helper, so one broken task cannot spoil the next.

In practice

An online magazine built with Ruby on Rails lets editors upload photo galleries of up to 200 images. Generating five image sizes per photo inside the upload request took several minutes and caused browser timeouts.

The team moved resizing into a Resque job that receives only the gallery ID. Uploads now finish in a few seconds, and three servers each run eight workers on an images queue, with a separate mail queue served first so notifications are never delayed. resque-scheduler runs a nightly job at 02:00 that cleans orphaned files, and resque-retry retries storage errors three times with growing delays.

The result is a responsive editing interface and a predictable processing time of about 90 seconds per large gallery. When a corrupted image crashes a child process, only that single job fails and appears in the dashboard for review.