VCL problem-solving: SOA routing & non-ASCII support | Fastly

In “How to solve anything, part 1,” we discussed Andrew Betts’ clever tips for using Fastly’s Custom Varnish Configuration Language (VCL) to collect data at the edge. In this post, we’ll look at how Nikkei (the Financial Times parent company) uses VCL to deal with a service-oriented architecture as well as write synthetic responses with non-ASCII (i.e., Japanese) characters.

SOA routing

Send requests to multiple microservice backends, which is great if you:

  • Have a microservice architecture

  • Have many back ends and one domain name

  • Add and remove services regularly

Having a service-oriented architecture (SOA) can present a problem when you’re trying to track down your various services. “Almost all of us have some sort of microservices architecture on our back end,” said Andrew. FT has different back end services that are dealing with different parts of the website, with requests coming into the same domain but being routed to different back ends. To address this, they have Fastly make the routing decisions and decide which microservice to send the request to. As Andrew put it, it’s simple to do using a bit of VCL:

SOA-routing-VCL

FT then automates this process with a bit of orchestration. They have a JSON file that lists all of their back end microservices and have a VCL template file which combines VCL with some handlebars code. Finally, they have a javascript Node.js application that zips the two together and creates their final VCL which they then upload through the API. They do all this in their continuous integration (CI) environment.

SOA-routing-VCL2

Tools and techniques

  • In VCL, you can set your own custom back end, so you can just change which back end is used for routing this request:set req.backend = {{backendName}};

  • You can match against a regular expression pattern using a regular expression comparison operator:if (req.url ~ "{{pattern}}")

  • If you are choosing a back end that has its own specific host, remember to set the host header:set req.http.Host = "{{backendhost}}";

  • For a “neat and simple way of uploading VCLs from the command line,” Andrew recommends using the open source FT Fastly tools package.

Check out the slides from Andrew’s talk for in-depth examples of the code he used for SOA routing and automation.

Beyond ASCII

Use these tips to embed non-ASCII (American Standard Code for Information Interchange) content in your VCL file, which is helpful if your users don’t speak English, since you can only write ASCII in VCL files.

“At Nikkei we’re obviously writing Japanese,” Andrew noted. “I inevitably ended up wanting to write a synthetic response in VCL in Japanese.” But this happened:

non-ASCII-results

Andrew wrote some javascript to convert any non-ASCII character to an HTML escape sequence:

"string"
  .split('')
  .map(
    char => char.codePointAt(0) < 128 ?
    char :
    "&#"+char.codePointAt(0)+";"
)
.join('') ;

The result of which, when wrapped put into the VCL, looks like:

synthetic {"Responsive
Nikkei&#12450;&#12523;&#12501;&#12449;&#12503;&#12525;&#12464;&#
12521;&#12512;&#12398;&#12513;&#12531;&#12496;&#12540;&#12398;&#
30342;&#27096;&#12289;&#12450;&#12523;&#12501;&#12449;&#12496;&#
12540;&#12472;&#12519;&#12531;&#12398;&#12469;&#12452;&#12488;&#
12395;&#12450;&#12463;&#12475;&#12473;&#12391;&#12365;&#12394;&#
12356;&#22580;&#21512;&#12289;rnfeedback@nex.nikkei.co.jp
&#12414;&#12391;&#12381;&#12398;&#26088;&#36899;&#32097;&#12367;
&#12384;&#12373;&#12356;&#12290;"};

“It’s not the prettiest thing in the world, but it’s effective,” Andrew noted. He also discovered that you can base64 encode it, using the digest.base64_decode function.

synthetic digest.base64_decode(
{"IlJlc3BvbnNpdmUgTmlra2Vp44Ki44Or44OV44Kh44OX44Ot44Kw44Op44Og44
Gu44Oh44Oz44OQ44O844Gu55qG5qeY44CB44Ki44Or44OV44Kh44OQ44O844K444
On44Oz44Gu44K144Kk44OI44Gr44Ki44Kv44K744K544Gn44GN44Gq44GE5aC05Z
CI44CBcm5mZWVkYmFja0BuZXgubmlra2VpLmNvLmpwIOOBvuOBp
+OBneOBruaXqOmAo+e1oeOBj+OBoOOBleOBhOOAgiI="});

It’s not quite as clear, but “you can save a few bytes.”

Curious about using restarts in VCL? Head over to part 3 of this series.

Rogier Mulhuijzen
Senior Professional Services Engineer
Anna MacLachlan
Content Marketing Manager
Published

3 min read

Want to continue the conversation?
Schedule time with an expert
Share this post
Rogier Mulhuijzen
Senior Professional Services Engineer

Rogier “Doc” Mulhuijzen is a senior professional services engineer and Varnish wizard at Fastly, where performance tuning and troubleshooting have formed the foundation of his 18-year career. When he’s not helping customers, he sits on the Varnish Governance Board, where he helps give direction and solve issues for the Varnish open source project. In his spare time, he likes to conquer all terrains by riding motorcycles, snowboarding, and sailing.

Anna MacLachlan
Content Marketing Manager

Anna MacLachlan is Fastly’s Content Marketing Manager, where she talks to brands and partners to tell stories about scale, security, and performance. She received her MA in Comparative Literature from NYU and loves megafauna and mountains.

Ready to get started?

Get in touch or create an account.