loading...

April 13, 2020

Asynchronous Communication – Difference Between Queue and Topic

In his book Cloud Architecture Patterns (O’Reilly Media, 2010), Bill Wilder described important characteristics for cloud-native app development. Amongst others, cloud-native apps feature an asynchronous communication. Asynchronous communication means that messages are either published on a topic or send to a queue. This enables reactive programming through famous message brokers like RabbitMQ or Apache Kafka. But what are ‘queue’ and ‘topic’? This article describes the differences between asynchronous messaging via a topic or a queue.

Synchronous Communication

Synchronous communication is based on the request-reply pattern. A (micro)service calls another specific (micro)service. Then, the calling (micro)service waits for a response. After receiving response of the called (micro)service, it goes on with its process. This communication has a blocking characteristic which means that the caller cannot continue its process until it received the response.

Asynchronous Communication

Asynchronous communication is non-blocking. This means that a (micro)service sends a message and goes on with its process without waiting for a response of the called (micro)service(s). For asynchronous communication, responses are not sent. Moreover, the caller does not know the callee(s). Several (micro)services could subscribe/register to receive messages of topics/queues. The callees can react to the published/sent messages. These messages are often also called events.

Topic

When programmers talk about communication via topics, they often talk about the publish-subscribe pattern. This pattern is also typical for the Web Application Messaging Protocol. Multiple subscribers could subscribe to a topic. When any service publishes a message on this topic, the message will get delivered to all subscribers.

Asynchronous communication via a topic disseminates messages of publishers to multiple subscribers.
Asynchronous Communication: Topic

Queue

Asynchronous communication via a queue works similarly: Several consumers register for a queue. A queue would then disseminate each message in it to only one consumer. This means that a queue could often be used to dispatch tasks to a consumer who is available and has no work.

Asynchronous communication via a queue disseminates each message send to it to one consumer.
Asynchronous Communication: Queue

Difference Between Topic and Queue

The main difference between asynchronous communication via a topic or a queue is that a topic disseminates messages to ALL subscribers while a queue dispatches messages to only ONE consumer.

The publish-subscribe pattern of a topic can be used in a microservices-based application to send one messages to many different subscribed microservices, which means that the publisher does not have to know about the subscribers and could make them react to the published message. On the other hand, communication via a queue can be used to distribute workload among several instances of the same microservice.

Posted in Cloud, GeneralTaggs: