0

I am currently trying to improve my Redis configuration by allowing a Redis client pool to my Rails 6 app as per this thread What is the best way to use Redis in a Multi-threaded Rails environment? (Puma / Sidekiq)

Though before implementing the above I am trying to name the only Redis client my Rails app is sharing and can't manage to make this work.

redis.yml

development:
  url: redis://localhost:6379/
  db: 0
  id: my_rails_redis

production:
  url: 
  db: 0

test: 
  url: redis://localhost:6379/
  db: 1  

redis.rb

conf = Rails.application.config_for(:redis)
redis = Redis.new(conf)

Everything works fine. My calls to Redis.current are working properly yet in redis-cli when I type client list I get the following list :

id=57 addr=127.0.0.1:60754 fd=19 name= age=1099 idle=422 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=hget
id=58 addr=127.0.0.1:60756 fd=20 name=ActionCable-PID-18018 age=1099 idle=422 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=publish
id=47 addr=127.0.0.1:60702 fd=8 name= age=1110 idle=1 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=brpop
id=48 addr=127.0.0.1:60704 fd=9 name= age=1110 idle=1 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=brpop
id=65 addr=127.0.0.1:32896 fd=17 name=ActionCable-PID-19465 age=460 idle=422 flags=N db=0 sub=3 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=49 addr=127.0.0.1:60706 fd=10 name= age=1110 idle=1 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=brpop
id=50 addr=127.0.0.1:60708 fd=11 name= age=1110 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=scard
id=63 addr=127.0.0.1:32784 fd=15 name= age=472 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
id=66 addr=127.0.0.1:32994 fd=18 name= age=424 idle=424 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=hget
id=67 addr=127.0.0.1:32996 fd=21 name=Sidekiq-server-PID-19455 age=424 idle=424 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=exec
id=64 addr=127.0.0.1:32790 fd=16 name= age=460 idle=447 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=hvals
id=62 addr=127.0.0.1:32782 fd=14 name=ActionCable-PID-19477 age=474 idle=460 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=unsubscribe
id=51 addr=127.0.0.1:60710 fd=12 name= age=1110 idle=1 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=brpop
id=52 addr=127.0.0.1:60712 fd=13 name= age=1110 idle=1 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=brpop

with clients belonging to Actioncable or sidekiq being flagged properly though I can't see any client called my_rails_redis.

Am I missing something ?

Maxence
  • 2,029
  • 4
  • 18
  • 37

1 Answers1

0

My mistake after further Puma restart it seems to have picked it up.

The result is pretty funny as I expected to have a single Redis client created but it looks like this now :

id=79 addr=127.0.0.1:33576 fd=19 name=Sidekiq-server-PID-21977 age=5 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=exec
id=73 addr=127.0.0.1:33458 fd=11 name=my_rails_redis age=17 idle=2 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=brpop
id=74 addr=127.0.0.1:33460 fd=12 name=my_rails_redis age=17 idle=0 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=brpop
id=63 addr=127.0.0.1:32784 fd=15 name= age=1829 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
id=69 addr=127.0.0.1:33450 fd=14 name=ActionCable-PID-21999 age=34 idle=14 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=unsubscribe
id=80 addr=127.0.0.1:33586 fd=20 name= age=4 idle=3 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=hget
id=81 addr=127.0.0.1:33588 fd=21 name=ActionCable-PID-22041 age=4 idle=3 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=publish
id=77 addr=127.0.0.1:33568 fd=17 name=my_rails_redis age=12 idle=2 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=exec
id=70 addr=127.0.0.1:33452 fd=8 name=my_rails_redis age=17 idle=1 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=brpop
id=71 addr=127.0.0.1:33454 fd=9 name=my_rails_redis age=17 idle=2 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=brpop
id=76 addr=127.0.0.1:33566 fd=16 name=ActionCable-PID-21987 age=13 idle=3 flags=N db=0 sub=3 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=75 addr=127.0.0.1:33464 fd=13 name= age=14 idle=14 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=hvals
id=72 addr=127.0.0.1:33456 fd=10 name=my_rails_redis age=17 idle=2 flags=b db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=brpop
id=78 addr=127.0.0.1:33574 fd=18 name= age=5 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=hget

I am not even sure I need to create a pool if the only created Redis client redis = Redis.new(conf) turns into many different client ..

Mystery to be dug further ..

EDIT for those who will ever get the same problem as me :

Actually I used my redis.yml conf into Sidekiq. Eventhough some Sidekiq redis clients were named, some others werent (the server part of the Sidekiq.rb config file).

then I removed id: my_rails_redis from redis.yml not to spoil every config file using Redis config

and I changed Sidekiq.rb as per below :

conf = Rails.application.config_for(:redis)
confid = conf.merge({id: "Sidekiq-server-PID-#{::Process.pid}"})
confservid = conf.merge({id: "Sidekiq-server"})

if Rails.env.development?
  Sidekiq.configure_client do |config|         
    config.redis = confid       
  end

  Sidekiq.configure_server do |config|    
    config.redis = confservid    
  end

end

Which happened to name EVERY Sidekiq Redis client.

Also, the key of the problem, in redis.rb I was initializing a Redis client though I was not using it in my code as it was assigned a local instance ("redis") :

conf = Rails.application.config_for(:redis)
conf[:id] = "Rails_redis_client"
redis = Redis.new(conf)

Actually that's true: I was using Redis.current in my code. redis not being available.

Then switching to a constant :

Red_g = Redis.new(conf)

And using Red_g (for Redis_global ) instead of Redis.current happened to fix my problem: naming work.

Maxence
  • 2,029
  • 4
  • 18
  • 37