Serverless Event Driven Microservice Architecture using Cloud Run, Eventarc & Pub/Sub

Ankur Gautam
Google Cloud - Community
4 min readJun 8, 2023

Moving from Monolith to Microservices has immediate benefits like flexible scalability, improved fault tolerance & agility. While there are benefits, the journey from Monolithic to Microservice can present some challenges too. Disintegrating and distributing services into individually deployable components increase the number of network connections required to be managed. These connections can be expensive by creating unnecessary and unmanaged chatter on the network.

The connections between services can be synchronous or asynchronous. Direct communication between services can sometimes result in a web of unmanaged and complex connections while asynchronous communication can result in a loosely coupled architecture providing better agility and isolation to services.

Synchronous vs Asynchronous Communications

Event driven architecture (EDA) is a way to handle asynchronous communication between multiple services which allows components to process transactions independently. To implement EDA, an intermediary component is required which can hold and route events (messages) between Microservices.

Another overhead in Microservice architecture is to manage scalability & resource requirements of individual services. To solve these you require a platform which can allow you to deploy services in Serverless manner. Auto scaling & capacity management can be managed by the platform which enables architects to spend time on other important areas.

How Google Cloud enables this architecture

Cloud Run can run any stateless container in Serverless manner on Google Cloud infrastructure. Cloud Run can automatically scale your applications up or down based on demand. You don’t need to worry about cold start problems generally associated with serverless platforms. You can develop your application in your favourite language, containerise it and run it directly on cloud run which integrates seamlessly with other Google Cloud services.

Deploy containers directly on Cloud Run

Eventarc is a fully managed event routing service that allows you to decouple Microservices and applications, and to easily build event-driven architectures. Eventarc allows you to decouple microservices and applications by making it easy to subscribe to and process events. This can improve the scalability, reliability, and efficiency of your applications.

Services can be integrated with Cloud Run using Eventarc

Pub/Sub is an asynchronous and scalable messaging service that decouples services producing messages from services processing those messages. Pub/Sub allows services to communicate asynchronously.

Example of a Cloud run Microservice with Eventarc & Cloud Pub/Sub

The example creates an Evantarc trigger whose target is a Microservice deployed in Cloud Run. Eventarc trigger listens to message published events from a pub-sub topic and triggers Cloud run service.

Creating a Cloud Run Service

Create a new service from Cloud Run Dashboard
Deploy a cloud run service which accepts an event from Eventarc and logs it.
An https url created once the service is up and running

Configure Evantarc & PubSub to send or receive Events with Cloud Run

Add Eventarc trigger
Create new Eventarc Trigger

Choose event provider as Cloud Pub/Sub (you can create a new pub-sub topic while choosing the topic)

Send a message to Cloud Pub/Sub

gcloud pubsub topics publish test-topic - message "Test Message from pubsub"
Check for Cloud Run Logs to see the message

Read More about Architecture & Services

Cloud Run with Pub/Sub & Eventarc. https://cloud.google.com/eventarc/docs/run/pubsub#existing-pubsub-topic

Cloud Run with Eventarc. https://codelabs.developers.google.com/eventarc-workflows-cloud-run#0

--

--