Cloudflare worker edge proxy

recipe-exchange

#1

We just released an edge proxy implemented as a Cloudflare worker script and published to npm as cloudflare-edge-proxy. It enables a bunch of features including a/b/n testing, canary releasing, gatekeeping, and SEO a/b/n testing. Full docs are on Github: https://github.com/DigitalOptimizationGroup/cloudflare-edge-proxy

Install with: npm install cloudflare-edge-proxy

A quick example of using it for an a/b/c test

The worker script:

import cloudflareEdgeProxy from "cloudflare-edge-proxy";

const config = {
    defaultBackend: "https://a.com",
    abtest: true,
    origins: [
        { url: "https://a.com" },
        { url: "https://b.com" },
        { url: "https://c.com" }
    ],
    salt: "test-abc-123"
};

const proxy = cloudflareEdgeProxy(config);

addEventListener("fetch", event => {
    event.respondWith(proxy(event));
});

And another example for canary releasing

Canary releasing can be used to gradually shift traffic from one backend to another. It should ONLY be used with two backends, (unlike a/b/n testing), so that users do not get reassigned as the traffic percentage is increased. An example config is shown below. To assure consistent assignment, for visitors, the weight parameter should only be increased.

import cloudflareEdgeProxy from "cloudflare-edge-proxy";

const config = {
    canary: true,
    weight: 50, // 0-100
    canaryBackend: "https://canary-backend.com",
    defaultBackend: "https://default-backend.com",
    salt: "canary-abc-123"
};

const proxy = cloudflareEdgeProxy(config);

addEventListener("fetch", event => {
    event.respondWith(proxy(event));
});

If this is useful to anyone of if there are any questions, please let us know!


#2

Hey I’m looking at A/B testing two different origin backends by doing something similar to this in VCL on a Varnish server, but just found out about edge workers… If I’m looking for specific cookies or query string parameters to use as seeds in a random function so visitors would consistently get served the same backend… could this handle that?


#3

Yes, definitely. This library assures users are consistently assigned to the same backend. If you set setCookie to true in the config, it will automatically set the cookie needed for consistent assignment. All you should need to do is npm install cloudflare-edge-proxy, create the script below (update the config for your needs), and then deploy it as a Cloudflare worker.

import cloudflareEdgeProxy from "cloudflare-edge-proxy";

const config = {
    defaultBackend: "https://a.com",
    abtest: true,
   // this line sets a cookie for you
    setCookie: true,
    origins: [
        { url: "https://a.com" },
        { url: "https://b.com" }
    ],
    salt: "test-abc-123"
};

const proxy = cloudflareEdgeProxy(config);

addEventListener("fetch", event => {
    event.respondWith(proxy(event));
});

You can read more over at the repo: