为什么我不能有一个 Redis 客户端充当同一连接中的 PUB 和 Sub? [英] Why can't I have a single Redis client acting as PUB and Sub in the same connection?

查看:58
本文介绍了为什么我不能有一个 Redis 客户端充当同一连接中的 PUB 和 Sub?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的心智模型是聊天",我订阅了某个频道,我可以向该频道发布消息.

My mental model was that of a 'chat', where I am subscribed to a certain channel and I can publish messages to this channel.

推荐答案

由于 pub/sub 是异步的,发布的消息可能会在任何时候出现,包括在您期待对命令的响应时.

Since pub/sub is asynchronous, the published message could appear at any time, including when you are expecting the response to a command.

尽管 redis 是单线程的,这通常可以防止这种事情,但网络延迟会导致一些有趣的影响 - 根据消息的内容,您可能会在服务器实际收到命令之前收到对命令的有效响应.

Although redis is single threaded, which normally prevents that sort of thing, network latency can cause some interesting effects - depending on the content of the messages, you could receive a valid response to a command before the server has actually received it.

也就是说,如果您真的想要,您可能可以使用单个连接 - 不应该"与不能"不同,并且 redis 遵循一个简单的设计理念,即不试图阻止您将自己射入脚.但是,只打开两个到服务器的连接要容易得多.如果您达到每个客户端两个连接的连接限制,无论如何您很快就会遇到每个客户端一个连接的问题.

That said, you could probably use a single connection if you really wanted to - "should not" is not the same as "can not", and redis follows a simple design philosophy of not trying to prevent you from shooting yourself in the foot. However, it is a lot easier to just open two connections to the server. If you hit connection limits with two connections per client you will probably run into problems with one connection per client fairly soon anyway.

这篇关于为什么我不能有一个 Redis 客户端充当同一连接中的 PUB 和 Sub?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆