How to use Sidekiq in rails for background processing?

tudip-logo

Tudip

19 June 2020

What is Sidekiq?

Sidekiq is a Ruby-written open source job scheduler. It is important to be aware that Sidekiq does not do scheduling by default, it only performs jobs. The Enterprise version comes with outbox scheduling.

What is a background Job?

A context or asynchronous job is one that is done beyond the normal request/response workflow which is part of any modern web system. Web applications normally receive an external request, do some processing and return a response within a few milliseconds. This is the normal pattern we’ve all become used to when creating web applications, and it’s called synchronous communication.

Why do we use Sidekiq in rails applications?

Ruby, Sidekiq uses simple, efficient background processing to handle many jobs simultaneously in the same process using threads. It uses the exact same message format as Resque so that it can be integrated into an existing processing farm. At the same time, you can have Sidekiq and Rescue run side by side and use the Rescue client to enquire about jobs in Redis to be handled by Sidekiq. At the same time, Sidekiq uses multithreading so it is much more memory-efficient than Rescue.

Execution of Sidekiq in rails application

sidekiq_01

  • First, the Controller creates an asynchronous process and that process details store the queue.
  • Queue is a data store that is similar to a data structure.
  • Worker is similar to a ruby object so it is deployed to another object to handle the ruby process.
  • Multiple Workers are executed parallelly if they perform async use during the worker call.
  • After completing the process, it will be generated to an HTTP request.

How to Implement Sidekiq in rails application?

  1. Add sidekiq to your Gemfile:
    sidekiq_02
  2. Then run ‘bundle install’
  3. Then run ‘rails g sidekiq:worker HardWorker’ and auto create worker file
    sidekiq_03
  4. Create a job to be processed asynchronously:
    sidekiq_04
  5. Start sidekiq from the root of your Rails application so the jobs will be processed:
    sidekiq_05
  6. Start sidekiq from the root of your Rails application so the jobs will be processed:
    sidekiq_06

Request a quote