Prompt
How do I run A/B tests at the edge?
Latest observation
Running A/B tests at the edge means deciding which variant a user gets close to the user—usually in a CDN or edge compute layer—before your origin app is hit. This reduces latency and lets you personalize or test without a round trip to your backend.
Core idea
- A request arrives at the edge.
- The edge logic assigns the user to a variant, e.g.
AorB. - The assignment is made sticky so the same user keeps seeing the same variant.
- The edge forwards the request to the appropriate origin/content path or rewrites the response.
Common patterns
1) Cookie-based bucketing
- On first request, the edge randomly assigns variant A or B.
- It sets a cookie like
exp_checkout= B. - Future requests read the cookie and route consistently.
Good for: stable experiments, easy measurement
Watch out for: cookie consent/privacy requirements
2) Header-based bucketing
- The edge reads a user ID, session ID, or geo/device header.
- It hashes the value to a bucket.
Good for: logged-in users or deterministic assignment
Watch out for: changing IDs can break stickiness
3) Hashing + percentage rollout
- Use a stable identifier and hash it to
0–99. - Example:
- 0–49 = control
- 50–99 = treatment
Good for: predictable traffic splits
Watch out for: ensure the hash is uniform
4) Edge KV / config-driven experiments
- The edge checks a config store or key-value store for active experiments.
- This lets you change splits or target segments without redeploying.
Good for: operational control
Watch out for: cache propagation delays
Architecture
Basic flow
- Client → CDN/Edge worker
- Edge worker:
- checks experiment config
- determines bucket
- sets cookie / header
- routes to origin path or serves edge-generated content
- Origin serves variant-specific content or returns a generic page that the edge modifies
Example routing options
/→/variant-a/→/variant-b- same URL, but edge injects variant-specific HTML/JS
- same URL, but edge adds a header consumed by the origin
Best practices
Make assignment deterministic
Don’t rely only on randomization per request. Use:
- cookie
- user ID
- session ID
- stable hash
Keep variants isolated
- Ensure A and B differ only in the test element.
- Avoid unintended differences from caching, personalization, or A/B-specific assets.
Handle caching carefully
Edge + CDN caching can accidentally serve the wrong variant to the wrong user.
Use:
- cache keys that include experiment variant
- variant-specific paths
Varyheaders only when appropriate- separate caches per variant if needed
Track exposure and conversion
Log:
- experiment ID
- variant
- user/session identifier
- timestamp
- request path
- conversion events later
Make sure “exposed” means the user actually saw the variant, not just that they were assigned to it.
Predefine experiment rules
- traffic split
- target audience
- duration
- success metrics
- stop conditions
Watch for edge limitations
Edge runtimes may have:
- limited CPU/memory
- no long-lived state
- restricted libraries
- limited observability
So keep logic simple and fast.
Example pseudo-logic
if (cookie.exp_variant exists) {
variant = cookie.exp_variant
} else {
variant = hash(user_id or anonymous_id) % 100 < 50 ? "A" : "B"
set cookie exp_variant=variant
}
if (variant === "A") {
route to /control
} else {
route to /treatment
}
What to measure
Typical metrics:
- conversion rate
- click-through rate
- bounce rate
- revenue per visitor
- page load time
- downstream retention
Use statistical testing to determine whether differences are significant.
Common pitfalls
- Inconsistent bucketing across devices or browsers
- Cache bleed between variants
- Bots/internal traffic contaminating results
- Sample ratio mismatch from implementation bugs
- Changing experiment logic mid-test
- Too many simultaneous experiments causing interaction effects
Practical tools/platforms
You can do edge A/B testing with:
- Cloudflare Workers
- Fastly Compute
- Akamai EdgeWorkers
- Vercel Edge Middleware
- Netlify Edge Functions
- custom CDN + Lambda@Edge
If you want, I can also show:
- a Cloudflare Worker example,
- an architecture diagram, or
- a production checklist for edge A/B testing.