Cloudflare Workers

Fast serverless functions hosted by Cloudflare's CDN

Tom • Cloud, Net & Web •

Serverless Functions on the Edge Network

Cloudflare has a whole suite of cloud-based products that offer you a “backend-as-a-service”, and Cloudflare Workers is an important part of this suite. Cloudflare Workers are serverless functions, written in Javascript (or preferrably, Typescript) that are hosted in a Node.js-environment powered by the V8 engine. You can write them locally and deploy via the “wrangler”-CLI or develop directly in the cloud.

// This is a minimal example for a
// serverless function hosed by
// Cloudflare. You define this code
// in a JS-file and upload it via the
// 'wrangler'-CLI.

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response("Hello world")
}

javascriptLanguageCheck out all coding posts

If you’re hearing of “serverless functions” here for the first time, here’s a quick explanation. Serverless functions are literally functions that encapsulate your service’s business logic and that are hosted in the cloud. The main advantage is that you don’t have to waste a single second of your time on hosting, patching and maintaining a server yourself. You just “upload” your functions and they’re available globally in a few minutes.

Cloudflare particularly promotes the speed of its products, as it controls a very large content delivery network, with many endpoints across the planet. Of course, this CDN is also leveraged for the Cloudflare Workers.

How to distribute your backend around the globe

Every time you deploy your Cloudflare Workers, they get shared and updated across many locations worldwide. This allows Cloudflare to route incoming requests to the always nearest endpoint to invoke your serverless functions, which reduces the network delay to a few milliseconds. This routing done by Cloudflare is transparent when making requests - the routing and balance-loading is done in the background automatically.

To better understand the topology of Cloudflare Worker’s architecture, here’s a breakdown from high to low level:

As mentioned, Cloudflare has a CDN that consists of many servers around different locations on the globe
A single location itself consists of many servers that process incoming requests
Zooming in on a single server, we see that this machine runs a so-called “Workers runtime”, which is powered by the Javascript V8-engine
Finally, your serverless function is just one “isolate” inside this runtime; a single runtime can hosts thousands of functions simultaneously

A particularly important piece of information: each Cloudflare Worker you deploy has its own memory, isolated from every other process. This ensures that your data is really only accessible by your instance.

When to use Cloudflare Workers, and when not

Cloudflare Workers sound very exciting and are definitely worth trying out, even if you have already existing apps that use serverless functions from other providers. Yet this might also point out the main reason why Cloudflare’s product is harder to adopt: Many services, such as Next.js combined with Vercel or Firebase by Google offer such solutions as well. If you’re currently using their products, switching to Cloudflare doesn’t make much sense.

But if you’re starting a new project, it’s well worth considering using Cloudflare Workers as your backend. Their usage is practically unlimited in terms of use cases. Furthermore Cloudflare offers a large range of other services that might just fit your requirements.

Actions

Feedback

Suggested Posts