In this webinar, we'll discuss how to swap out wrestling Varnish Configuration Language (VCL) with writing modern JavaScript. Using JavaScript will not only make it easier to configure traditional caching behavior but also open up new possibilities for moving additional functionality away from the origin.
As part of this, we'll briefly cover:
- How to perform custom routing
- Creating custom authorization
- Building an API Gateway
Thank you for joining us on today’s webinar to talk about how you can:
My name is Remy and I’m a product marketing manager here at cloudflare focused on all things serverless and workers...
Today we’re going to cover: ...
So, what is cloudflare workers?
Workers is an extremely lightweight serverless execution environment, that can spin up in single digit milliseconds in response to events like web requests and disappear just as quickly. Since they are so lightweight we actually run them on Cloudflare’s global network of 154 data centers to handle billions of events every day. And we’re using Workers to help you gain unprecedented control of how Cloudflare performs caching and security logic for any application.
But, before we get to talking about how you can control Cloudflare, you just heard me mention the term “serverless.” And some of you might be thinking...what exactly did he mean by that?
You might have heard some jokes about serverless and maybe even seen some funny photos of racks without servers. But, to get at the heart of what serverless can do for us...I’m going to take us back in time just a little bit to bring everybody up to speed on how serverless compute even came to be.
When we think about compute, it started with what we’ll call generation one...which is one big metal box and one lonely application in a single location that could take months to change and deploy...
it moved on to generation two...where that lonely application, it found some friends with the help of virtualization and reduced deployment time down to weeks or days, but the applications were all still tied to the big metal box and in a single location…
then something new happened with generation 3...some of those applications from generation two got to exchange their steel and silicone cages for a nice soft cloud and deploy times measured in hours and minutes. But we asked ourselves, what if we could reduce deployment time to seconds and reduce compute time to miliseconds? If we could do that, we could have have functions as a service everywhere, and deploy things differently.
And this is where the next generation of computing comes into play...serverless computing, which is really just a fully managed compute environment where you don’t have to worry about configuring or running a server, VM or, container…and at Cloudflare we’ve taken this concept and expanded it to our entire network using Cloudflare Workers.
We were able to expand Workers to the network through the use of Isolates. Isolates drastically reduce the amount overhead required to run sandboxed code. They have such a low overhead so code can be spun up and fired up quickly from cold start, and with workers, it is about a 5 millisecond cold start time.
By using JavaScript and Isolates, Workers provides both a quick and robust platform for building basic and complex functionality on the Cloudflare network.
So we’ve talked about what Workers are and how they came to be, but you’re probably wondering how exactly you might benefit by switching from writing VCL to JavaScript.
Well the first is that you get to benefit from first class support of a full featured and modern programming language that is JavaScript instead of a less popular domain specific language like VCL.
While Varnish Configuration Language is fine for basic routing and logic based on headers and other request parameters, it can become very cumbersome as needs grow and expand in scope beyond….well….configuration. We can see here how even simple variable declarations can be needlessly complex in VCL.
The second is the greater flexibility that you get from Cloudflare Workers. Since Cloudflare Workers were built with a broader set of uses cases in mind, you can do things like call out to multiple API endpoints (either 3rd party or your own), fetch multiple images or documents, read or write to storage (including our own Workers Key Value store), and directly control how every object is cached. Including the use of custom cache keys and TTLs.
Finally, Cloudflare Workers are built on top of the Service Worker API specification and JavaScript, one of the most popular programming languages, which means that there are always plenty of people around to help with any issues and not just on the Cloudflare forums at community.cloudflare.com, but in the broader community as well. Here you can see the rather comical difference in activity on StackOverflow for JavaScript vs VCL.
So, now let’s move on to some example use cases that demonstrate the power of Cloudflare Workers...
Here you can see that we can easily perform custom routing based on things like headers, location, device type, and more… For this example we’re reading both the user agent and a custom debug header to decide what page to send the requester. Something to note is that in this example (instead of redirecting) we can actually directly return the response without changing the URL so that the visitor sees the appropriate content without any extra round trips.
Here is just a small sample of a larger implementation that uses JSON Web Tokens and HMAC to validate both the user as well as Cloudflare’s connection to the backend object storage. You can see here that Cloudflare Workers supports the WebCrypto API standard which allows for easy and secure, generation and verification of tokens right in the Worker.
Cloudflare Workers can not only act as a traditional API gateway that performs authorization checks and does basic routing to various endpoints, but it can also combine multiple endpoints into a single response. Cloudflare Workers can help you expose, cache, and secure endpoints however you need without forcing unnecessary changes to your underlying code.
So if this sounds interesting to you and you’re looking to switch, we have some resources to help you get started...
Please head over to the cloudflare developer portal at developers.cloudflare.com/workers/ where you can find tons of detailed recipes covering what we talked about today and more as well as recipes showing you common VCL usage patterns and how they can be implemented in Workers. We also have many more in depth tutorials and case studies (including one on authorization using JSON Web Tokens and HMAC) available on our blog under the serverless tag. You can also contact us at …..
And the last thing I would like to leave you with is that you’re in good company...
We’ve had many customers switch from VCL including NPM (nodejs package manager) to benefit from the extra flexibility and speed they can get with workers. Through clever use of Workers they are able to deliver 1 billion code packages per day.
Thank for attending today’s webinar!
How much does it cost to use Workers? $.5 / million and $5