Pub/Sub is a messaging middleware which globally manages event ingestion and messaging.
Let’s consider a scenario where we might face failures due to tight dependency.
In the case of tight dependency, every component is dependent on some other element for its smooth functioning. The system relies on things to happen in a specific order. When some component fails to perform its function, the functioning of other components is also affected, and that may cause a temporary system failure.
Here, the backend receives data from two sources:
Once the data is received in the backend, it is moved to the frontend.
Let’s suppose that the backend fails at a certain point, maybe due to some hardware issues. In that case, all the messages from these sources are lost, and the frontend stops working because it doesn’t have any data to work with (backend).
To avoid such failures, we use a message bus. There are different types of message buses, but we will only learn about a publish/subscribe model (Pub/Sub) in this shot.
Pub/Sub is a layer between the components where we pass the message directly.
In this model, we can split our message bus into different groups of messages. In Pub/Sub, we call these topics. A message can either be sent to or received from a topic. Let’s look at the system using Pub/Sub:
By introducing Pub/Sub, we have loosely coupled our services and presented resilience in component failure. Pub/Sub in Google Cloud is a fully managed service, that is, messages are queued, and made ready to be consumed again when a component is restored. In this way, we can think of Pub/Sub as a shock absorber for the system.
Pub/Sub can also act on triggers or trigger some actions. Here are a few essential points to know about Pub/Sub:
It is Google’s global messaging and event ingestion service.
It is a server-less and fully-managed service.
We can create multiple publisher/subscriber patterns—for example, one to one, many to one, and so on.
It guarantees at least one delivery of every message.
We can process messages in real-time or in batches.