SlideShare a Scribd company logo
Fastly presents
Programming
the edge
April 17, 2018 · Andrew Betts
NYC
presents
CDN
Your application
Traditional CDN model
NYC
presents
Your application
Fastly as part of your app
NYC
presents
fastly.com/demos
Altitude NY 2018: Programming the edge workshop
NYC
presents
Geo-IP API at the Edge
Redirects
Decoding auth cookies at the edge
Relative date insertion
Overriding TTLs based on content-type
Streaming server-sent-events
RUM logging and aggregation
Routing for microservices
CORS OPTIONS preflights at the edge
Serve robots.txt from the edge
Serve stale on origin failure
Getting creative at the edge
Partial content 'Range' requests
Time-limited URL tokens
Image optimization
Modern web security headers
GZip compression at the edge
Client public IP API at the edge
Google Cloud storage / AWS S3 origin
CAPTCHA challenge
Logging to Google BigQuery
Blocking obsolete TLS
Replace origin errors with 'safe' responses
NYC
presents
Debugging today: headers
Fastly-Debug-Path:
Fastly-Debug-TTL:
X-Cache:
X-Cache-Hits:
(D cache-iad2150-IAD 1523245558) (F cache-iad2120-IAD
1523245558) (D cache-lcy8132-LCY 1523245558) (F
cache-lcy8149-LCY 1523245531)
(M cache-iad2150-IAD - - 0) (H cache-lcy8132-LCY - - 27)
HIT, MISS
1, 0
Washington, DC London
iad2150 iad2120
MISS
lcy8132 lcy8149
HIT!
27 sec old
1 previous hit
NYC
presents
fiddle.fastlydemo.net
Altitude NY 2018: Programming the edge workshop
Fastly-Debug-Path:
(D cache-iad2150-IAD
1523245558)
(F cache-iad2120-IAD
1523245558)
(D cache-lcy8132-LCY
1523245558)
(F cache-lcy8149-LCY
1523245531)
Fastly-Debug-TTL:
(M cache-iad2150-IAD - - 0)
(H cache-lcy8132-LCY - - 27)
NYC
presents
● Everything you enter into fiddle is public
● Pushing new config usually <10s but occasionally
longer if there’s a huge compilation job in the batch.
● Prefer that you use Chrome today, but should work in
all modern browsers
● We suggest you do each fiddle in a different tab, so
you still have them all to refer to at the end.
● Some instrumentation is asynchronously collected,
please allow a few seconds for it all to arrive.
● Ask Fastly helpers if you get stuck
NYC
presents
Exercises
Basics GeoIP headers RUM Logging
Redirects Routing and AB testing Shielding
NYC
presents
Proxy and
cache
fiddle/5f263e69
Basics
1
NYC
presents
➔ Create a fiddle
➔ Set a fiddle title
➔ Configure a backend
➔ Execute the fiddle
➔ Understand the result
➔ Understand caching in fiddles
➔ Understand about fiddle insights
fiddle/5f263e69
Basics
1
NYC
presents
Exercises
Basics GeoIP headers RUM Logging
Redirects Routing and AB testing Shielding
NYC
presents
Add extra data
fiddle/7a6cbc28
GeoIP headers
2
NYC
presents
➔ Create a fiddle
➔ Set a title
➔ Write VCL in RECV to add GeoIP data
➔ Learn about real time linting and autocompletion
➔ Run the fiddle
➔ Learn about header highlighting
fiddle/7a6cbc28
GeoIP headers
2
NYC
presents
fiddle/7a6cbc28
RECV:
set req.http.client-geo-continent = client.geo.continent_code;
set req.http.client-geo-country = client.geo.country_code;
set req.http.client-geo-city = client.geo.city;
set req.http.client-geo-lat = client.geo.latitude;
set req.http.client-geo-lng = client.geo.longitude;
set req.http.edge-geo-datacenter = server.datacenter;
GeoIP headers
2
NYC
presents
Exercises
Basics GeoIP headers RUM Logging
Redirects Routing and AB testing Shielding
NYC
presents
No origin request
fiddle/b62fa9ee
RUM Logging
3
Batch and
write to log
NYC
presents
➔ Create a fiddle
➔ Set a title
➔ Write RECV code to detect relevant requests and trigger error
➔ Write ERROR code to log the data
➔ Update the request URL to give it some data to log
➔ Run the fiddle
➔ Learn about real time log streaming and fiddle log capture
➔ For more info on RUM beacons see
fastly.com/blog/beacon-termination-edge
fiddle/b62fa9ee
RUM Logging
3
NYC
presents
fiddle/b62fa9ee
RECV:
if (req.url ~ "^/log/?") {
error 901;
}
RUM Logging
3
NYC
presents
fiddle/b62fa9ee
ERROR:
if (obj.status == 901) {
set req.url = querystring.add(req.url, "city", client.geo.city);
set req.url = querystring.add(req.url, "dc", server.datacenter);
log "syslog " req.service_id " logger_name :: " req.url.qs;
set obj.status = 204;
set obj.response = "No content";
return (deliver)
}
RUM Logging
3
NYC
presents
fiddle/b62fa9ee
Request URL:
/log?field1=foo&field2=bar&field3=42
RUM Logging
3
NYC
presents
Exercises
Basics GeoIP headers RUM Logging
Redirects Routing and AB testing Shielding
NYC
presents
fiddle/7fc13ab0
Redirects
4
Redirect response
for old URLs
Allow valid URLs
to go to origin
NYC
presents
➔ Create a fiddle
➔ Set a title
➔ Learn about edge dictionaries
➔ Write a dictionary in INIT
➔ Write RECV code to detect relevant requests and trigger error
➔ Write ERROR code to output a redirect response
➔ Update the request URL to one of the source paths
➔ Run the fiddle
➔ For a more complex solution try fiddle/36470bb6
fiddle/7fc13ab0
Redirects
4
NYC
presents
fiddle/7fc13ab0
INIT:
table redirects {
"/source1": "/dest1",
"/source2": "/dest2"
}
Redirects
4
NYC
presents
fiddle/7fc13ab0
RECV:
if (table.lookup(redirects, req.url)) {
error 811;
}
Redirects
4
NYC
presents
fiddle/7fc13ab0
ERROR:
if (obj.status == 811) {
set obj.status = 301;
set obj.http.Location = "https://" req.http.host table.lookup(redirects,
req.url);
set obj.response = "Moved permanently";
return (deliver);
}
Redirects
4
NYC
presents
fiddle/7fc13ab0
Request URL:
/source1
Redirects
4
NYC
presents
Exercises
Basics GeoIP headers RUM Logging
Redirects Routing and AB testing Shielding
NYC
presents
fiddle/2df6df84
Routing and AB testing
5
Preflight request to
check test is enabled
A
BRestart and send
real request to A or B
NYC
presents
➔ Create a fiddle and set a title
➔ Configure three backends - A, B, and preflight API
➔ Write RECV code to choose backend
➔ Write DELIVER code to restart after preflight request
➔ Learn about specifying backends in fiddle
➔ Learn about randomness functions
➔ Understand how fiddle displays restarts
fiddle/2df6df84
Routing and AB testing
5
NYC
presents
fiddle/2df6df84
Request URL:
/
Routing and AB testing
5
Backends:
https://programming-edge.glitch.me
https://www.fastly.com
https://www.example.com
NYC
presents
fiddle/2df6df84
RECV:
if (req.http.Test-Status) {
# TODO
} else {
set req.http.Orig-URL = req.url;
set req.url = "/check";
set req.backend = F_origin_0;
}
Routing and AB testing
5
NYC
presents
fiddle/2df6df84
DELIVER:
if (resp.http.Test-Status && req.restarts == 0) {
set req.http.Test-Status = resp.http.Test-Status;
set req.url = req.http.Orig-URL;
unset req.http.Orig-URL;
restart;
}
Routing and AB testing
5
NYC
presents
fiddle/2df6df84
RECV:
if (req.http.Test-Status) {
if (req.http.Test-Status == "ENABLED" && randombool(1,2)) {
set req.backend = F_origin_2;
log "syslog " req.service_id " logger :: Chose B experience";
} else {
set req.backend = F_origin_1;
log "syslog " req.service_id " logger :: Chose A experience";
}
set req.http.Backend = req.backend;
} else {
set req.http.Orig-URL = req.url;
set req.url = "/check";
set req.backend = F_origin_0;
}
Routing and AB testing
5
NYC
presents
fiddle/2df6df84
FETCH:
set beresp.http.Vary = if (beresp.http.Vary, beresp.http.Vary ", ", "") "Backend";
Routing and AB testing
5
NYC
presents
fiddle/2df6df84
DELIVER:
if (resp.http.Test-Status && req.restarts == 0) {
set req.http.Test-Status = resp.http.Test-Status;
set req.url = req.http.Orig-URL;
unset req.http.Orig-URL;
restart;
}
set resp.http.Vary = regsub(resp.http.Vary, "(,s+)?Backend", "");
Routing and AB testing
5
NYC
presents
★ Add a Test-Status: ENABLED header to skip the preflight
★ See what happens when your preflight request hits cache
fiddle/2df6df84
Routing and AB testing
5
NYC
presents
Exercises
Basics GeoIP headers RUM Logging
Redirects Routing and AB testing Shielding
NYC
presents
fiddle/32f9f345
Shielding
6
Close to
user
Close to
origin
NYC
presents
➔ Create a fiddle and set a title
➔ Write RECV code to prevent caching at the edge
➔ Enable shielding and clustering
➔ Learn about shielding
➔ Run the fiddle twice
➔ Learn about targeting VCL at edge or shield POPs
fiddle/32f9f345
Shielding
6
NYC
presents
fiddle/32f9f345
RECV:
if (!req.http.Fastly-FF) {
return(pass);
}
Shielding
6
OPTIONS:
Clustering, Shielding
NYC
presents
★ Try inverting the Fastly-FF test to move the pass to the Shield,
instead of the Edge
★ Try doing hash_always_miss instead of pass
fiddle/32f9f345
Shielding
6
NYC
presents
Wrapping up
NYC
presents
● Explore more solutions at fastly.com/demos
● Try fiddle yourself at fiddle.fastlydemo.net
● Got something good? Let us know in community.fastly.com or
email me: abetts@fastly.com.
● Remember all fiddle content is public
Programming the edge

More Related Content

Altitude NY 2018: Programming the edge workshop