Cloudflare JavaScript Workers are $0.50 per million requests

综合技术 Hacker News (源链)

Exactly one year ago today, Cloudflare gave me a mission: Make it so people can run code on Cloudflare’s edge. At the time, we didn’t yet know what that would mean. Would it be container-based? A new Turing-incomplete domain-specific language? Lua? “Functions”? There were lots of ideas.

Eventually, we settled on what now seems the obvious choice: JavaScript, using the standard Service Workers API, running in a new environment built on V8. Five months ago, wegave you a preview of what we were building, and started the beta.

Today, with thousands of scripts deployed and many billions of requests served, Cloudflare Workers is now ready for everyone.

“Moving away from VCL and adopting Cloudflare Workers will allow us to do some creative routing that will let us deliver JavaScript to npm’s millions of users even faster than we do now. We will be building our next generation of services on Cloudflare’s platform and we get to do it in JavaScript!”

— CJ Silverio, CTO, npm, Inc.

What is the Cloud, really?

Historically, web application code has been split between servers and browsers. Between them lies a vast but fundamentally dumb
network which merely ferries data from point to point.

We don’t believe this lives up to the promise of “The Cloud.”

We believe the true dream of cloud computing is that your code lives in the network itself. Your code doesn’t run in “us-west-4” or “South Central Asia (Mumbai)”, it runs everywhere

More concretely, it should run where it is most needed. When responding to a user in New Zealand, your code should run in New Zealand. When crunching data in your database, your code should run on the machines that store the data. When interacting with a third-party API, your code should run wherever that API is hosted. When human explorers reach Mars, they aren’t going to be happy waiting a half an hour for your app to respond — your code needs to be running on Mars.

Cloudflare Workers are our first step towards this vision. When you deploy a Worker, it is deployed to Cloudflare’s entire edge network of over a hundred locations worldwide in under 30 seconds. Each request for your domain will be handled by your Worker at a Cloudflare location close to the end user, with no need for you to think about individual locations. The more locations we bring online, the more your code just “runs everywhere.”

Well, OK… it won’t run on Mars. Yet. You out there, Elon?

What’s a Worker?

Cloudflare Workers derive their name from Web Workers, and more specifically Service Workers, the W3C standard API for scripts that run in the background in a web browser and intercept HTTP requests. Cloudflare Workers are written against the same standard API, but run on Cloudflare’s servers, not in a browser.

Here are the tools you get to work with:

  • Execute any JavaScript code, using the latest standard language features.
  • Intercept and modify HTTP request and response URLs, status, headers, and body content.
  • Respond to requests directly from your Worker, or forward them elsewhere.
  • Send HTTP requests to third-party servers.
  • Send multiple requests, in serial or parallel, and use the responses to compose a final response to the original request.
  • Send asynchronous requests after the response has already been returned to the client (for example, for logging or analytics).
  • Control other Cloudflare features, such as caching behavior.

The possible uses for Workers are infinite, and we’re excited to see what our customers come up with. Here are some ideas we’ve seen in the beta:

  • Route different types of requests to different origin servers.
  • Expand HTML templates on the edge, to reduce bandwidth costs at your origin.
  • Apply access control to cached content.
  • Redirect a fraction of users to a staging server.
  • Perform A/B testing between two entirely different back-ends.
  • Build “serverless” applications that rely entirely on web APIs.
  • Create custom security filters to block unwanted traffic unique to your app.
  • Rewrite requests to improve cache hit rate.
  • Implement custom load balancing and failover logic.
  • Apply quick fixes to your application without having to update your production servers.
  • Collect analytics without running code in the user’s browser.
  • Much more.

Here’s an example.

// A Worker which:
// 1. Redirects visitors to the home page ("/") to a
//    country-specific page (e.g. "/US/").
// 2. Blocks hotlinks.
// 3. Serves images directly from Google Cloud Storage.
addEventListener('fetch', event => {

async function handle(request) {
  let url = new URL(request.url)
  if (url.pathname == "/") {
    // This is a request for the home page ("/").
    // Redirect to country-specific path.
    // E.g. users in the US will be sent to "/US/".
    let country = request.headers.get("CF-IpCountry")
    url.pathname = "/" + country + "/"
    return Response.redirect(url, 302)

  } else if (url.pathname.startsWith("/images/")) {
    // This is a request for an image (under "/images").
    // First, block third-party referrers to discourage
    // hotlinking.
    let referer = request.headers.get("Referer")
    if (referer &&
        new URL(referer).hostname != url.hostname) {
      return new Response(
          "Hotlinking not allowed.",
          { status: 403 })

    // Hotlink check passed. Serve the image directly
    // from Google Cloud Storage, to save serving
    // costs. The image will be cached at Cloudflare's
    // edge according to its Cache-Control header.
    url.hostname = ""
    return fetch(url, request)
  } else {
    // Regular request. Forward to origin server.
    return fetch(request)

It’s Really Fast

Sometimes people ask us if JavaScript is “slow”. Nothing could be further from the truth.

Workers uses the V8 JavaScript engine built by Google for Chrome. V8 is not only one of the fastest implementations of JavaScript, but one of the fastest implementations of any dynamically-typed language, period. Due to the immense amount of work that has gone into optimizing V8, it outperforms just about any popular server programming language with the possible exceptions of C/C++, Rust, and Go. (Incidentally, we will support those soon, via WebAssembly.)

The bottom line: A typical Worker script executes in less than one millisecond.
Most users are unable to measure any latency difference when they enable Workers — except, of course, when their worker actually improves
latency by responding directly from the edge.

On another speed-related note, Workers deploy fast, too. Workers deploy globally in under 30 seconds
from the time you save and enable the script.


Workers are a paid add-on to Cloudflare. We wanted to keep the pricing as simple as possible, so here’s the deal:

Get Started

“Cloudflare Workers saves us a great deal of time. Managing bot traffic without Workers would consume valuable development and server resources that are better spent elsewhere.”

— John Thompson, Senior System Administrator, MaxMind


9 Best JavaScript Charting Libraries For you to be reading this, it surely would be no news thatdata visualization has become a very critical part of the IT world toda...
Friday Hack Chat: Simulating Analog Simulation is a valuable tool for any hardware developer. Instead of building hardware for a long debugging session, you can emulate a microcontroller...
精心收集的 95 个超实用的 JavaScript 代码片段( ES6+ 编写)... 翻译自: 30 seconds of code 精心收集的有用的 JavaScript 代码片段,你可以在30秒或更少的时间内理解。 有了这些代码片段,获取你可以完全抛弃 Lodash 或 Underscore。 使用 Ctrl + F 或 command + F 来搜索...
浮点数计算引发的血泪史——以此为戒... 最近遇到很头疼的一件事,比较2个浮点数的大小,可能想想应该很简单。不就一行代码的事吗?但是也就是因为一行代码引发了不可想象的后果。 实际的开发中,难免会进行浮点数进行计算和判断大小。然后通过条件 if 语句执行相关的操作。 举个栗子,一个简单的比较2个浮点数的大小: ...
Static Typing: a Personal Journey Last February my company sent me to the Colorado School of Mines, my alma mater, to recruit computer science grads. Many of my former classmates were ...
Hacker News责编内容来自:Hacker News (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Cloudflare JavaScript Workers are $0.50 per million requests

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录