Lately, I’ve been very fascinated with serverless architecture because of its potentially infinite scalability without the need to configure a whole infrastructure. After some tinkering with the Serverless-Framework and AWS Lambda, I came across Cloudflare Workers and that changed everything for me.
Why I moved on from Serverless-Framework
Serverless-Framework is a great way to transition an existing project into the serverless world, but it comes at a cost. When you’re trying to serverless host an express.js project using Serverless-Framework it adds a wrapper to make it compatible with the platform of your choice, which adds code and increases load time significantly (at least from my experience). Since I always try to reduce bloat and package dependencies to a minimum I moved on from Serverless Framework to directly develop my project in AWS Lambda.
Why AWS Lambda wasn’t for me either
At first, I found it kind of confusing how to structure my project. Should I put it into multiple Lambda functions or just into one? Should I use a Rest API Gateway or the HTTP API Gateway? Do I want to configure my routes in GUI or code? In the end, I decided to create two functions, one as an authentication middleware and one for core functionality. I used the HTTP API Gateway since it was cheaper and I didn’t need the more complex GUI which the Rest API Gateway would have provided. I defined all my routes through the GUI to only call my functions when an existing endpoint is called and to prevent unnecessary executions of my functions when a bot tries to call random endpoints. Once my API was done, I deployed it and it worked great until I stopped calling it for an hour or so, then it took ages to get a response. Turns out that AWS Lambda uses Docker containers in the background which are being stopped after some time of inactivity and need to spin up again with the first call, which takes some time, this process is called a “cold start”. Even though it would be possible to set up a cronjob which calls it every few minutes to prevent it from going to sleep this wasn’t for me either so I went back to the drawing board and after some googling, I came across Cloudflare Workers.
Why I fell in love with Cloudflare Workers
Working with node.js and express.js for some time now it kinda caught me off guard that Cloudflare Workers doesn’t use node.js but the “raw” V8 engine instead. In other words, it’s a Chrome web browser running headlessly on a server, which is kinda cool since it reduces overhead even more but made developing for it a bit different than I was used to since you’re essentially developing for a browser. Cloudflare provides great documentation with many examples and a command-line tool called wrangler which helped a lot to get going with Workers. I again started from scratch and since there weren’t many tools that suited my needs, I created my own router/middleware framework inspired by express.js and a JWT library, which is built on the natively supported crypto.subtle API. My highest priorities in creating those tools were to be lightweight and have ZERO dependencies, to reduce bloat and maximize performance, while also providing all features needed to create a functional REST API. After finishing my project and deploying it to Cloudflare Workers I switched my frontend to the new URL and I couldn’t believe how fast it was, also there were no cold start delays anymore. On top of that Cloudflare Workers is significantly cheaper than AWS Lamba, allowing 100,000 requests per day for free and 10,000,000 requests per month for $5.
After trying some of the serverless offerings I found my favorite platform. Providing high performance for an affordable price, great documentation and quick assistance from the Workers Team and the community on the Cloudflare Discord Server made me fall in love with Cloudflare Workers.