Cloudflare Load Balancing to Route Server Traffic (Step by Step)

Published:6 April 2022 - 7 min. read

Elly Obare Image

Elly Obare

Read more tutorials by Elly Obare!

Are you tired of applications and system outages whenever your site detects a spike in traffic? Worry not! All you need is a networking cloud-based solution to help in routing traffic evenly across your servers. Luckily, there is Cloudflare load balancing which distributes traffic across your multiple servers, providing the high availability your site users need.

In this tutorial, you’ll learn to configure Cloudflare load balancing while highlighting its key features and advantages.

Read on and start creating your own load balancer!

Prerequisites

To follow along, be sure you have a Cloudflare account, an active domain, and more than one origin server to serve content. An origin server is the source of your website content, with a minimum of two being necessary to demonstrate Cloudflare load balancing.

Configuring a Failover for Traffic Steering

Before you can take advantage of Cloudflare load balancing, you need to understand the different available failover configurations. Server failover requires having a backup server (alternative origin server) configured to serve traffic and take over if the primary server (first origin server) becomes unavailable.

Automatic failover distributes traffic based on server latency and geographic regions of users when any server is unresponsive or unavailable.

Cloudflare’s common failover configurations include:

  • Active – Passive: The primary origin services all traffic until it is unavailable and the passive, or alternative origin, is promoted to service all traffic instead.
  • Active – Active: All origins are available to service traffic, with traffic divided up between them.

Steering Traffic with Active-Passive Failover

An active-passive failover configuration steers the traffic to servers in your active pool until a failure threshold occurs. The load balancer will then redirect traffic to the configured secondary origin when the failure happens.

This configuration provides an uninterrupted service, especially when there are planned outages, but may result in slower responses due to all traffic being directed to a single origin.

To have an active-passive failover, you’ll need to have a load balancer with two origin pools.

1. Open your favorite web browser and log in to the Cloudflare dashboard.

2. Create the first origin pool and set that pool as the primary pool for your load balancer.

An origin pool consists of a group of servers known by their IP addresses or hostnames. Cloudflare load balancing redirect traffic towards the hostnames according to the health and weight of each origin.

Creating the primary origin.
Creating the primary origin.

3. In the Origins section, click Add Origin to add a second origin to the existing pool, and click Next.

Adding a secondary pool.
Adding a secondary pool.

4. Finally, select the Off option to direct traffic from any unhealthy pool to the next healthy pool in the configuration, and click Next to prompt you to save the configuration.

With this configuration, your load balancer will direct all traffic to be primary until the primary pool has fewer available origins than specified in its health threshold. Only then will your load balancer direct traffic to the secondary pool.

Selecting Traffic Steering Option
Selecting Traffic Steering Option

With the massive presence of Cloudflare servers around the globe, Cloudflare load balancing reduces latency by serving traffic across multiple servers. Or routing traffic to the closest geolocation region to ensure customers are not getting any lags at all.

Steering Traffic with Active-Active Failover

An active-active failover steers traffic to servers within the same pool until the pool reaches its configured failure threshold. The load balancer would then redirect traffic to the set fallback pool when a failure occurs.

Create two origin pools (primary and secondary) then save the origin pool configurations, which you’ll use in step three of the “Creating a Load Balancer” section.

Setting up an Active-Active Failover
Setting up an Active-Active Failover

Creating a Load Balancer

Cloudflare load balancing enhance application performance and availability by channeling traffic away from unhealthy and unavailable servers. At the same time, dynamically distributing the traffic to any available and responsive server pools without experiencing any latency. But how to create a Cloudflare load balance?

The first step in creating a load balancer is to create a hostname. A load balancer defaults to the root hostname, but you can also set a unique hostname matching your needs. Secondly, note the session affinity that attempts to pin a session to a single origin.

1. On your Cloudflare dashboard, click on the Traffic menu —> Load Balancing tab to launch the creation page, as shown below.

Click on Create Load Balancer to configure a new load balancer.

Setting up Cloudflare load balancing requires no additional software or hardware, just the Dashboard or REST API. Additionally, you are at liberty to scale origin pools tied to load balancers depending on the traffic in your system.

Initiating Cloudflare Load Balancing
Initiating Cloudflare Load Balancing

2. Next, configure a hostname with the following:

  • Supply a Hostname, basically a DNS name that hosts the load balancer to be created. In this demo, the hostname blur the hostname.

For this tutorial, traffic to the set hostname will not pass through Cloudflare. But as a developer, you can choose whether to proxy or not to proxy traffic to this hostname through Cloudflare by clicking the icon to the right of the hostname input box.

  • Shift the session-based load balancing toggle for Session Affinity. When session affinity is enabled, your load balancer will direct all requests from a particular end-user to a specific origin server.

Session affinity also reduces request traffics, saving customers on usage billing.

  • Click Next to load the Origin Pool page.
Configuring a Hostname
Configuring a Hostname

3. Select an existing pool from the dropdown box and click Add Pool or click on Create a new origin pool to create a new one, then click Next. This demo uses the existing origin pool you created in the “Steering Traffic with Active-Active Failover” section.

Adding an Origin Pool
Adding an Origin Pool

4. Now, select an existing Cloudflare monitor or create a new one to attach to your origin pool, and click Next.

The Cloudflare monitor you attached will perform health checks regularly to determine the health of your origin pool. Your load balancer will therefore not direct traffic to that pool if it becomes unhealthy.

Attaching a Health Check Monitor
Attaching a Health Check Monitor

5. Select your desired option shown below Add Traffic Steering. Geo or dynamic steering is only ideal for enterprise scenarios. So instead, choose the conventional failover (Off) option, then click Next.

Traffic Steering distributes incoming requests to a load balancer based on factors such as pool and origin health, pool steering, and origin steering. Any traffic decision to be made starts from which pools and origins are healthy and available and should receive traffic.

Selecting Preferred Traffic Steering Option
Selecting Preferred Traffic Steering Option

6. Finally, review the summary for your load balancer configuration, and click on the Save and Deploy button to deploy the new load balancer.

Suppose you want to share with the load balancer you created with other sites, you’ll need to create a canonical name (CNAME) first. A CNAME will help you share your configurations with several domains without creating new load balancers for each domain or site involved.

Saving a Load Balancer
Saving a Load Balancer

You’ve seen how sequential creating a load balancer is. But creating a load balancer can also be achieved through an API approach to simplify the activity.

Editing and Deleting Load Balancer Configurations

In case you made some errors while creating your load balancer, you can either edit or delete the configurations through the Cloudflare dashboard.

With Cloudflare load balancing, you can fashion custom rules or reuse monitors and pools across multiple load balancers, Doing so, lets you adjust routing according to attributes of each request sent from different domains.

1. Navigate to the Traffic page and click the Load Balancing option.

Load balancer dashboard.
Load balancer dashboard.

2. Click on either Edit or Delete depending on your goal for your target load balancer configuration.

Edit or delete dashboard.
Edit or delete dashboard.

3. Update the settings as needed while going through the creation workflow if you’re editing a load balancer configuration.

4. Click the Save option after you’re satisfied with all the adjustments.

Saving Modifications on a Load Balancer Configuration
Saving Modifications on a Load Balancer Configuration

Conclusion

In this article, you’ve learned how to create a load balancer. You’ve also looked into steering traffic with two different failovers for your load balancer, ensuring the high availability of your website.

Cloudflare’s global load balancing operates at the DNS level, supporting major protocols – HTTP(s), TCP, and even UDP-based services. This feature allows for integration with Cloud platforms or other services, be it compute or storage.

Now that you have seen Cloudflare Load balancing in action, why not implement it in any of your high-volume transaction businesses? Such as eCommerce applications to keep them fast and stable.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!