Redis Pub/Sub 和 Redis Stream 之间的主要区别是什么? [英] What are the main differences between Redis Pub/Sub and Redis Stream?
问题描述
各自的优缺点是什么?请建议何时使用一种而不是另一种.
What are the pros and cons of each? Please advice when to use one and not the other.
推荐答案
数据存储
Pub/Sub 是一个发布者/订阅者平台,它不是数据存储.无论是否有订阅者,已发布的消息都会消失.
Data storage
Pub/Sub is a Publisher/Subscriber platform, it's not data storage. Published messages evaporate, regardless if there was any subscriber.
在Redis Streams中,流是一种数据类型,本身就是一种数据结构.消息或条目存储在内存中并一直留在那里,直到被命令删除.
In Redis Streams, stream is a data type, a data structure on its own right. Messages or entries are stored in memory and stay there until commanded to be deleted.
Pub/Sub 是同步通信(推送 协议).各方需要同时处于活动状态才能进行通信.这里Redis是一个纯粹的同步消息代理.
Pub/Sub is synchronous communication (push protocol). All parties need to be active at the same time to be able to communicate. Here Redis is a pure synchronous messaging broker.
Redis Streams 允许同步(XREAD
with BLOCK
和特殊的 $
ID 是一个 push 协议) 和异步通信(常规的 XREAD
是一个 pull 协议).XREAD
和 BLOCK
类似于 Pub/Sub,但能够在断开连接时恢复而不会丢失消息.
Redis Streams allow for both synchronous (XREAD
with BLOCK
and the special $
ID is a push protocol) and asynchronous communication (regular XREAD
is a pull protocol). XREAD
with BLOCK
is like Pub/Sub, but with the ability to resume on disconnection without losing messages.
Pub/Sub 是最多一次,即即发即忘".
Pub/Sub is At-most-once, i.e. "fire and forget".
Redis Streams 允许 At-most-once 或 At-least-once(接收者发送的显式确认)
Redis Streams allows for both At-most-once or At-least-once (explicit acknowledgement sent by the receiver)
Pub/Sub 仅为阻塞模式.订阅频道后,客户端将进入订阅模式,并且无法发出命令(除了[P]SUBSCRIBE
、[P]UNSUBSCRIBE
、PING
和 QUIT
),它变成了只读.
Pub/Sub is blocking-mode only. Once subscribed to a channel, the client is put into subscriber mode and it cannot issue commands (except for [P]SUBSCRIBE
, [P]UNSUBSCRIBE
, PING
and QUIT
), it has become read-only.
Redis Streams 允许消费者以阻塞模式或不阻塞模式读取消息.
Redis Streams allows consumers to read messages in blocking mode or not.
Pub/Sub 仅支持扇出.所有活动的客户端都会收到所有消息.
Pub/Sub is fan-out only. All active clients get all messages.
Redis Streams 允许扇出(使用 XREAD
),但也从同一流向多个客户端提供不同的消息子集.这允许通过将不同的消息路由到不同的工作人员来扩展消息处理,以一种不可能将相同的消息传递给多个消费者的方式.最后一个场景是通过消费者群体实现的.
Redis Streams allows fan-out (with XREAD
), but also to provide a different subset of messages from the same stream to many clients. This allows scaling message processing, by routing different messages to different workers, in a way that it is not possible that the same message is delivered to multiple consumers. This last scenario is achieved with consumer groups.
Redis Streams 提供了更多功能,例如时间戳、字段值对、范围等.这并不意味着您应该始终使用 Streams.如果您的用例可以通过 Pub/Sub 实现,那么您最好使用 Pub/Sub.使用 Streams,您必须注意内存使用情况.
Redis Streams provide many more features, like time-stamps, field-value pairs, ranges, etc. It doesn't mean you should always go for Streams. If your use-case can be achieved with Pub/Sub, it is better for you to use Pub/Sub then. With Streams, you have to care for memory usage.
这篇关于Redis Pub/Sub 和 Redis Stream 之间的主要区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!