You appear to be offline. Some site functionality may not work.
Sign Up

Fastly Blog

A/B testing at the edge

The following post is based on Chris’ talk at Altitude, our customer summit. Read the full recap here.

When you make a change to a system — whether it’s design, code, or infrastructure — it’s important to know that you’re causing the desired outcome. You want to base your business decisions on data, and one of the best ways to get that data is with A/B testing.
 
A/B testing can be as simple or as complicated as you want it to be. You can build elaborate multivariate test frameworks, or use a variety of commercial tools to set up tests either serverside or clientside. Sometimes, you just want to run a quick check to validate that a change didn't make anything worse — perhaps a lack of change is exactly what you are looking for. Either way, you won't know for sure until you look.
 

A/B testing at the edge

At Fastly, we’re big fans of doing things at the edge, closer to users — whether that’s extending as much of your application as possible to the edge of our network or looking towards a future where we’ll have both data and logic at the edge. A/B testing closer to your users means they won’t suffer due to decreased performance while a separate segmentation service is consulted, and also prevents delays on the client while it re-renders the page. To pull this off, you need to:

  1. Segment users into different buckets
  2. Persist those decisions across sessions, so returning users get the same experience
  3. Keep distinct copies of the objects in the cache so a user immediately gets the right content

All of this can be done at the edge, so the only thing the server has to do is create one copy of each variant — and that's it.

Suppose you’re creating two different versions of your homepage, one with a green background and one blue. You want 10% of users to get the green one. You'd then monitor your analytics tools to see if user behavior changes — whether it’s conversion, bounce rate, or whatever metrics you care about.

Segmentation

First, we need to put users into the right buckets. We'll use the randombool ([https://docs.fastly.com/guides/vcl/randomness-related-vcl-features.html](https://docs.fastly.com/guides/vcl/randomness-related-vcl-features.html)) function. Then, based on the decision, we set a header going to server so it knows which version to give us.

sub vcl_recv {
   if (randombool(10,100)) {
      set req.http.X-Homepage = "green";
   } else {
      set req.http.X-Homepage = "blue";
   }
}

Persistence

Now we need to remember this decision for returning users:

sub vcl_deliver {
  # If the cookie doesn't already exist, set it.
  if (!req.http.Cookie:Homepage){
    add resp.http.Set-Cookie = "Homepage="
    req.http.X-Homepage ”; expires=Thu, 20-Jul-17 19:47:08;”;  
  }   
  return(deliver);
}

Now that we have the cookie, we can revisit the example from above, and make sure we only make a segmentation decision if they are a first-time visitor.

sub vcl_recv {
  if (!req.http.Cookie:Homepage) {
    if (randombool(10,100)) {
       set req.http.X-Homepage = "green";
    } else {
       set req.http.X-Homepage = "blue";
    }
  } else {
    # Set the value of the header to whatever decision was previously made
    set req.http.X-Homepage = req.http.Cookie:Homepage; 
  }
}

Keeping different versions cached with HTTP Vary

You'll want to keep both versions in the cache to ensure fast page loads for all users, but also ensure that each user is seeing the right version. By default, Fastly considers an object unique if it has the same hostname and URL.

Those two homepages, green and blue, would look like the same object — so we need to use <http vary> to keep them separate. Vary is a hint from the server to tell us what other properties the cache should consider a distinct object. Note that this doesn't change the hash, so a single purge will get rid of all variants at once.

The server could set that response header, like this:

Vary: Accept-Encoding, X-Homepage

If you don't want to change the response from the server, you could just get Fastly to insert the header into the response from the server.

if (beresp.http.Vary) {
	set beresp.http.Vary = beresp.http.Vary ", X-Homepage";
} else {
	set beresp.http.Vary = "X-Homepage";
}

Testing different back ends

You could use the same techniques to choose a different back end, for example: if you wanted to send one percent of traffic to a new version of your site.

if (randombool(1,100)) {
   set req.backend = F_NewApp;
} else {
   set req.backend = F_OldApp;
}

Persistence without cookies (and important errata)

Lastly, I need to correct something I said in my talk about A/B testing at Altitude. If you want to use the same techniques without cookies, you can create something unique to segment different users.

I suggested doing something like this to maintain persistence without cookies:

set req.http.X-ClientIDHash = digest.hash_md5(client.ip req.http.User-Agent)
set req.http.X-ClientID = std.strtol(req.http.X-ClientIDHash,16)

# e.g. 9223372036854775807

if (randombool_seeded(5,100,std.atoi(req.http.X-ClientID))) {
  set req.http.X-FastAB = "B";
} else {
  set req.http.X-FastAB = "A";
}

This mostly works, but one function requires a tweak. In some cases, creating a hash of different properties makes the number used as the seed too large. We just need to trim it, so you can do this:

set req.http.X-ClientIDHash = digest.hash_md5(client.ip req.http.User-Agent);
set req.http.X-ClientIDHash-trimmed = regsub(req.http.X-ClientIDHash, "^([a-fA-F0-9]{10}).*$","\1");
set req.http.X-ClientID = std.strtol(req.http.X-ClientIDHash-trimmed, 16); 

It is still unique(ish), and good enough to maintain the segmentation decision.

Some of our customers (like the nice folks at Good Eggs) have used these techniques to build pretty elaborate logic at the edge, even building internal tooling for their users to directly control weights and tests.

If you’ve used Fastly in an interesting way, we’d love to hear about it — head over to our Community Forum to share your work.

Watch the video of Chris’ talk below, and stay tuned for more posts from Altitude.

Edge Intelligence Performance Varnish

You may also like:

Author

Chris Jackel | Systems Engineer

Chris Jackel is a recovering Ops person, and spends his days staring at cache headers. He is based in New York.