Using multiple sidekiqs on the same Rails app

Tram Ho

Sidekiq threads are assumed to be connected to a single redis. So when connecting the application to multiple redis, the number of sidekiqs needed corresponds to the number of redis. This article guides to install multiple sidekiq on the same rails application in the most concise way

config/initializers/sidekiq.rb

First you need to understand what is Sidekiq server and sidekiq client?

  • client: is where jobs are submitted to and queued (also known as Rails applications)
  • server: is where jobs are retrieved (dequeue) for processing (Sidekiq process)

Sidekiq config always includes Sidekiq server and Sidekiq client. Usually the sidekiq_servers are located on different instances, so it is necessary to set the correct value of REDIS_SERVERNAME at these instances for example:

  • sidekiq_1:

  • sidekiq_2:

  • rails:

config/sidekiq.yml

This is where the list of queues used in sidekiq is defined. Each sidekiq process needs to define a separate sidekiq.yml file. eg: sidekiq.yml and sidekiq_1.yml

Definition of worker for sidekiq_1

The rails application will call this worker’s method to send jobs to sidekiq.

Definition of worker for sidekiq_2

At this point, some of you may wonder why in step 1 we defined 2 variables in the rails instance but sidekiq_client only used 1 value. The reason is that we can choose a sidekiq as the default for rails applications. Usually we will choose which sidekiq has more queue types, and in sidekiq_options we just need to name the queue. For sidekiq_2, we need to define which sidekiq this worker uses via the ConnectionPool class.

Conclusion

With that, we can configure our rails application to use multiple sidekiq processes! Thank you to everyone who visited this post! Looking forward to hearing from everyone.

Share the news now

Source : Viblo