Don't cache the answer to a feature flag
Is it safe to cache a page that sits behind a feature flag? Only if the flag gave everyone the same answer.
Evaluating a flag is cheap, so the flag is rarely the slow part. The slow part is the report, the product page or the API response, and that is what you cache. The cache key is the URL or the account id. The flag depends on the user.
So the first visitor in the beta fills the cache, and the next hour of visitors get the beta. Or the reverse: you turn the flag off because something is wrong, and the cache keeps serving the broken version until it expires. A kill switch that waits for a TTL is not much of a kill switch.
The other version of this is a flag read once at import time. It runs before the first download of rules has finished, gets the failover variant, and keeps it until the next deploy.
Cache the work, not the decision
Evaluate the flag on every request, then use the variant as part of the cache key. Each variant gets its own entry, and changing a rule changes which entry a user reads on their next request.
// Before: whoever asks first decides for everyone for 60s.
const html = await cache.getOrSet('report:' + accountId, () =>
render(featureflow.evaluate('new-reports', flagUser(user)).isOn(), accountId),
);
// After: the variant is part of the key.
const variant = featureflow.evaluate('new-reports', flagUser(user)).isOn() ? 'v2' : 'v1';
const html = await cache.getOrSet('report:' + accountId + ':' + variant, () =>
render(variant, accountId),
);The same rule applies one layer out. A CDN or framework page cache needs the variant in its key, or the flagged route needs to skip the shared cache. If neither is possible, cache the expensive data below the flag and let the cheap rendering above it run on each request.
Check three things
- The key. Does it change when the variant changes?
- The call site. Is
evaluateinside the request, not at module scope? - The switch. Flip the flag in staging and reload. Does the page change at once?
With Featureflow, rules are evaluated inside your process, so calling evaluate per request does not add a network round trip. The docs cover the Node SDK and waiting for ready().
Today, search for cache wrappers around code that reads a flag. Add the variant to each key.
#FeatureFlags#Caching#KillSwitch#ContinuousDelivery#DevOps
Flip a flag and see it straight away
Start free with Featureflow. Evaluate rules in-process on every request and keep an audit trail of every change.
Start Now (Free)Related Articles
Freeze the deploys, not the flags
A change freeze stops merges but not the flag dashboard. Freeze anything that adds risk, keep the switches that remove it, and enforce the rule with approvals.
What to put in the user you pass to a feature flag
The flag context starts as an id and ends up holding emails and names that no rule reads. Use an opaque id, pass only the attributes your rules compare, and build the user in one place.
Retiring an API version without a surprise outage
A shutdown date hits every client at once, including the cron job nobody owns. Gate v1 with a per-client flag, brown out a slice of traffic, and turn a client back on if they still need the old routes.