Example
Text in Expression Editor:
Selected operation under Modify request header: Set dynamic
Header name: Cloudflare-Workers-Version-Key
Value: regex_replace(http.request.uri.path, "/asset/(.*)", "${1}")
Gradual Deployments give you the ability to incrementally deploy new versions of Workers by splitting traffic across versions.
Using gradual deployments, you can:
The following section guides you through an example usage of gradual deployments. You will choose to use either Wrangler or the Cloudflare dashboard to:
Create a new "Hello World"
Worker using the create-cloudflare
CLI (C3) and deploy it.
Answer yes
or no
to using TypeScript. Answer yes
to deploying your application. This is the first version of your Worker.
To create a new version of the Worker, edit the Worker code by changing the Response
content to your desired text and upload the Worker by using the wrangler versions upload
command.
This will create a new version of the Worker that is not automatically deployed.
Use the wrangler versions deploy
command to
create a new deployment that splits traffic between two versions of the Worker. Follow the interactive prompts to create a deployment with the versions uploaded in step #1 and step #2. Select your desired percentages for each version.
Run a cURL command on your Worker to test the split deployment.
You should see 10 responses. Responses will reflect the content returned by the versions in your deployment. Responses will vary depending on the percentages configured in step #3.
You can test also target a specific version using version overrides.
Run wrangler versions deploy
again and follow the interactive prompts. Select the version uploaded in step 2 and set it to 100% deployment.
Response
content) and upload the Worker.You should see 10 responses. Responses will reflect the content returned by the versions in your deployment. Responses will vary depending on the percentages configured in step #6.
By default, the percentages configured when using gradual deployments operate on a per-request basis — a request has a X% probability of invoking one of two versions of the Worker in the deployment.
You may want requests associated with a particular identifier (such as user, session, or any unique ID) to be handled by a consistent version of your Worker to prevent version skew. Version skew occurs when there are multiple versions of an application deployed that are not forwards/backwards compatible. You can configure version affinity to prevent the Worker’s version from changing back and forth on a per-request basis.
You can do this by setting the Cloudflare-Workers-Version-Key
header on the incoming request to your Worker. For example:
For a given deployment, all requests with a version key set to foo
will be handled by the same version of your Worker. The specific version of your Worker that the version key foo
corresponds to is determined by the percentages you have configured for each Worker version in your deployment.
You can set the Cloudflare-Workers-Version-Key
header both when making an external request from the Internet to your Worker, as well as when making a subrequest from one Worker to another Worker using a service binding.
You may want to extract a version key from certain properties of your request such as the URL, headers or cookies. You can configure a Ruleset Engine rule on your zone to do this. This allows you to specify version affinity based on these properties without having to modify the external client that makes the request.
For example, if your worker serves video assets under the URI path /assets/
and you wanted requests to each unique asset to be handled by a consistent version, you could define the following request header modification rule:
Example
Text in Expression Editor:
Selected operation under Modify request header: Set dynamic
Header name: Cloudflare-Workers-Version-Key
Value: regex_replace(http.request.uri.path, "/asset/(.*)", "${1}")
You can use version overrides to send a request to a specific version of your Worker in your gradual deployment.
To specify a version override in your request, you can set the Cloudflare-Workers-Version-Overrides
header on the request to your Worker. For example:
Cloudflare-Workers-Version-Overrides
is a Dictionary Structured Header.
The dictionary can contain multiple key-value pairs. Each key indicates the name of the Worker the override should be applied to. The value indicates the version ID that should be used and must be a String.
A version override will only be applied if the specified version is in the current deployment. The versions in the current deployment can be found using the wrangler deployments list --experimental-versions
command or on the Workers Dashboard under Worker > Deployments > Active Deployment.
You may want to test a new version in production before gradually deploying it to an increasing proportion of external traffic.
In this example, your deployment is initially configured to route all traffic to a single version:
Version ID | Percentage |
---|---|
db7cd8d3-4425-4fe7-8c81-01bf963b6067 | 100% |
Create a new deployment using wrangler versions deploy --experimental-versions
and specify 0% for the new version whilst keeping the previous version at 100%.
Version ID | Percentage |
---|---|
dc8dcd28-271b-4367-9840-6c244f84cb40 | 0% |
db7cd8d3-4425-4fe7-8c81-01bf963b6067 | 100% |
Now test the new version with a version override before gradually progressing the new version to 100%:
Due to global uniqueness, only one version of each Durable Object can run at a time. This means that gradual deployments work slightly differently for Durable Objects.
When you create a new gradual deployment for a Durable Object Worker, each Durable Object instance is assigned a Worker version based on the percentages you configured in your deployment. This version will not change until you create a new deployment.
This example assumes that you have previously created 3 Durable Objects and derived their IDs from the names “foo”, “bar” and “baz”.
Your Worker is currently on a version that we will call version “A” and you want to gradually deploy a new version “B” of your Worker.
Here is how the versions of your Durable Objects might change as you progress your gradual deployment:
Deployment config | ”foo" | "bar" | "baz” |
---|---|---|---|
Version A: 100% | A | A | A |
Version B: 20% Version A: 80% | B | A | A |
Version B: 50% Version A: 50% | B | B | A |
Version B: 100% | B | B | B |
This is only an example, so the versions assigned to your Durable Objects may be different. However, the following is guaranteed:
When using gradual deployments, you may want to attribute Workers invocations to a specific version in order to get visibility into the impact of deploying new versions.
A new ScriptVersion
object is available in Workers Logpush. ScriptVersion
can only be added through the Logpush API right now. Sample API call:
ScriptVersion
is an object with the following structure:
Use the Version metadata binding in to access version ID or version tag in your Worker.
You can only create a new deployment with the last 10 uploaded versions of your Worker.