Message Queues Part 2: The PubSub Protocol

综合编程 2016-06-02

The Publisher-Subscriber model in a nutshell

What is PubSub exactly?

PubSub is a communication topology where a single entity called Publisher produces messages that it sends out to other entities called Subscribers. Subscribers may receive everything the publisher sends, or they may subscribe to message subsets called Topics.

This sounds much like some news service sending articles out to readers, and in fact many news services work this way. Whether you subscribe to an RSS feed, to one or more forums on a discussions platform, or follow someone on Twitter–in each case, there is one publisher and multiple subscribers involved.

Where can a PubSub topology be used?

Typically, PubSub addresses scenarios like these:

  • Multiple observers need to act upon status changes of a single entity.
  • Multiple workers shall process data from a single entity. Results are not sent back to that entity.

Implementation variations

Subscription strategies can differ based on the system architecture.

  • If the system has a broker , clients subscribe to the broker rather than to the server, and the broker takes care of routing the messages to the clients based on the subscribed topics.
  • In brokerless systems, clients may either send their topics to the server, and the server then sends each message only to the clients that have subscribed for that topic.
  • Or the clients filter the messages at their end. The server then simply sends all messages to all clients. (This approach is fine in smaller, local scenarios but does not scale well.)

How Mangos implements the PubSub protocol

As seen in the Pair example in the previous article, Mangos uses special, protocol-aware sockets. In a PubSub scenario, the “pub” socket just sends out its messages to all receivers (or to nirvana if no one is listening). The “sub” socket is able to filter the incoming messages by topic and only delivers the messages that match one of the subscribed topics.

The animation below shows the Mangos approach - the publisher sends all messages to all subscribers, and the subscribers filter the messages according to the topics they have subscribed to:

This is certainly a rather simple and robust approach, as the server does not need to manage the clients and their subscriptions; on the downside, as noted above, filtering on client side does not scale well with the number of clients.

A PubSub example

Let’s dive into coding now. We’ll develop a tiny example where a couple of clients subscribe to a few topics, and the server then publishes some messages by topic.

But first, let’s do the installation stuff.

Installing Mangos and importing the packages

Like in the previous post, Mangos is installed via a simple go get:

go get -u

Now you can import Mangos into your .go file.

The code

责编内容by:Applied Go (源链)。感谢您的支持!


深度 | 金融级消息队列的演进 — 蚂蚁金服的实践之路... 消息队列作为一个数据的集散中心,承载了越来越多的场景和数据,从最开始的 OLTP 到 OLAP,甚至再到物联网、人工智能、机器学习等场景,都有很大的想像空间。 在能力上,消息队列现在拥有了数据,拥有了算力,从承载数据走到理解数据。 蚂蚁金服也在思考给消息队列加入算法的能力,让算法...
如何设计出一个“前浪微博”? 本文 来自微信公众号“InfoQ”(ID:infoqchina),作者:李运华,编辑:何潇,虎嗅获授权转载。 让我们结合复杂度来源和架构设计原则,通过一个模拟的设计场景“前浪微博”,和你一起看看在实践中究竟如何进行架构设计。 我们假想一个创业公司,名称叫作“前浪微博...
Android消息处理机制 messages Hi~ 我们来说说Android的消息处理机制 消息处理机制 相关类 Message:消息,其中包含了消息ID,处理对象和处理数据,由MessageQueue统一队列管理,最终由Handler处理。 Handler:负责Messa...
分布式消息系统如何解决消息的顺序&重复两大硬伤?... 分布式消息系统作为实现分布式系统可扩展、可伸缩性的关键组件,需要具有高吞吐量、高可用等特点。而谈到消息系统的设计,就回避不了两个问题: 消息的顺序问题 消息的重复问题 RocketMQ作为阿里开源的一款高性能、高吞吐量的消息中间件,它是怎样来解决这两个问题的?R...
最简洁的Handler介绍 最简洁系列介绍:对于理科生来说有条理的知识结构更便于记忆和学习与理解,比起无比详细的文章和成段的代码,知识框架更容易分析问题和定位问题。这系列的文章现在还在尝试阶段,如果您有好的意见,欢迎交流! 一、解决问题 1、理解Handler原理; 2、一个线程里面可以有多少个Looper,多少个...