Before starting with setup process lets first briefly go through what Sidekiq
is? and how it works?
Sidekiq provides very handy and efficient way to perform most of our work asynchronously in a job. Provided Redis cache connection to it, it serializes all job parameters in Redis and deserializes it while picking it up for execution based on priority of job as it uses queue data structure to store each job in particular queue specified in job and each queue has its priority defined as high, medium, low etc.
To setup Sidekiq with Redis in your application you can follow following basic steps and in addition to it you can also go through Sidekiq gem wiki link provided at bottom for better understanding.
-
Add Sidekiq and Redis gem to your
Gemfile
-gem 'sidekiq'
gem 'redis'
-
To use Redis for caching server for Sidekiq you have to specify Redis server url to Sidekiq. For that first define Redis url for all your environment preferably through environment key and for developement environment specify Redis server url environment variable in
development.rb
-ENV['REDIS_SERVER_URL'] = 'redis://localhost:6389'
-
For
Staging
andProduction
you can set above environment variable to Redis url you want to point to in respective environment. -
Setup URL for applications Redis instance inside
initializers > redis.rb
-uri = URI.parse(ENV["REDIS_SERVER_URL"])
Redis.current = Redis.new(:uri => uri)
-
Setup Sidekiq to point to use Redis in
sidekiq.rb
-Sidekiq.configure_server do |config| config.redis = { url: ENV["REDIS_SERVER_URL"] } end Sidekiq.configure_client do |config| config.redis = { url: ENV["REDIS_SERVER_URL"] } end
-
There are also other things need to be considered like how many number of Redis connections should be allowed
to Sidekiq. You can find more information about it here