How do you handle background job failures and retries in Rails?
Answer
ActiveJob provides hooks for handling failures: rescue_from(StandardError) { |e| retry_job(wait: 5.minutes) }. Sidekiq has built-in retry logic with exponential backoff — it retries failed jobs up to 25 times by default, with increasing delays, then moves them to the Dead Job Queue (viewable in the Sidekiq Web UI). Custom retry logic: sidekiq_options retry: 5. For critical jobs, use idempotency — jobs should be safe to run multiple times without side effects (check if work was already done). For non-retriable jobs: sidekiq_options retry: false. Monitor with Sidekiq metrics, error tracking services (Sentry/Honeybadger), and alerting on Dead Queue size.
Previous
What is ActiveRecord's STI vs polymorphic associations — when to use each?
Next
What is database sharding in the context of Rails?
More Ruby on Rails Questions
View all →- Advanced What is metaprogramming in Ruby/Rails and give examples?
- Advanced What is the difference between include, extend, and prepend in Ruby?
- Advanced How does Rails handle database connection pooling?
- Advanced What is Rack and how does Rails relate to it?
- Advanced What is query optimization with explain in Rails?