This is a guest post by Tejas Dinkar, who is the Head of Engineering at Quintype, a platform for digital publishing. He’s continually looking for ways to make applications run faster and cheaper. You can find him on Github and Twitter.
TL;DR: Check out create-cloudflare-worker.
At Quintype, we are continually looking for new and innovative ways to use our CDN. Quintype moved to Cloudflare last year, partly because of the power of Cloudflare Workers. Workers have been a very important tool in our belt, and in this blog post we will talk a little bit about our worker development lifecycle.
Cloudflare Workers have drastically changed the way we architect and deploy things at Quintype. Quintype is a platform that powers many publishers, including many high volume ones like The Quint, BloombergQuint, Swarajya, and Fortune India. An average month sees hundreds of millions of page views come through our network.
Maintaining a healthy cache hit ratio is the key to scaling a content heavy app. Ensuring requests are served from Cloudflare is faster, and cheaper, as requests do not have to come through to an origin. We actively architect our apps to ensure that we maintain a healthy cache hit ratio, and every little thing that we can do to increase it really helps.
Even increasing our cache hit ratio from 90% to 95% means that we’ve reduced the traffic to our origin by about 50%.
Workers allow us to do all sorts of magic. For example, here are some of the things we do in our worker:
- Strip out URL parameters that aren’t used, especially things like UTM params
- Short circuit API requests for content that require a login
- A metered paywall which allows users to read 3 free articles a week
- Bucketing users and annotating requests for A/B testing
- ... not to mention the fact that it’s ridiculously cheap.
In many ways, the Cloudflare Worker is the next evolution of serverless computing. The code we write is distributed to 100s of locations, and scales up as traffic increases. Cloudflare has even launched (in beta) a Key Value store, so we can store data at 100s of locations as well.
In fact, Workers can even handle traffic without ever forwarding to an upstream origin, creating a truly distributed computing platform. Last week, we went live with our first origin-less worker, which implements a metered paywall purely using KV Store to store information about which content a user has read.
The one thing we were looking to improve was the development workflow for a worker. The ability to start, build, test and deploy a worker from scratch. Enter create-cloudflare-worker.
create-cloudflare-worker is a new framework that lets anyone bootstrap their CF worker. Just run
$ npm init cloudflare-worker your-worker-name
and you are ready to go!
Your worker is located at `src/index.js`. By default, it doesn't do too much, it just forwards your request to the upstream origin, and adds a response header depending on the response status. You can run your worker locally, using a local app server as the upstream target.
$ npm run build && npm start # Starts the worker on port 4000, forwarding requests to port 3000
Projects created with create-cloudflare-worker come prebuilt with the Webpack configuration you need to ensure that you can use npm modules, and bundling everything for the correct targets.
It also comes with jest configured for integration testing, so that you can run end to end tests against your built worker.
$ npm run build && npm test
Finally, create-cloudflare-worker also comes with npm scripts to deploy your worker to Cloudflare (via the REST API). This makes it suitable for a workflow where the worker is built and deployed via Circle CI, Github Actions, or any other CD pipeline. Deploying your worker is as easy as running
$ CF_ACCOUNT=acct-id CF_WORKER_NAME=worker-name CF_EMAILfirstname.lastname@example.org CF_AUTH_KEY=auth-key npm run deploy
Please see the readme on Github for a list of commands available today.
create-cloudflare-worker is stable enough for you to start working with it today, and we are still looking for help with supporting WebAssembly, and with filling out the readme with different recipes for building workers.
We hope that create-cloudflare-worker will significantly reduce your time to get started with Cloudflare Workers. Happy Hacking!