A serverless execution environment at the edge
By Leander GillesEdge Computing
Serverless
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
return new Response("Hello world")
}
Azure Functions and sometimes takes 10s to respond
How can Cloudflare claim 0ms cold starts?
var http = require('http');
var url = require('url');
var fs = require('fs');
var http = require('http');
import { parse } from "cookie"
"A change to the Workers runtime must never break an application that is live in production."
Developers may now opt into backwards-incompatible fixes by setting a compatibility date.
# wrangler.toml
compatibility_date = "2021-09-20"
How does the Workers runtime implement compatibility dates?
if (compatibility_date < "2021-09-20") {
// run old behavior
} else {
// run new behavior
}
Workers are given 30s to complete any requests
| Free plan | Paid Plan - Unbound | Paid plan - Bundled | |
|---|---|---|---|
| Requests | 100,000 / day | 1 million, + $0.15/million | 10 million, +$0.50/million |
| Duration | 10ms CPU time / invocation | 400,000 GB-s, + $12.50/million GB-s1,2 | 50 ms CPU time / invocation |
Minimum of $5 per month on the Paid Plan
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const value = await NAMESPACE.get("first-key")
if (value === null) {
return new Response("Value not found", {status: 404})
}
return new Response(value)
}
Perfect use case:
Real-time collaborative document editing
Not yet generally available 😢
# wrangler.toml
type = "webpack"
name = "my-worker-dev"
account_id = "12345678901234567890"
zone_id = "09876543210987654321"
route = "dev.example.com/*"
vars = { ENVIRONMENT = "dev" }
[env.staging]
name = "my-worker-staging"
vars = { ENVIRONMENT = "staging" }
route = "staging.example.com/*"
[env.production]
name = "my-worker"
vars = { ENVIRONMENT = "production" }
routes = [
"example.com/foo/*",
"example.com/bar/*"
]
wrangler publish --env staging
Want to learn more?
Open-source headless CMS build by Cloudflare:
$ brew install cloudflare/cloudflare/cloudflared
$ cloudflared login
$ cloudflared tunnel --url http://localhost:3000