Implementing scalable microservices for the consumer goods delivery industry

Kiran Vasudevan
Google Cloud - Community
4 min readDec 21, 2022

A common challenge faced by the firms performing the last-mile delivery of consumer goods like groceries, vegetables, stationeries is the ability to scale based on unpredictable demands. The scale-in and scale-out must happen without any latency as the speed-of-delivery to the consumer is a key benchmark in this business.

Google Cloud Platform (GCP) provides efficient ways to build, deploy and run scalable microservices to address this challenge. A last-mile delivery firm would be a consumer to various producers from multiple industries like grocery, stationery, medical etc. One way to architect this is to have each business unit (producers) to be in their own GCP Projects taking care of their respective domain processes. A central GCP project would run the Application (compute) cluster and perform the scaling by monitoring metrics from each of the individual GCP projects used by the business units. An illustration of the architecture can be found in the figure 1 below.

Figure 1: Illustration of Architecture to build scalable microservices (pods) utilizing external metrics from Pubsub

Configure a GCP project to deploy the GKE Cluster, then deploy the metrics adapter to let GKE cluster send and receive metrics with Cloud Monitoring. (Tips: Please refer to this: https://cloud.google.com/kubernetes-engine/docs/tutorials/autoscaling-metrics#step1) Make this GCP project the Monitoring (Scoping) Project to monitor metrics from GCP projects pertaining to other business units. The individual GCP projects being scoped are generally referred as Monitored projects. This can be performed using the Metrics Scope feature of the Monitoring Workspace of the Monitoring (Scoping) project as displayed in the sample screenshot (Figure 2) below: (Tips: Use ‘Add GCP Projects’ option to add monitored projects)

Figure 2: Sample screenshot of scoping multiple Monitored Projects into a Monitoring Project

Ensure that the relevant Pubsub subscriptions and topics are created (or already existing) in the respective GCP projects (both in the Monitoring projects and Monitored projects).

(Tips: Following sample commands may be used for creating pubsub topics and subscriptions.

  • gcloud pubsub topics describe echo-projectD
  • gcloud pubsub subscriptions create echo-read-projectD — topic=echo-projectD — topic-project=point-of-views-11282022 )

Deploy the microservices (Pods) in the GKE cluster in the GCP Monitoring project. The application must use a service account having the ‘pubsub.subscriber’ role. The following screenshots show a sample application that polls the pubsub topic and its manifest file.

Figure 3: Sample code of a Python App that can be deployed (for testing)
Figure 4: Manifest file for the sample App shown in Figure 3

Configure a Horizontal Pod Autoscaler (HPA) to look for pubsub metrics. For example, configure a HPA to listen to the pubsub.googleapis.com/subscription/num_undelivered_messages metric from the resource type, pubsub_subscription. In the example screenshot in Figure 5 below, HPA is looking for specific subscription ids. (This can be extrapolated to include additional labels to filter further as per requirements.)

Figure 4: Manifest file of a sample HPA that listens to pubsub metric

Apply the manifest and describe the HPA to view the initial state. The Target field shows the actual value vs. the desired value. A sample screenshot of the same is shown in the Figure 5 below

Figure 5: The description of the HPA showing the initial state of scaling

The setup is complete now. As messages are posted to the pubsub topics that are tied to the subscriptions monitored by the HPA (in the example above), the pods running in the GKE cluster will start to scale horizontally. In order to test the setup, you may please generate the load on the pubsub by posting a series of messages to the pubsub topics from each of the Monitored Projects (handled by individual Business Units) and within the Monitoring Project. It can be observed that the HPA listens to these external metrics and reacts by initiating horizontal pod autoscaling. Observe the current state of the HPA. A sample screenshot demonstrating the scale out is shown in Figure 6.

Figure 6: The description of the HPA showing the current state of scaling

From a business standpoint, the last-mile consumer goods delivery apps running in the GKE cluster of the Monitoring Project can be seamlessly scaled based on the (external) metrics received from apps from the other Business Units (Grocery BU, Stationery BU, Medical BU etc.), providing automation, speed and operational efficiency.

For more details, you may please refer to the following documentation from Google:

[1] Viewing metrics from multiple projects — https://cloud.google.com/monitoring/settings

[2] Optimize Pod autoscaling based on metrics — https://cloud.google.com/kubernetes-engine/docs/tutorials/autoscaling-metrics#pubsub_1

--

--

Kiran Vasudevan
Google Cloud - Community

I am a Googler helping customers to build modern applications on Cloud. I have worked for SAP, Cognizant, EY Technology Consulting as an Architect in the past.