LOG IN SIGN UP
Documentation

Load-balancing configuration

This guide describes how to automatically load balance between two or more origin servers. Load balancing distributes requests across multiple servers to optimize resource use and avoid overloading any single resource.

Before you begin

Before you configure load balancing, keep in mind the following:

Setting up load balancing between origins

If you want to randomly load balance between two or more origin servers, enable the Fastly automatic load balancing feature.

Enabling load balancing

  1. Log in to the Fastly web interface and click the Configure link.
  2. From the service menu, select the appropriate service.
  3. Click the Edit configuration button and then select Clone active. The service version page appears.
  4. Click the Origins tab. The Origins page appears.
  5. Click the name of the host you want to edit. The Edit a host page appears.

    the Edit Backend window

  6. From the Auto load balance menu, select Yes.

  7. Click the Update button.

  8. Repeat steps 5, 6, and 7 for each origin server you want to include in the automatic load balancing group.

  9. Click the Activate button to deploy your configuration changes.

How Weight affects load balancing

The Weight setting on the Create a new host or Edit a host page allows you to specify the percentage of the total traffic that is sent to specific origins servers. This setting only appears on the pages when you set Auto load balance to Yes.

A new backend with load balancing select to display the weight field

When you specify a whole number in the Weight field, you specify the percentage of the total traffic to send to a specific origin server. Each origin server receives the percentage (<weight>/<total>) of the total traffic equal to the number you specify.

For example, if you have two origin servers, A and B, setting the Weight field to 50 on both splits the traffic between them equally. Each origin server receives 50 percent of your total traffic.

If you increase the Weight setting on origin server A to 55 and decrease the Weight setting on origin server B to 45, the percentage of traffic changes to 55 percent and 45 percent respectively.

How conditions affect load balancing

When you set conditions on origin servers you can potentially change how automatic load balancing works. The load balancing autodirector groups servers together based on like conditions, giving you the flexibility to effectively create subsets of the autodirector by assigning a condition to one group of origins and another condition to another set of origins. If each group of origin servers has a different condition that affects load balancing, the auto load function will not randomly load balance between the different servers.

Conditions can also be assigned to a server through a header. For example, you have three servers called F_Fastly, F_Second_backend, and F_Third_backend and want all URLs with a certain prefix to default to the second server.

the Create a new header window

After the header is created, click the Attach a condition link to the right of the header name, then add a new condition to apply if the URL matches the desired prefix.

the New Condition window

The generated VCL below illustrates the autodirector set for all three servers. Within the section sub vcl_recv, the default origin server is set to the autodirector and, if the media condition is met, requests are forwarded to the second server.

director autodirector_ random {
  {
    .backend = F_Second_backend;
    .weight  = 100;
  }{
    .backend = F_Third_backend;
    .weight  = 100;
  }{
    .backend = F_Fastly;
    .weight  = 100;
  }
}

sub vcl_recv {
#--FASTLY RECV CODE START
  if (req.restarts == 0) {
    if (!req.http.X-Timer) {
      set req.http.X-Timer = "S" time.start.sec "." time.start.usec_frac;
    }
    set req.http.X-Timer = req.http.X-Timer ",VS0";
  }

  # default conditions
  set req.backend = autodirector_;

  # end default conditions

  # Request Condition: Media files Prio: 10
  if (req.url ~ "^/media") {

    # Header rewrite Media Requests : 10
    set req.backend = F_Second_backend;
  }
  #end condition

#--FASTLY RECV CODE END
}

Back to Top