Cloudflare Workers
Cloudflare Workersは、開発者にサーバーレスコードを瞬時にCloudflareのグローバルネットワークにデプロイする力を与えます。
Cloudflare Workersは、実行モデルとアーキテクチャにおいて他のサーバーレスコンピューティングプロバイダーとは大きく異なります。
単一のWorkerプロジェクトは、開発者が望むように複雑または単純なロジックを持つことができます。小規模なプロジェクトは、単一のルートで小さなHTMLページを返すWorkerのように見えるかもしれません。より複雑なWorkerプロジェクトは、複数のドメイン、各ドメインの複数のルート、および各ルートの異なるロジックにまたがります。開発者は、自分のWorkerプロジェクトのアーキテクチャの複雑さを決定します。
あなたのアプリケーションは、複数のWorkerで構成され、協力してエンドユーザーに単一の体験を提供することができます。Workersは、ストレージ、メディア、AIなどの他のCloudflare Developer Platform機能とも統合できます。これについては、Developer Platformモジュールでさらに学びます。
Workers runtime ↗は、JavaScript標準に準拠し、ウェブ相互運用性を持つように設計されています。Workers runtimeはV8エンジンを使用しており — ChromiumやNode.jsで使用されているのと同じエンジンで、オープンソース版のworkerd ↗があります。
Cloudflare Workers runtimeは、Cloudflareの300以上の都市にわたるグローバルネットワークのすべてのデータセンター ↗で実行されます。各Workerは独自のアイソレート内で実行されます。アイソレートアーキテクチャがWorkersを効率的にします。
Workersは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.
Most Workers are a variation on the default Workers flow:
export default { async fetch(request, env, ctx) { return new Response('Hello World!'); },};export default { async fetch(request, env, ctx): Promise<Response> { return new Response('Hello World!'); },} satisfies ExportedHandler<Env>;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.
このページを読むことで、あなたは以下のことを学びました:
- Workerプロジェクトの基本的な構成。
- Cloudflareネットワーク上でのWorkersの実行の基本。
- リクエストからレスポンスへのフローの実行方法。
次のモジュールでは、最初のWorkerをCloudflareのグローバルネットワークに構築してデプロイします。
- コンテナなしのクラウドコンピューティング ↗ - Cloudflareの文脈におけるコンテナとアイソレートの違いを詳述したブログ記事。
- Workersの仕組み - Workers runtimeと従来のブラウザやNode.jsの違いを学びます。
- キャッシュの仕組み - WorkersがCloudflareキャッシュとどのように相互作用するかを学びます。
この学習パスを改善するためや、欠落または不正確な情報を報告するために、GitHubで問題を報告 ↗してください。
質問をしたり、あなたが構築しているものを共有したり、他の開発者とプラットフォームについて議論するために、Cloudflare Developer PlatformコミュニティにDiscordで接続 ↗してください。