Design a Pub-Sub
Learn to design a pub-sub system and its components.
What is a pub-sub system?
Publish-subscribe messaging, often known as pub-sub messaging, is an asynchronous service-to-service communication method that’s popular in serverless and microservices architectures. Messages can be sent asynchronously to different subsystems of a system using the pub-sub system. All the services subscribed to the pub-sub model receive the message that’s pushed into the system.
For example, when Cristiano Ronaldo, a famous athlete, posts on Instagram or shares a Tweet, all of his followers are updated. Here, Cristiano Ronaldo is the publisher, his post or Tweet is the message, and all of his followers are subscribers.
Motivation
The hardware infrastructure of distributed systems consists of millions of machines. Using a pub-sub system to communicate asynchronously increases scalability. Producers and consumers are disconnected and operate independently, thereby allowing us to scale and develop them separately. The decoupling between components, the producers and consumers, allows greater scalability because adding or removing any component doesn’t affect the other components.
Let’s define the requirements for such a system.
Requirements
We aim to design a pub-sub system that has the following requirements.
Functional requirements
Let’s specify the functional requirements of a pub-sub system:
-
Create a
: The producer should be able to create a topic.topic A topic is a collection of related events or messages. -
Write messages: Producers should be able to write messages to the topic.
-
Subscription: Consumers should be able to subscribe to the topic to receive messages.
-
Read messages: The consumer should be able to read messages from the topic.
-
Specify retention time: The consumers should be able to specify the retention time after which the message should be deleted from the system.
-
Delete messages: A message should be deleted from the topic or system after a certain retention period as defined by the user of the system.
You’re designing a real-time chat application that has channels for different discussions. Explain how you would use a pub-sub system for its messaging component.
Describe the mapping of the chatting application onto the following pub-sub components:
Topics
Subscribers
How message delivery would work
Write your answer in the widget below.
Non-functional requirements
We consider the following non-functional ...