简体   繁体   English

Redis Pub Sub:设计模式

[英]Redis Pub Sub : Design pattern

We are using socket i/o for lots of live data. 我们使用socket i / o来处理大量实时数据。 User sends/receive the data using sockets. 用户使用套接字发送/接收数据。 As we are using load balancer, we cannot use socket i/o's namespace model and instead using redis' pub/sub within socket. 当我们使用负载均衡器时,我们不能使用套接字i / o的命名空间模型,而是使用套接字中的redis'pub / sub。

Till now, we were creating a separate redis conection for subscription per user per channel. 到目前为止,我们正在为每个用户的每个频道创建一个单独的redis连接。 But recently we faced an issue that of max connection reached ( Error: Ready check failed: ERR max number of clients reached ) on redis and we figured out that it is because of having too many redis connections through pub sub. 但是最近我们遇到了redis上达到最大连接的问题( Error: Ready check failed: ERR max number of clients reached ),我们发现这是因为通过pub sub有太多的redis连接。

To counter that, it occurred to me that instead of using multiple subscribe redis connections per user, why not have a one publish redis connection and one subscribe redis connection which will listen to all channels and can be achieved by: 为了解决这个问题,我想到,不是每个用户使用多个订阅redis连接,为什么不使用一个发布redis连接和一个订阅redis连接,它将监听所有通道,并且可以通过以下方式实现:

var pub = redis.createClient();
var sub = redis.createClient();
sub.psubscribe('*');

sub will listen to all channels. sub将收听所有频道。 Also, we can store information about channels to which user is subscribe in socket object and handle data accordingly. 此外,我们可以存储有关用户在套接字对象中订阅的频道的信息,并相应地处理数据。

Hopefully, I am clear with the problem statement and would like to understand how will be the performance using this design pattern? 希望我对问题陈述很清楚,并想了解使用这种设计模式的性能如何?

Performance-wise it is much better to use a single connection, it's much less stress on the redis-server and when publishing data, it won't have to loop through all the connections. 在性能方面,使用单个连接要好得多,它对redis-server的压力要小得多,并且在发布数据时,它不必遍历所有连接。 The "pattern" does create some overhead but it's very negligible, make sure to be more specific about your patterns, you might be publishing other event on that server in the future. “模式”确实会产生一些开销,但它可以忽略不计,确保更具体地说明您的模式,您可能会在将来在该服务器上发布其他事件。

sub.psubscribe('someprefix_*');

By the way, if your problem is broadcasting to socket.io within the context of multiple NodeJS instances, you could check out this module: https://github.com/socketio/socket.io-redis 顺便说一句,如果您的问题是在多个NodeJS实例的上下文中广播到socket.io,您可以查看此模块: https//github.com/socketio/socket.io-redis

It leverages redis to provide a scale,multi-node experience with socket.io 它利用redis为socket.io提供扩展的多节点体验

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM