Using Global Cloud Load Balancer to Route User Requests to the Closest App Instance
Tags: googlecloud, webdev, distributedsystems, architecture
Published: 2022-10-25 15:17:12

Ahoy, Mateys! 

In my last two articles, I showed you how to deploy [microservice instances across several cloud regions](https://dev.to/yugabyte/automating-java-application-deployment-across-multiple-cloud-regions-27db) and how to set up a [geo-distributed API layer](https://dev.to/yugabyte/geo-distributed-api-layer-with-kong-gateway-4d25). 

All seems good, except that I’ve now got several standalone application instances running across the US West, Central, and East regions, and each instance has its own private IP address.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmda2hai8ljqg0lihwwz.png)

My next challenge is figuring out how to forward application requests to the instances closest to the user. 

I don’t want my Web UI and mobile apps to keep track of all the IPs and decide how and where to route user requests. This is way too complicated and becomes unmanageable. Instead, the Web UI and mobile apps need to connect to a single IP address, which lets other components of my geo-distributed solution decide where to route a particular user request.

What could those components be, matey? 

You’ve guessed it: usually, a proxy and load balancer. 

In this article, I’ll show you how to configure a [global cloud load balancer](https://cloud.google.com/load-balancing) that serves as both a proxy and a load balancer. This type of load balancer comes with a single IP address that can be accessed from any location on earth and can route a request to the nearest (active!) application instance.

So, if you’re still with me on this journey, then, as the pirates used to say, “Weigh Anchor and Hoist the Mizzen!” which means, “Pull up the anchor and get this ship sailing!”

# Explaining Global Cloud Balancer Usage

Before diving into details and technicalities, let’s use a few illustrations to see the value of the load balancer.

Suppose Ms. Blue, one of my application users, is vacationing in the Tampa Bay area of Florida. She promised not to open my corporate Slack-like messenger (the [geo-distributed app I’m building](https://github.com/YugabyteDB-Samples/geo-distributed-messenger)) while on vacation. 

She failed to keep her promise, but for a good reason. She couldn’t resist sharing a few pictures from the pristine beaches of Tampa Bay.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pm6aallmqcuo7haad6bu.png)

The diagram above shows what happens when Ms. Blue opens the app and sends a few gorgeous photos. 

The app connects to the global load balancer's public IP address, `35.186.215.175`. Next, the load balancer determines that the application instance with IP `10.1.12.8` is closest to Ms. Blue (both Ms. Blue and the instance are on the US East coast). Finally, the load balancer asks the East Coast instance to upload the pictures.

Next, Mr. Green, a colleague of Ms. Blue, working from his Seattle-based company office, receives a push notification from the messenger and can’t resist checking what’s been posted. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9i8ddnbodge7l9qgpi97.png)


Mr. Green opens the mobile app and the app connects to the same load balancer using the same public IP address, `35.186.215.175`. This time, the load balancer forwards Mr. Green’s request to the application instance from the West Coast that uses `10.1.11.16` IP address.

Also, the load balancer is instrumental in case of cloud outages. Let’s consider a final example.

A few days later, Ms. Blue decides to make her colleagues jealous again and opens the app to send another set of fabulous pictures. The application connects to the load balancer on the same public IP address (`35.186.215.175`) and…

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n07sdps3kj4it2cwlmsi.png)

…this time the load balancer forwards Ms. Blue’s request to the application instance in the US Central with IP address `10.1.10.10`. 

This is because a storm in the Atlantic impacted the availability of the entire US East Coast cloud region. The US Central region is up and running as normal, and is now the closest for Ms. Blue. So, the request is routed there automatically.

Alright, matey, now let’s move forward and look into the architecture, or the main building blocks, of the global cloud load balancer.

# Global Load Balancer Architecture

The global load balancer is comprised of several building blocks. Take a look at the picture below and review the components from left to right. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v9hovsz75m72igzdboff.png)

This picture represents the architecture of the load balancer in Google Cloud. The components might vary depending on your cloud environment. But you can be sure that every major cloud environment supports the global load balancer.

So, moving from left to right in the picture: As you already know, users connect to the public IP address assigned to the load balancer (or DNS name that translates to this address). After that, the load balancer consults with existing forwarding rules to determine which target proxy needs to handle the request.

Once the target proxy is selected, the proxy checks its URL map to decide which backend services are responsible for the request.

Finally, the backend service delegates the request to one of its backends (actual application instances). In our case, during normal operations, requests from Mr. Green, based in Seattle, will be processed by the US West backend instance. The traffic from Ms. Blue will go to the backend in the US East region.

If you wish to learn more about the global load balancer’s architecture, check out [this documentation](https://cloud.google.com/load-balancing/docs/https) for an extensive overview of all its components. 

# Global Load Balancer Deployment

At last, matey, let’s go ahead and deploy an instance of the global external load balancer in Google Cloud.

Assuming that applications instances are already running across US West, Central, and East, these are our next steps (explore [how to deploy the instances in Google Cloud](https://github.com/YugabyteDB-Samples/geo-distributed-messenger/blob/main/gcloud_deployment.md)):

1. Reserve a static public IP address for the load balancer:
    ```shell
    gcloud compute addresses create load-balancer-public-ip \
        --ip-version=IPV4 \
        --network-tier=PREMIUM \
        --global
    ```
2. Configure a health check for a backend service:
    ```shell
    gcloud compute health-checks create http load-balancer-http-basic-check \
        --check-interval=20s --timeout=5s \
        --healthy-threshold=2 --unhealthy-threshold=2 \
        --request-path=/login \
        --port 80
    ```
3. Create the backend service for the HTTP traffic and provide the just-created health check to ensure the application instances are up and running:
    ```shell
    gcloud compute backend-services create load-balancer-backend-service \
        --load-balancing-scheme=EXTERNAL_MANAGED \
        --protocol=HTTP \
        --port-name=http \
        --health-checks=load-balancer-http-basic-check \
        --global
    ```
4. Provide the backend service with backends (application instances) across several cloud regions:
    ```shell
    gcloud compute backend-services add-backend load-balancer-backend-service \
        --balancing-mode=UTILIZATION \
        --max-utilization=0.8 \
        --capacity-scaler=1 \
        --instance-group=ig-us-central \
        --instance-group-zone=us-central1-b \
        --global
    
    gcloud compute backend-services add-backend load-balancer-backend-service \
        --balancing-mode=UTILIZATION \
        --max-utilization=0.8 \
        --capacity-scaler=1 \
        --instance-group=ig-us-east \
        --instance-group-zone=us-east4-b \
        --global
    
    gcloud compute backend-services add-backend load-balancer-backend-service \
        --balancing-mode=UTILIZATION \
        --max-utilization=0.8 \
        --capacity-scaler=1 \
        --instance-group=ig-us-west \
        --instance-group-zone=us-west2-b \
        --global
    ```
5. Finally, configure the default URL map to route all requests to the created backend service:
    ```shell
    gcloud compute url-maps create load-balancer-url-map --default-service load-balancer-backend-service
    ```

After the backend service is ready, go ahead and configure the frontend part of the load balancer which is the forwarding rule and target proxy.

1. Create an HTTP proxy providing the URL map:
    ```shell
    gcloud compute target-http-proxies create load-balancer-http-frontend \
        --url-map load-balancer-url-map \
        --global
    ```
2. Define a forwarding rule to direct the traffic from the public Internet to the just created proxy:
    ```shell
    gcloud compute forwarding-rules create load-balancer-http-frontend-forwarding-rule \
        --load-balancing-scheme=EXTERNAL_MANAGED \
        --network-tier=PREMIUM \
        --address=load-balancer-public-ip  \
        --global \
        --target-http-proxy=load-balancer-http-frontend \
        --ports=80
    ```

That’s it! The global external load balancer is fully configured. It might just take a few minutes for your configuration to propagate worldwide.

# Testing Load Balancer

Now, it’s testing time!

I’ll play the role of Ms. Blue, who wants to share gorgeous photos from Tampa Bay. A colleague of mine who is based in Seattle will be Mr. Green.

The manual testing is straightforward. I’m opening the messenger in my browser using the load balancer’s public IP address and uploading a few pictures:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dos7jal6jknit3lomrfk.png)

How do I ensure those pictures were uploaded through an instance in the US East?

Well, I opened the monitoring tab for the load balancer and confirmed all my requests are delegated to the `ig-us-east` instance:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9inunh7eui6igx9bg7rn.png)

After that, my Seattle colleague opens the app to check the pictures. In a few moments, I can see that the load balancer directs his traffic to the `ig-us-west`!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5y0no3k8e8q9hrwqw8y.png)

Job done! 

The global load balancer is fully configured and functions globally. This means my geo-distributed messenger can seamlessly process user requests with high performance across the globe and withstand various cloud outages.

# What’s on the Horizon?

Alright, matey! 

My geo-distributed app now functions across multiple cloud regions and availability zones. But the app, API layer, and database still run within the boundaries of the USA. 

My next move would be to scale the messenger to other countries and continents and experiment with several YugabyteDB deployment options (my geo-distributed database).

[Follow me](https://twitter.com/denismagda) to be notified as soon as the next update is published! And check out the [previous articles](https://dev.to/denismagda/series/19150) in my geo-distributed messenger application development journey.