Member-only story
Set up A/B testing using Cloudflare workers
If you’ve ever worked on a B2C web application, chances are you’ve encountered A/B testing. My first experience with A/B testing was back in 2011 at Yahoo!. Every new widget on the search page underwent A/B testing, with Yahoo! having its own setup for conducting these tests. However, in subsequent roles at different organizations, I noticed varying approaches to A/B testing, often limited to frontend modifications resulting in UI flickers.
Fast forward to this date. I use Cloudflare at work. Cloudflare offers a serverless feature known as “Workers,” which can be leveraged for A/B testing. The concept is straightforward:
- Divide incoming traffic into buckets
- Set a cookie, let us call this
abcookie
, for each bucket - Pass this cookie with every request to and from your application
- Implement the necessary actions in your application based on these cookies
- …
- Achieve success!
Now, in Cloudflare workers:
- Intercept every request
- Check for the
abcookie
- If the cookie was found, pass the request to origin
- If the cookie wasn’t found, create
abcookie
with its value to a test variant - Ensure the cookie is sent to origin for you to do your thing and to client for them to return it
- That’s all