Last active
June 8, 2019 17:31
-
-
Save tgwizard/9ca0e3419f45c288d3736b24b4bad963 to your computer and use it in GitHub Desktop.
Fastly VCL config for A/B tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub vcl_deliver { | |
# ----- snip ----- | |
# Set a cookie with the experiment group sequence, if the client didn't | |
# already have the cookie and provided it in the request, and if the origin | |
# hasn't already set it. Use `setcookie.get_value_by_name` to handle the | |
# cases when the backend sets multiple cookies. | |
# Only do this on the edge nodes, not the origin shield, otherwise the edge | |
# nodes will see a response with a `Set-Cookie` header and not cache it. | |
if (!req.http.Fastly-FF && req.http.X-Tt-Backend-AB-First ~ "1" && !setcookie.get_value_by_name(resp, "tt_bab")) { | |
add resp.http.Set-Cookie = "tt_bab=" req.http.X-Tt-Backend-AB "; Expires=" time.add(now, 3650d) "; Path=/"; | |
} | |
# ----- snip ----- | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub vcl_recv { | |
# ----- snip ----- | |
# Create and manage the Tictail Backend AB Experiments cookie. This cookie is | |
# a random sequence of As and Bs, and is assigned to each client. The backend | |
# can use this cookie, and the corresponding X-Tt-Backend-AB header, to place | |
# the client into groups for backend A/B tests. | |
# For example: an A as the first character could mean that the client is in | |
# group A, control, for test X, while a B would mean group B, treatment. 50% | |
# of all clients would then be in each group. The second character could be | |
# used for another test Y. X and Y would then be independent. | |
# The backend can decide whether to have this cookie/experiment group | |
# placement as part fo then Fastly cache key by including `X-Tt-Backend-AB` | |
# in its `Vary` response header. | |
if (!req.http.Fastly-FF) { | |
# Only do this at the edge nodes, not the origin shield. There's no need to | |
# do it twice. | |
if (req.http.Cookie ~ "tt_bab=") { | |
set req.http.X-Tt-Backend-AB = req.http.cookie:tt_bab; | |
} else { | |
set req.http.X-Tt-Backend-AB-First = "1"; | |
set req.http.X-Tt-Backend-AB = randomstr(2, "AB"); | |
if (req.http.Cookie) { | |
set req.http.Cookie = req.http.Cookie "; tt_bab=" req.http.X-Tt-Backend-AB; | |
} else { | |
set req.http.Cookie = "tt_bab=" req.http.X-Tt-Backend-AB; | |
} | |
} | |
} | |
# ----- snip ----- | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment