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 application.
  2. Click the configure tab (the wrench icon at the top of the application window).
  3. Select the appropriate service from the Service menu.
  4. Click the Configure button to the right of the displayed service name.
  5. Click the Hosts pane from the section list on the left.
  6. In the Backends area, click the gear icon to the right of the origin server you want to edit.
  7. Select Edit from the menu. The Edit Backend window appears.

    the Edit Backend window

  8. Change the setting for Auto Load Balance to Yes and click Update.

You can repeat steps 7 and 8 for each origin server you want to include in the automatic load balancing group.

How Weight affects load balancing

The Weight setting on the New Backend or Edit Backend window allows you to specify the percentage of the total traffic that is sent to specific origins servers. This setting only appears on the windows 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 New Header window

After the header is created, click the gear icon to the right of the header name and select Request Conditions, 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 {
  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

Back to Top