We've been making changes to how we organize and display our docs. Our work isn't done but we'd love your feedback.
Getting started
Basics
Domains & Origins
Performance

Configuration
Basics
Conditions
Dictionaries
Domains & Origins
Request settings
Cache settings
Headers
Responses
Performance
Custom VCL
Image optimization
Video

Security
Access Control Lists
Monitoring and testing
Securing communications
TLS
Web Application Firewall

Integrations
Logging endpoints
Non-Fastly services

Diagnostics
Streaming logs
Debugging techniques
Common errors

Account info
Account management
Billing
User access and control

Reference

    Load-balancing configuration

      Last updated May 14, 2018

    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:

    Enabling load balancing

    To enable load balancing across two or more origin servers, follow the steps below:

    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 Configuration button and then select Clone active. The Domains page appears.
    4. Click the Origins link. The Origins page appears.
    5. Click the name of the host you want to edit. The Edit this host page appears.
    6. From the Auto load balance menu, select Yes.
    7. In the Weight field, type the percentage of the total traffic to send to the origin server.

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

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

    Using conditions with load balancing

    You can set conditions on origin servers or headers to change how load balancing works.

    Setting conditions on origin servers

    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.

    Setting conditions on headers

    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. First, you'd create a header.

    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 Configuration button and then select Clone active. The Domains page appears.
    4. Click the Content link. The Content page appears.
    5. In the Headers area, click the Create header button to create a new header. The Create a header page appears.

      the Create a header window

    6. Fill out the Create a header fields as follows:
      • In the Name field, type a descriptive name for the new header (for example, Media Requests).
      • From the Type menu, select Request and from the Action menu, select Set.
      • In the Destination field, type the name of the header that will be affected by the action (for example, backend).
      • In the Source field, type the name of the origin server the content for this header comes from (for example, F_Second_backend).
      • Leave the Ignore if set and Priority fields set to their default settings.
    7. Click Create.

    After the header is created, you'd create a new condition to apply if the URL matches the desired prefix.

    1. In the Headers area, click the Attach a condition link next to the name of the new header you just created. The Create a new request condition window appears.

      the New Condition window

    2. Fill out the Create a new request condition fields as follows:
      • In the Name field, type a descriptive name for the new condition (for example, Media Files).
      • In the Apply if field, type the request condition that will be applied (for example, req.url="^/media").
      • Leave the priority set to its default value.
    3. Click the Save and apply to button to create the new condition for the header.
    4. Click the Activate button to deploy your configuration changes.

    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. If the condition is not met, requests are forwarded to one of the three servers at random.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    
    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

    Additional resources: