Back to article list

Solving cold starts with Cloudflare Workers

James Vansteenkiste

5 minute read

Did this post load fast? 🚀 This is in no small part because the content was delivered to your browser using Cloudflare's Workers. Workers provides a high performance global network with a0ms cold-start time.

Serverless today

Cloud providers can allocate machine resources on demand, scaling automatically to the requirements of your business. Two fundamental technologies make this possible today: virtualization, and containerization.

Virtualization is the process of running a virtual instance of a computer system in a layer abstracted from the actual hardware. Most commonly, it refers to running multiple operating systems on a computer system simultaneously.
opensource.com - https://opensource.com/resources/virtualization
Containerization is the packaging of software code with just the operating system libraries and dependencies required to run the code to create a single lightweight executable.
IBM - https://www.ibm.com/cloud/learn/containerization

Whether it’s Amazon’s ECS or Lamba, Google’s Cloud Run or Cloud Functions, or any of the other modern offerings – services are built on these two fundamental concepts.

Code deployed as containers to these cloud services becomes a small independent workload that can be passed around and run on different hardware. For all intents and purposes this works really well, but providers make a trade-off between keeping your workload ‘hot’ (ready to accept requests) and the cost of occupying infrastructure.

Typically you can take over the trade-off with controls given to you from the provider, but you will need to decide: low cost with longer initial start times, or hot and ready with a higher cost. Businesses generally accept the cost associated with always having a workload running to eliminate the 'cold start', especially when the workload has to load a user experience like a web application.

Lambda cold starts

My team at ASICS RaceRoster struggled with this very problem last year when experimenting with Next.js and AWS Lambda. We were attracted to the atomic nature, and billing per execution time. Using the custom server feature of Next.js, our demo projects looked very promising, and didn’t require a large time investment from our infrastructure team. Unfortunately we couldn't find a reliable solution to bring down cold requests consistently under three seconds, unacceptable for business requirements and set standards. This is especially the case when a lambda is delivering a visual interactive experience. I'm unsure I would recommend Lambda to serve a modern application experience like with Next.js, but can definitely see value for background tasks, or service to service cron workloads.

In the end, we chose AWS Elastic container service configured to guarantee a hot workload (never shutting off). Meaning we have accepted the cost of running a container every minute of every day to guarantee an instance response to user requests. Can we do better?

Cloudflare Workers

Faced with recent problems with Lambda at work, and not wanting to pay monthly to run this simple blog, Cloudflare Workers was the perfect solution.

You can think of Workers as a giant web browser running in the cloud. When a request is handled by Workers it’s akin to opening another tab. This comparison works well as a mental model because the Workers runtime uses the V8 engine — the same engine used by Chromium and Node.js. Instead of needing to package code in a container, you only need to adapt the worker syntax for application entry points. Unlike your web browser, the code is executed inside the server environment allowing your application access to privileged secrets, rendering content server-side, or completing costly calculations.

This blog is built with the latest version of Nuxt, an opinionated hybrid vue framework that deliveries high quality web experiences. With Cloudflare Workers I no longer worry about choosing between cold starts or paying a premium in addition to containerizing my application. This blog is now instantly available anywhere in the world, with very little configuration.

Welcome 👋, I'm James

Thanks for visiting my blog! I'm a software developer located in Southwestern Ontario, Canada. My passion is building modern, highly scalable web applications with Typescript using NestJS, and NuxtJS.