Skip to main content
Web App Development

What is Google PubSub and when you should use it

By October 30, 2023No Comments

Have you ever worked on a small project that grew to become a giant system as you added modules on top of modules and use cases on top of use cases to it?

Let’s say this system is an Uber clone.

When the Rider ends a ride, the following tasks need to be done:

  1. Calculate the total fare of the ride
  2. Send a notification to the Rider and the Customer
  3. Show the total fare on the Rider app and the Customer app
  4. Send an email to the Customer

One way to achieve this is to make a monolithic service that performs all the aforementioned tasks. But the downside of that will be:

Increased execution time: As each of these tasks will be done by this API, it will return the result after all of them have completed

No fault tolerance: If the API fails because any single task failed, it will be difficult to track down why the overall API failed. Moreover, once the issue has been diagnosed, it will be difficult to recover from this issue.

Let’s say that our API failed because it could not send an email to the Customer. How will the system fix this? Will this entire API have to be executed all over again? Or will you create another API that sends the email only?

Now imagine this issue impacted 1 million users. How will you fix this issue?

Another (and recommended) way is to use a distributed architecture – which comes with its own complexities. This can further be simplified (to a great extent) by using Google PubSub!

The Power of Distributed Architecture

To overcome the limitations of a monolithic approach, distributed architecture comes to the rescue. By breaking down the tasks into separate services, you can achieve better scalability, fault isolation, and improved performance. However, implementing and managing a distributed system can be complex.

Google PubSub

Google PubSub offers a powerful solution to simplify your distributed architecture. PubSub is an asynchronous messaging service designed to be highly reliable and scalable. It follows the publisher and subscriber model, providing loose coupling between services and allowing for seamless communication.

How Does Google PubSub Work?

PubSub revolves around four core concepts
Message: the data that moves through the service.
Topic: Feed of messages related to an event.
Subscription: An entity representing an interest in receiving messages on a particular topic.
Publisher(producer): Service that creates a message belonging to a specific topic.
Subscriber(consumer): Service that subscribes to a specific topic and receives messages under it.

Real-World Example

Let’s see how we can use PubSub to optimise the ride-ending flow in our Uber clone scenario.

When the ride ends, following operations take place:

  • Fare Calculation – primary
  • Send notification to customer – secondary
  • Send Email to customer – secondary
  • Calculate the rider’s share – secondary

By looking at this we can see that the only time-sensitive and direct tasks are fare calculations. Others can afford a small delay, so what we will do is we will create a main API to manage the fare calculation and 3 subscribers who subscribe to each unique subscription and will process them in a queue.

In this way, we have reduced the load from the main task and even if any secondary service fails, it can easily be triggered again.

Advantages of Using PubSub

Loose Coupling: With PubSub, publishers and subscribers operate independently, eliminating direct dependencies between services. This enhances modularity and simplifies system maintenance.

Scalability: PubSub is designed to handle high loads by supporting horizontal scaling. Increasing the number of topics, subscriptions, or messages is achieved by adding more server instances, ensuring smooth performance even with growing user bases.

Other Alternatives

While Google PubSub is a robust solution, alternative options exist.

  1. AWS SNS (Simple Notification Service), a “push” service that triggers immediate notifications to all subscribers when a message is sent to a topic.
  2. Azure Event Hubs, on the other hand, offers a partitioned, reliable, ordered, pub-sub data stream.

Next Steps

Google Cloud Documentation: Dive into the official Google Cloud documentation on PubSub. It provides in-depth information, tutorials, code samples, and best practices for implementing PubSub in your projects. Take advantage of this valuable resource to enhance your understanding and gain practical insights.

Pub/Sub Made Easy by Priyanka Vergadia: This Google Cloud Tech series offers a step-by-step guide and real-world examples to help you get started with PubSub. It provides comprehensive guidance on various aspects of PubSub implementation and can serve as a valuable reference.

Hands-on Practice: Create a small project or experiment to implement PubSub in a real-world scenario. Start with a simple use case and gradually expand your implementation to cover more complex scenarios. This hands-on experience will deepen your understanding and help you overcome any challenges you may encounter.

Experiment with Other Cloud Providers: While Google PubSub is a powerful solution, it would be worthwhile exploring alternative messaging services offered by other cloud providers, such as AWS SNS or Azure Event Hubs.

Compare their features, scalability, pricing, and integration capabilities to determine which solution best aligns with your project requirements.

Conclusion

As your project evolves into a complex system with various modules and use cases, adopting a distributed architecture becomes essential. Google PubSub serves as a reliable and scalable messaging service that simplifies your distributed architecture, enabling loose coupling, fault isolation, and easy scalability. By leveraging PubSub, you can efficiently handle tasks within your system and ensure fault-tolerant communication between services. With PubSub, you can break down monolithic APIs into smaller, decoupled services, leading to improved performance and easier maintenance.

If you have any questions or need further assistance, feel free to reach out to me via LinkedIn or Twitter. I’m here to help and would be happy to provide guidance or address any queries you may have.

Leave a Reply