Meltemi
Meltemi

Reputation: 38359

Configuring Redis + Sidekiq in production

I'm trying to set up Redis + Sidekiq in a Passenger/Rails production environment and have run into some confusion.

The server has a number of small-ish apps running on it. I'm certain that more than one of these apps will leverage the delayed processing offered by Sidekiq.

My first thought was to use the namespace option in Sidekiq to create a namespace for each Rails app.

But then I noticed a databases 16 setting in redis.conf and I'm wondering what that does, exactly. I can't seem to find documentation on it other than the comments in the config:

# 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

So, wondering if the default/example of 16 'databases' means that I could use DB0 for one app and DB1 for another app?!? I don't think this assumption is correct but I can't find more information other than:

Redis Cluster does not support multiple databases like the stand alone version of Redis, there is just database 0, and SELECT is not allowed.

Hoping someone can advise me on how to share Sidekiq & Redis among various Rails applications running on same server.

Upvotes: 4

Views: 7663

Answers (3)

Dmitry Polyakovsky
Dmitry Polyakovsky

Reputation: 1585

databases 16 in redis.conf simply sets the maximum number of databases Redis instance has (0-15). You can change it if you want. I am using Redis DB0 for Rails caching and DB1 for Sidekiq (in addition to using namespaces). Just makes it cleaner in case I need to FLUSHDB.

In your case I would use separate Redis DBs for separate apps. If need to, just increase the number of databases.

Upvotes: 0

Simptive
Simptive

Reputation: 987

You can use multiple Redis Databases; each one associated with a Rails App on a single machine. You can put a file sidekiq.rb in initializers, with the following code:

app_name = Rails.application.class.parent_name

app_num = case app_name
  when 'AppOne'
    0
  when 'AppTwo'
    1
  when 'AppOne'
    2
  end

Redis.new(db: app_num) # existing DB is selected if already present

Sidekiq.configure_server do |config|
  config.redis = { url: "redis://localhost:6379/#{app_num}", namespace: "#{app_name}" }
end

Sidekiq.configure_client do |config|
  config.redis = { url: "redis://localhost:6379/#{app_num}", namespace: "#{app_name}" }
end

In this way, You are separating out Redis DBs as well as namespaces by Sidekiq processes.

Upvotes: 5

d_ethier
d_ethier

Reputation: 3911

I think you're looking for namespaces. You can configure a server/client configuration with sidekiq to use different namespaces and configure Passenger/Unicorn to use a different namespace. I've done it with Unicorn, but there should be an equivalent option in Passenger.

You can find the documentation on how to do this here.

Upvotes: 2

Related Questions