Why did Cloudflare create workers?
Cloudflare provided limited features and options that developers could build in-house. There was not much flexibility for customers to build features themselves. To enable users to write code on their servers deployed around the world, they had to allow untrusted code to run, with low overhead. This needed to process millions of requests per second and that too at a very fast speed.
Customers couldn’t write their own code without the team’s supervision. It would be expensive to use traditional virtualization and container technologies like Kubernetes let alone run thousands of Kubernetes pod at 155 data centers of Cloudflare would be resource intensive. Enter Cloudflare’s ‘Workers’ to solve these issues.
Features of Workers
#1 ‘Isolates’- Run code from multiple customers
#2 Cold Starts
Workers facilitate the concept of ‘cold start’ when a new copy of code has to be started on a machine. In the Lambda world, this means spinning up a new containerized process which can delay requests for as much as ten seconds ending up in a terrible user experience. A Lambda can only process one single request at a time. A new Lambda has to be cold-started every time an additional concurrent request is recieved. If a Lambda doesn’t get a request soon enough, it will be shut down and it all starts again. Since Workers don’t have to start a process, Isolates start in 5 milliseconds. It scales and deploys quickly, entirely upgrading existing Serverless technologies.
#3 Context Switching
A normal context switch performed by an OS can take as much as 100 microseconds. When multiplied by all the Node, Python or Go processes running on average Lambda servers, this leads to a heavy overhead. This splits the CPUs power between running the customer’s code and switching between processes. An Isolate-based system runs all of the code in a single process which means there are no expensive context switches. The machine can invest virtually all of its time running your code.
The V8 was designed to be multi-tenant. It runs the code from the many tabs in a user’s browser in isolated environments within a single process. Since memory is often the highest cost of running a customer’s code, V8 lowers it and dramatically changes the cost economics.
It is not safe to run code from multiple customers within the same process. Testing, fuzzing, penetration testing, and bounties are required to build a truly secure system of that complexity.
The open-source nature of V8 helps in creating aanisolation layer that helps Cloudflare take care of the security aspect.
Cloudlfare’s Workers also allows users to build responses from multiple background service requests either to the Cloudflare cache, application origin, or third party APIs. They can build conditional responses for inbound requests to assess and subsequently block or reroute malicious or unauthorized requests.
All of this at just a third of what AWS costs, remarked an astute Twitter observer.
Cloudflare is burying the lede here a bit: they have built a cloud platform that costs a third of what AWS costs and can run any language, by compiling it down to WebAssembly and running it in V8 isolates. https://t.co/rngbL2BNyg
— Laurie Voss (@seldo) November 11, 2018
Running code through WebAssembly
Also, if a user cannot recompile their processes, they won’t be able to run them in an Isolate.
This has been nicely summarised in the above mentioned tweet. He notes that WebAssembly modules are already in the npm registry and it creates the potential for npm to become the dependency management solution for every programming language. He mentions that the “availability of open source libraries to achieve the task at hand is the primary reason people pick a programming language”. This leads us to the question of “How does software development change when you can use any library anytime?”
You can head over to the Cloudflare blog to understand more about containerless cloud computing.
Cloudflare Workers KV, a distributed native key-value store for Cloudflare Workers
Cloudflare’s decentralized vision of the web: InterPlanetary File System (IPFS) Gateway to create distributed websites