2

I am using Sidekiq for the first time in a Rails app. This is also my first time using Redis.

I have seen several examples (Here, here, here) where initializers/sidekiq.rb contains the following lines:

Sidekiq.configure_server do |config|
    config.redis = { url: 'redis://localhost:6379/0', namespace: "sidekiq_app_name_#{Rails.env}" }
end

Sidekiq.configure_client do |config|
    config.redis = { url: 'redis://localhost:6379/0', namespace: "sidekiq_app_name_#{Rails.env}" }
end 

I haven't yet been able to find any documentation explaining exactly what is going on here.

I understand that a hash is being assigned to config.redis. The hash has a url pointing to the redis server, and a namespace prefixed with sidekiq_app_name_ followed by the current environment. I assume the prefix can be any string of my choosing -- most probably the app of my app.

What I don't understand is, why is the line repeated?

Why is the line repeated?

It's exactly the same. Surely Rails.env returns the same thing on each occasion? Is it a typo or does the duplication have an effect? If so, what is the point of the duplication?

user3574603
  • 3,364
  • 3
  • 24
  • 59
  • 2
    Author of Sidekiq here, I hope you learn and enjoy using it. Client and server are explained here: https://github.com/mperham/sidekiq/wiki/The-Basics I strongly recommend against using namespaces. https://www.mikeperham.com/2017/04/10/migrating-from-redis-namespace/ – Mike Perham Oct 02 '18 at 15:44

1 Answers1

4

There's a Sidekiq server library and a Sidekiq client library, and they are configured independently. The server is responsible for popping jobs off the queue(s) and executing them. The client is responsible for adding jobs to the queue. For example, the call MyWorker.perform_in(5.minutes) uses the client library, not the server library.

Sidekiq doesn't assume that that your client and server are colocated on the same host, or even in the same codebase. You may want your Sidekiq jobs executing on one host (or group of hosts), with Redis running locally, and your Rails app running on another host (or group of hosts). In that case, you would use a localhost address in the server config and a remote address in the client config. If your codebases are separate, you may not have Rails in the server codebase, so you'd have to configure Redis without Rails.env.

If you plan to run everything together on the same host, it's easy enough to deduplicate the code:

REDIS_CONFIG = { url: 'redis://localhost:6379/0', namespace: "sidekiq_app_name_#{Rails.env}" }

Sidekiq.configure_server {|config| config.redis = REDIS_CONFIG }
Sidekiq.configure_client {|config| config.redis = REDIS_CONFIG }

Or:

sidekiq_config = proc do |config|
  config.redis = { url: 'redis://localhost:6379/0', namespace: "sidekiq_app_name_#{Rails.env}" }
end

Sidekiq.configure_server &sidekiq_config
Sidekiq.configure_client &sidekiq_config

Or you can just set REDIS_PROVIDER or REDIS_URL in the parent environment and skip the in-Ruby configure step altogether!

export REDIS_URL="redis://localhost:6379/0/sidekiq_app_name_${APP_ENV}"
mwp
  • 8,217
  • 20
  • 26