Search code examples
sidekiq

Sidekiq: configure exponential backoff for all workers


By default Sidekiq will retry jobs 25 times with an exponential backoff, I am trying to change the configuration for both the default retry count and the default exponential backoff for all workers.

I see that you can change both of them at the class level with

sidekiq_options :retry
and
sidekiq_retry_in

class WorkerWithCustomRetry
  include Sidekiq::Worker
  sidekiq_options :retry => 5

  sidekiq_retry_in do |count|
    10 * (count + 1) # (i.e. 10, 20, 30, 40)
  end

  def perform(...)
  end
end

I would like to change these defaults for the application as a whole. Not add the overrides to every class. I found out how to change the default max retries using

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add Sidekiq::Middleware::Server::RetryJobs, :max_retries => 5
  end
end

However I cannot find any examples of how to change the exponential backoff globally.
Is this possible and if so how?

Thanks


Solution

  • The middleware configuration doesn't support that.

    Two options. One would be to extend the Server::RetryJobs middleware and override def seconds_to_delay there. Then use your customized middleware instead of the Server::RetryJobs that comes out of the box.

    Another solution would be to have all your workers include WorkerWithCustomRetry instead of Sidekiq::Worker. Basically, make a wrapper around the Sidekiq default worker that is just for your app. I slightly prefer this second option because it works with Sidekiq's public interface rather than redefining a private method from the middleware, which could change anytime without notice.