Rails Sidekiq: Job executes on the wrong service

Pagorn Phusaisakul
2 min readDec 13, 2021

It might be relates to our Sidekiq config, Redis URL.

Image by Buck Buckley from Pixabay

We shouldn’t use duplicate URLs if we are running multiple Sidekiq services on the same Redis service. We need to configure Redis URL with a different number for each service

Solution

Let’s say we have Rails Service A and Rails Service B.

Rails Service A, we set 0 to the url

# config/initializers/sidekiq.rbSidekiq.configure_server do |config|
config.redis = { url: 'redis://localhost:6379/0' }
end

Sidekiq.configure_client do |config|
config.redis = { url: 'redis://localhost:6379/0' }
end

Rails Service B, we set 1 to the url

# config/initializers/sidekiq.rbSidekiq.configure_server do |config|
config.redis = { url: 'redis://localhost:6379/1' }
end

Sidekiq.configure_client do |config|
config.redis = { url: 'redis://localhost:6379/1' }
end

Which number we should use?

The default range of numbers is 0–15 depending on the database’s index setting in redis.conf

To check range of numbers

$ cat /etc/redis/redis.conf | grep databases -C 5 

Result:

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16

databases 16 means we can set redis url to 0–15

  • redis://localhost:6379/0
  • redis://localhost:6379/1
  • redis://localhost:6379/2
  • redis://localhost:6379/3
  • redis://localhost:6379/15

So, pick one and make sure it isn’t used by another Rails service 😉

--

--