Implementing scalable microservices for the consumer goods delivery industry
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.
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)
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.
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.)
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
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.
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