Yet another Redis PubSub adapter for Phoenix. Supports sharding across multiple redis nodes.
The original phoenix_pubsub_redis will subscribe to a single topic (the namespace) from all Phoenix nodes. Whatever you publish, the message will be sent to all your nodes. So when you have a large number of nodes, it will become very inefficient. The single Redis instance will become the bottleneck. And because there is only a single topic, it is impossible to scale it.
So we have made this adapter, which will subscribe to the specific topic of the Phoenix channel when creating it, and unsubscribe to that topic after the Phoenix channel is shut down. If you publish something to a topic, the message will only be sent to the nodes which have the Phoenix channels subscribing to that topic.
Also, we have added a feature for sharding, based on the topics. You can simply do a load balancing by adding extra Redis instances.
# mix.exs
def deps do
[
{:phoenix_pubsub_redis_z, "~> 0.4"}
]
end
Add it to your Application's Supervisor tree:
# application.ex
children = [
{
Phoenix.PubSub,
name: MyApp.PubSub,
adapter: Phoenix.PubSub.RedisZ,
redis_urls: ["redis://redis01:6379/0", "redis://redis02:6379/0"]
}
# ...
]
Use the ChannelDecorator
in your channel entrypoint:
# lib/xxx_web.ex
def channel do
quote do
use Phoenix.Channel
use Phoenix.PubSub.RedisZ.ChannelDecorator # <= add this line
# ...
end
end
Option | Description | Default |
---|---|---|
:name |
The required name to register the PubSub processes, ie: MyApp.PubSub |
|
:redis_urls |
The required redis-server URL list | |
:node_name |
The name of the node | node() |
:local_pool_size |
The pool size of local subscription server | 2 |
:publisher_pool_size |
The pool size of redis publish connections for each redis-server | 8 |
:compression_level |
Compression level applied to serialized terms (0 - none, 9 - highest) | 0 |