[feature] Add sentinel support to ActionCable Redis subscription adapter

498 views
Skip to first unread message

Alistair Israel

unread,
Apr 26, 2017, 2:00:40 PM4/26/17
to Ruby on Rails: Core
I've been trying to deploy a small, sample Rails 5 app that uses ActionCable to send some real-time updates via WebSockets to the front-end Javascript. It works fine when running locally, or on single node test environments running single-node Redis as well.

When I tried to deploy it on to Kubernetes, however, I ran into a small stumbling block because to deploy Redis on to Kubernetes in a highly available manner it seems the recommended way is through use of Redis Sentinels:

* https://redis.io/topics/sentinel

redis-rails already supports sentinels, so use of Redis with sentinels for cache, sessions, etc. is not a problem: https://github.com/redis-store/redis-rails#usage-with-redis-sentinel

However, the ActionCable Redis subscription adapter is configured independently, and when I dived into the code it became apparent that the configuration did not currently support specifying Redis sentinel configuration (even though the underlying redis gem powering both ActionCable and redis-rails already does).

It looks like it should only take a couple of lines of additional code at https://github.com/rails/rails/blob/master/actioncable/lib/action_cable/subscription_adapter/redis.rb#L13 to support an additional `sentinels: [...]` configuration parameter (if present).

I realize that I could simply supply my own custom lambda to ActionCable::SubscriptionAdapter::Redis.redis_connector in an initializer to support sentinels there, but am wondering if this is something the rest of the community might appreciate. If so, I'll gladly raise a PR.

Cheers!
- alistair

bl...@kolide.co

unread,
Aug 25, 2017, 10:41:27 AM8/25/17
to Ruby on Rails: Core
Hi Alistair, 
This is something that I need, care to share any pointers about how you solved this?

Thanks, 
- Blaed

Alistair Israel

unread,
Apr 27, 2018, 9:39:31 PM4/27/18
to Ruby on Rails: Core
Hi, Blaed. Apologies that I only saw this now (since Google groups doesn't actually notify you if someone replies to your email thread).

Anyway, yes, it's relatively easy. I ended up just supplying a custom lambda to ActionCable::SubscriptionAdapter::Redis.redis_connector. I made a file named config/initializers/action_cable.rb and in there wrote:

require 'action_cable/subscription_adapter/redis'

ActionCable::SubscriptionAdapter::Redis.redis_connector = lambda do |config|
  redis_opts
= { url: config[:url] }
  redis_opts
[:sentinels] = config[:sentinels] if config.key?(:sentinels)
 
::Redis.new(redis_opts)
end


Basically, check if an optional "sentinels" key was placed in config/cable.yml, and use that to configure the Redis connector accordingly. For example:

production:
  adapter
: redis
  url
: redis://mymaster
  sentinels
:
 
- host: redis-sentinel
    port
: 26379


Hope this helps!

Blaed Johnston

unread,
Jul 10, 2018, 9:40:53 AM7/10/18
to rubyonra...@googlegroups.com
Hey Alistair, 
No problem at all! That was nearly the exact solution I ended up using, so hopefully this is helpful to any other googlers

--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/-Wrpc51Es4E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

z...@useracquisition.com

unread,
Nov 28, 2018, 6:09:42 PM11/28/18
to Ruby on Rails: Core
This is fantastic, thank you both!
Reply all
Reply to author
Forward
0 new messages