Recipe

Sidekiq Primer

Sidekiq is a battle-tested background job processor for Ruby applications. It pulls jobs from Redis and runs them in worker threads, letting your web layer stay snappy while heavy work happens off the request path. This primer walks through wiring Sidekiq into a Meridian-backed service so your queues, retries, and observability all line up.

1. Install and configure

Add the gem to your Gemfile, then point Sidekiq at the Redis instance Meridian provisions for you. The connection pool size should track your worker concurrency plus a small buffer for ad-hoc enqueues.

# Gemfile
gem "sidekiq", "~> 7.2"

# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
  config.redis = { url: ENV.fetch("MERIDIAN_REDIS_URL"),
                   size: 12 }
end

Sidekiq.configure_client do |config|
  config.redis = { url: ENV.fetch("MERIDIAN_REDIS_URL"),
                   size: 4 }
end

2. Define a worker

A worker is a plain Ruby class that includes Sidekiq::Job and implements perform. Keep arguments to JSON-safe primitives (ids, strings, numbers) so they survive the round trip through Redis. Side effects live inside the method body, never in class-level state.

Tip: name the queue explicitly with sidekiq_options queue: :criticalso the Meridian dashboard can chart throughput per pipeline.

3. Enqueue and observe

Call YourWorker.perform_async(id) from a controller, callback, or another job. Meridian streams Sidekiq metrics into the same panel as your HTTP traces, so a slow job shows up beside the request that scheduled it. Retries default to 25 attempts with exponential backoff; tune sidekiq_options retry: per worker if you need stricter semantics.

  • Use perform_in(30.seconds, id) for short delays.
  • Use perform_at(time, id) for wall-clock scheduling.
  • Batch related jobs with Sidekiq Pro's Batch API.