8 Replies
Cloudflare Workers is a very diverse platform, but think of it as a way to easily write code (JavaScript/TypeScript/Python/WASM) to run on an serverless architecture (that is, there is no central server for workers, every instance/call to a worker is as if that code is running for the first time, at the location closest to the user)
Workers integrate well with a lot of other parts of Cloudflare too, and let you easily deploy front-end or backend code
in over simplistic terms:
- stores a bit of code in a hard drive somewhere
- whenever requests to domain X are received, downloads bit of code and runs the javascript in it
- once it has run, it will be shut down and code deleted if there are no more requests
very very useful, and its simple nature doesnt mean you have to run tiny apps, the bit of code can be multiple megabytes big and because its all in CF infra it doesn’t matter that much
thank you freddie. Downside?
cold starts: the bit of code has to be downloaded before it gets ran which means sometimes it’ll take a little bit longer, then it gets cached in the location and catches up
no memory state: because you don’t own the server, if you need anything in-memory like a quick LRU cache or something like that, it is now unfeasible. circumvented with a KV service like cloudflare KV, redis, dragonfly etc.
scalability (both a pro and con): if you get ABSOLUTELY BLASTED you may end up being charged extra for the extra traffic as many more functions needed to be spun up and ran. if this was a good old server you owned, this number of requests would instead have crashed the server but the cost would have been fixed
Workers have (pretty-much) eliminated cold starts! (tldr, in the time it takes to tls handshake/initially connect, workers loads in all the code) https://blog.cloudflare.com/eliminating-cold-starts-with-cloudflare-workers/
The Cloudflare Blog
Eliminating cold starts with Cloudflare Workers
A “cold start” is the time it takes to load and execute a new copy of a serverless function for the first time. It’s a problem that’s both complicated to solve and costly to fix.
i thought of mentioning it has been getting more and more negligible but for the sake of clarity i decided to mention it as a characteristic of serverless. it even makes it easy to visualise why it happens when explaining it to people
i recently explained it to my girlfriend and this is normally the "ohh i get it" moment