Asynchronous Messaging Patterns

Asynchronous messaging is a messaging scheme where message production by a producer is decoupled from its processing by a consumer. When dealing with messaging systems, we typically identify two main messaging patterns—message queuing and publish/subscribe.

Message Queuing

In the message-queuing communication pattern, queues temporally decouple producers from consumers. Multiple producers can send messages to the same queue. However, when a consumer processes a message, it's locked or removed from the queue and is no longer available. Only a single consumer consumes a specific message.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a7eb8c08-8e54-41cc-9f6c-c0a419933a43/Untitled.png

As a side note, if the consumer fails to process a certain message, the messaging platform typically returns the message to the queue where it's made available for other consumers. Besides temporal decoupling, queues allow us to scale producers and consumers independently as well as providing a degree of fault-tolerance against processing errors.

Publish/Subscribe

In the publish/subscribe communication pattern, a single message can be received and processed by multiple subscribers concurrently.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f62dfe77-7f61-42de-b3d7-214091542523/Untitled.png

This pattern allows a publisher, for example, to notify all subscribers that something has happened in the system. Many queuing platforms often associate pub/sub with the term topics. In RabbitMQ, topics are a specific type of pub/sub implementation, but for this piece, I refer to topics as a representation of pub/sub as a whole.

Generally speaking, there are two types of subscription: