Prompt
how to set TTL for feature flag caching
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
Setting a TTL for feature flag caching depends on where the flags are cached and which feature flag system/language you’re using. The general idea is:
- Store flags in memory or a cache
- Attach an expiration time (TTL)
- Refresh from the flag provider when expired
Common approaches
1) In-memory cache with TTL
If you’re caching feature flags yourself, set a timestamp and invalidate after TTL.
Example logic:
- Cache flags at
t = now - TTL = 60 seconds
- If
now - cached_at > 60s, fetch fresh flags
Pseudocode:
cache = {"flags": None, "cached_at": 0}
TTL = 60
def get_flags():
if cache["flags"] is None or time.time() - cache["cached_at"] > TTL:
cache["flags"] = fetch_flags_from_server()
cache["cached_at"] = time.time()
return cache["flags"]
2) Redis or distributed cache TTL
If you use Redis, set the key expiry directly.
Example (Redis):
SET feature_flags '{"new_ui": true}' EX 60
Or in code:
redis.set("feature_flags", flags_json, ex=60)
3) SDK-managed caching
Many feature flag providers already cache flags internally and offer configuration for refresh intervals.
Examples:
- LaunchDarkly: streaming/polling config, cache is typically managed by SDK
- Unleash:
refreshInterval/ polling interval - Flagsmith: client cache settings / polling interval
- ConfigCat: cache refresh mode and TTL-like settings
In these cases, you usually configure:
- Polling interval
- Cache refresh interval
- Stale-while-revalidate behavior
What TTL should you choose?
Typical values:
- 10–30 seconds for near-real-time changes
- 1–5 minutes for less critical flags
- Longer if you want fewer network calls and can tolerate delay
Best practice
If flags control sensitive behavior, use:
- Short TTL
- Background refresh
- Fallback/default values
- Cache invalidation on config updates if supported
If you tell me which language/framework and which feature flag service you’re using, I can give you the exact code/config.