Pub/Sub Pattern in Golang

Hello everyone, We continue to examine the important topics of Microservices. In this blog post, we will look for answers to questions such as “What is pub/sub pattern?” and “How does it work?” We will create a small pub/sub pattern using the Golang programming language.
What is Pub/Sub Pattern?

The Pub/Sub pattern is an asynchronous messaging pattern between publisher and subscriber. Publisher posts on a specific topic, and subscribers subscribe to those topics.
Rather than sending messages directly to a specific subscriber, the publisher publishes them on relevant topics. By subscribing to the topics they are interested in, the subscribers only receive messages related to these topics. In this way, message traffic is reduced, and more effective communication is ensured.
Let’s explain this with a few examples.
Elon Musk’s Tesla vehicles communicate with each other and with Tesla’s cloud-based background services using the pub/sub pattern. For example; a Tesla vehicle sends information about its charge level to the cloud service, and other Tesla vehicles receive this information by subscribing to the relevant topic. This provides a real-time update of charge levels between vehicles.
Disadvantages
Communication complexity: Pub/Sub architecture has a complex structure since many components communicate with each other. This requires more attention and effort from programmers.
Security issues: The Pub/Sub model involves processing messages at multiple points. This can cause security vulnerabilities.
Performance issues: In case of heavy traffic, the Pub/Sub model’s performance may decrease, and the system may become less efficient.
Resource consumption: The Pub/Sub model may require the use of many resources. This can increase costs and result in a more concentrated use of resources.
Administration challenges: The Pub/Sub model has a complex structure that includes many components. This can create challenges in administration and maintenance.
Popular Pub/Sub Technologies

Apache Kafka:
Apache Kafka is a high-performance, distributed pub/sub messaging system.
RabbitMQ:
RabbitMQ is an open-source messaging software and uses the AMQP (Advanced Message Queuing Protocol) protocol. It is compatible with many programming languages and supports the transmission of different types of messages.
Google Cloud Pub/Sub:
Google Cloud Pub/Sub is a pub/sub tool available as part of the Google Cloud Platform. It is known for its high performance, low latency, and high scalability features.
Amazon Simple Notification Service (SNS):
Amazon SNS is a pub/sub tool available as part of Amazon Web Services. SNS is known for its high performance, low latency, and scalability features.
We learned brief information about the most used pub/sub technologies. Comparing these technologies, their advantages and disadvantages require much more detail. So, can we do something ourselves without using these technologies?
Let’s continue with an example.
In this example, our scenario is as follows, both mail and SMS services listen to a message from the user, and they both complete the sending process within themselves.
If you are going to write a pub/subpattern, you can do much more effective work with the channel and context in the Golang programming language. You can check out a pub/sub repo I made with “channel” here.
1. First, let’s define a “Subscriber” interface.

“All services that include the “Notify()” method and subscribe to the publisher will be able to handle incoming events thanks to this method.
As a second step, let’s create our “Publisher”. For this, I define a struct, and it will contain a Subscriber array.

3. We have 3 methods of the Publisher object. Adds a new subscriber, deletes the existing subscriber, and sends an event to all subscribers.

4. After completing the Publisher part, it remains to write a few subscriber services.

We have 2 subscriber services in the above code. One of them is EmailSubscriber, and the other is SMSSubscriber. As both services implement the “Notify()” method, they can handle incoming messages.
5. Now it is time to run the code we wrote.

We add the subscriber objects we created in our main.go file to our publisher service as a subscriber, and both services handle the sent messages.
The pub/sub pattern in Go provides effective communication between publisher and subscribers and is often used in data sharing and event-based scenarios. Implementing this pattern allows you to build powerful and flexible systems and is quite simple, thanks to the ease of the Go language. I hope this article gave you an idea of how pub/sub pattern can be used in the Go language. ❤
Sources:
TL;DR
What is Pub/Sub Pattern?
What is the disadvantage of pub/sub pattern?
What are the popular Pub/Sub technologies?
Author: Baran Can Atbaş
Published on: Apr 7, 2023
