コンテンツにスキップ

ワーカーの動作

Cloudflare Workersは、ブラウザやNode.jsのJavaScriptと似た動作をしますが、コードについて考える方法にはいくつかの違いがあります。内部的には、WorkersランタイムはV8エンジンを使用しています — ChromiumやNode.jsで使用されているのと同じエンジンです。Workersランタイムは、ほとんどの現代のブラウザで利用可能な多くの標準APIも実装しています。

ブラウザやNode.js用に書かれたJavaScriptの違いは、ランタイムで発生します。個々のマシン上で実行されるのではなく(例えば、ブラウザアプリケーションや中央集権的なサーバー上で)、Workers関数はCloudflareのグローバルネットワーク上で実行されます - 数百の場所に分散された数千のマシンから成る成長中のグローバルネットワークです。

Cloudflare network global map

これらのマシンのそれぞれは、Workersランタイムのインスタンスをホストしており、それぞれのランタイムは数千のユーザー定義アプリケーションを実行することができます。このガイドでは、これらの違いのいくつかをレビューします。

詳細については、コンテナなしのクラウドコンピューティングに関するブログ記事を参照してください。

主な違いは、Isolates、リクエストごとの計算、分散実行の3つです。

Isolates

V8は、あなたのコードにアクセスできる変数と安全に実行される環境を提供する軽量コンテキストであるisolatesを調整します。isolatesは、関数が実行されるサンドボックスと考えることもできます。

A single instance of the runtime can run hundreds or thousands of isolates, seamlessly switching between them. Each isolate’s memory is completely isolated, so each piece of code is protected from other untrusted or user-written code on the runtime. Isolates are also designed to start very quickly. Instead of creating a virtual machine for each function, an isolate is created within an existing environment. This model eliminates the cold starts of the virtual machine model.

Unlike other serverless providers which use containerized processes each running an instance of a language runtime, Workers pays the overhead of a JavaScript runtime once on the start of a container. Workers processes are able to run essentially limitless scripts with almost no individual overhead. Any given isolate can start around a hundred times faster than a Node process on a container or virtual machine. Notably, on startup isolates consume an order of magnitude less memory.

Scheduling and routing
Scheduling and routing
HTTP client
HTTP client
HTTP server
HTTP server
Inbound
HTTP proxy
[Not supported by viewer]
Outbound
HTTP proxy
[Not supported by viewer]
Supervisor
[Not supported by viewer]
Main Runtime Process
Main Runtime Process
Outer Sandbox
Outer Sandbox
Disk
Disk
Control plane
[Not supported by viewer]
HTTP
[Not supported by viewer]
Cap'n Proto RPC
[Not supported by viewer]
In-process calls
[Not supported by viewer]
Other
[Not supported by viewer]
V8 Isolate
[Not supported by viewer]
V8 Isolate
[Not supported by viewer]
V8 Isolate
[Not supported by viewer]
V8 Isolate
[Not supported by viewer]
Process
Sandbox
[Not supported by viewer]
V8 Isolate
[Not supported by viewer]
Scheduling and routing
Scheduling and routing
Process
Sandbox
[Not supported by viewer]
V8 Isolate
[Not supported by viewer]
Scheduling and routing
Scheduling and routing

特定のisolateは独自のスコープを持っていますが、isolateは必ずしも長寿命ではありません。isolateは、以下の理由でスピンダウンされ、追い出されることがあります:

  • マシンのリソース制限。
  • 疑わしいスクリプト - isolateサンドボックスから抜け出そうとするものと見なされるもの。
  • 個別のリソース制限

このため、一般的には、これらの事態を考慮しない限り、グローバルスコープに可変状態を保存しないことが推奨されます。

CloudflareがWorkersランタイムでのセキュリティをどのように扱っているかに興味がある場合は、IsolatesがセキュリティおよびSpectre脅威の軽減にどのように関連しているかについてもっと読むことができます

リクエストごとの計算

Most Workers are a variation on the default Workers flow:

export default {
async fetch(request, env, ctx) {
return new Response('Hello World!');
},
};

For Workers written in ES modules syntax, when a request to your *.workers.dev subdomain or to your Cloudflare-managed domain is received by any of Cloudflare’s data centers, the request invokes the fetch() handler defined in your Worker code with the given request. You can respond to the request by returning a Response object.

分散実行

Isolatesは、リクエストの期間中は弾力性があり、継続的に利用可能ですが、稀にisolateが追い出されることがあります。Workerが公式の制限に達したり、リクエストが実行されているマシンでリソースが非常に逼迫している場合、ランタイムはイベントが適切に解決された後にisolateを選択的に追い出します。

他のすべてのJavaScriptプラットフォームと同様に、単一のWorkersインスタンスは、単一スレッドのイベントループ内で複数のリクエストを処理することができます。つまり、リクエストを処理している間に他のリクエストが来た場合、asyncタスク(例えばfetch)を待機している間に他のリクエストが処理される可能性があります(または処理されない可能性があります)。 ユーザーのリクエストが同じまたは異なるWorkerのインスタンスにルーティングされる保証はないため、Cloudflareはグローバル状態を使用したり変更したりしないことを推奨しています。

関連リソース

  • fetch()ハンドラー - WorkerへのHTTPリクエストがfetch()ハンドラーに渡される方法を確認します。
  • Request - 受信したHTTPリクエストがfetch()ハンドラーに渡される方法を学びます。
  • Workersの制限 - Workerのサイズ、起動時間などを含むWorkersの制限について学びます。