← Recipes

阅读中文版本

Blue-Green Switch with Cloudflare Workers

Route a portion of traffic between two origins using a Worker and shift weight programmatically to enable instant rollback.

~40 mincloudflareopsdeployment

Task

Implement weighted blue-green routing in a Cloudflare Worker with a switch stored as a variable.

When to use this

Cutover between environments needs to be instant and reversible without waiting for DNS propagation.

Prerequisites

  • Cloudflare account with Workers
  • Two origin environments
  • A deploy identity for Workers

Steps

1. Route by weight read from an environment variable.

export default {
  async fetch(request, env) {
    const toGreen = Math.random() * 100 < Number(env.GREEN_WEIGHT || 0);
    const origin = toGreen ? 'green.internal' : 'blue.internal';
    const url = new URL(request.url);
    url.hostname = origin;
    return fetch(new Request(url, request));
  },
};

Expected output: Traffic splits according to GREEN_WEIGHT and both origins remain warm.

2. Shift weight and roll back by variable change.

wrangler deploy --var GREEN_WEIGHT:100
wrangler deploy --var GREEN_WEIGHT:0

Expected output: The switch and rollback happen in seconds without DNS propagation delay.

Troubleshooting

Problem

Sticky sessions break

Solution

Route by a hash of a stable identifier so a user stays on one origin.

Problem

Origin host header wrong

Solution

Set the target hostname explicitly so TLS and routing match.

Limitations

  • Adds a Worker hop to every request.
  • Weight is probabilistic, not a precise quota.
  • Requires both origins healthy at all times.

What this recipe does

Implement weighted blue-green routing in a Cloudflare Worker with a switch stored as a variable.

Cutover between environments needs to be instant and reversible without waiting for DNS propagation.

Prerequisites

  • Cloudflare account with Workers
  • Two origin environments
  • A deploy identity for Workers

Procedure

  1. Route by weight read from an environment variable.
export default {
  async fetch(request, env) {
    const toGreen = Math.random() * 100 < Number(env.GREEN_WEIGHT || 0);
    const origin = toGreen ? 'green.internal' : 'blue.internal';
    const url = new URL(request.url);
    url.hostname = origin;
    return fetch(new Request(url, request));
  },
};

Expected result: Traffic splits according to GREEN_WEIGHT and both origins remain warm.

  1. Shift weight and roll back by variable change.
wrangler deploy --var GREEN_WEIGHT:100
wrangler deploy --var GREEN_WEIGHT:0

Expected result: The switch and rollback happen in seconds without DNS propagation delay.

How to confirm it worked

Run the commands above and confirm every expected result in order. If a step does not produce the stated output, use the troubleshooting notes before continuing. A passing run shows changing the weight variable shifts traffic immediately and setting it to zero restores the previous origin.

Troubleshooting

Sticky sessions break — Route by a hash of a stable identifier so a user stays on one origin.

Origin host header wrong — Set the target hostname explicitly so TLS and routing match.

Limitations

  • Adds a Worker hop to every request.
  • Weight is probabilistic, not a precise quota.
  • Requires both origins healthy at all times.