User Agents

Web browsers and everyday web tech, explained

Cache Feature Flags Without Stale Tenant Settings

· 5 min read · Settings and Privacy

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Caching feature flag evaluations improves application performance by reducing repeated calls to a feature flag service, but it risks serving stale tenant-specific configurations if not handled correctly. This guide shows how to cache flag evaluations per tenant while ensuring updates propagate reliably, maintaining consistency without sacrificing speed. You’ll implement a strategy combining time-based expiration, versioned tenant settings, and cache invalidation triggers to keep evaluations fresh.

What You'll Need

  • A feature flag system (e.g., LaunchDarkly, Flagsmith, or a custom solution)
  • Access to tenant configuration storage (database, key-value store)
  • A caching layer (Redis, Memcached, or in-memory cache with TTL support)
  • Ability to assign and track version identifiers for tenant settings
  • Middleware or service layer where flag evaluations occur

Step-by-Step

  1. Assign a version identifier to each tenant’s settings
    For every tenant, maintain a version field (e.g., settings_version) in your configuration store. Increment this number whenever a feature flag or tenant-specific setting changes. This version acts as a cache fingerprint.
  2. Store flag evaluations in cache using a composite key
    When caching a flag evaluation, use a key that combines the tenant ID, flag key, and current settings version. For example: flag:tenant_123:dark_mode:v5. This ensures that when the version increments, the cache key changes, and stale data is bypassed.
  3. Set a reasonable TTL on cached evaluations
    Even with versioning, apply a time-to-live (TTL) of 30–60 seconds. This provides a safety net in case version updates are missed or delayed, preventing indefinite stale data exposure.
  4. Read the current settings version before evaluating a flag
    Before checking a flag’s value, retrieve the tenant’s current settings_version from persistent storage. Use this value to construct the cache key. Avoid caching the version itself unless it’s also versioned or short-lived.
  5. Check cache using the versioned key
    Attempt to retrieve the flag value using the composite key. If present and not expired, return it immediately. This is your fast path.
  6. Fetch and evaluate from source if cache misses
    If the cache does not contain the value, retrieve the latest flag configuration from your feature flag service, evaluate it for the tenant, and store the result using the same versioned key and TTL.
  7. Propagate version updates on configuration changes
    Whenever a flag is updated for a tenant (via UI, API, or automation), increment the tenant’s settings_version. This can be done in the same transaction as the flag update to ensure atomicity.
  8. Broadcast version changes if using distributed caching
    In multi-instance environments, publish a message (e.g., via Redis Pub/Sub or a message queue) when a version changes. Other instances can listen and proactively invalidate relevant local caches or refresh metadata.
  9. Implement a fallback to versionless cache in emergencies
    Optionally, maintain a secondary cache key without the version (e.g., flag:tenant_123:dark_mode:latest) with a very short TTL (5–10 seconds). Use this only if version retrieval fails, and log the incident for diagnosis.
  10. Monitor cache hit rate and version drift
    Log metrics for cache hits, misses, and the time between version updates and cache invalidation. Alert if versions are not incrementing or if stale evaluations persist beyond TTL.

Common Mistakes

  • Using only TTL without versioning: Relying solely on expiration means changes won’t be reflected until the TTL ends, potentially minutes later.
  • Caching the settings version too long: If the version number itself is cached for too long, services may use outdated versions to build cache keys, defeating the purpose.
  • Forgetting atomic updates: Updating the flag and version in separate operations risks inconsistency if one succeeds and the other fails.
  • Not propagating version changes across instances: In clustered environments, one instance may see the new version while others continue serving stale cache.
  • Using global version counters: A single version for all tenants causes unnecessary cache invalidations across tenants when one changes.

Tips

  • Use a monotonic incrementing integer or timestamp for settings_version; avoid hashes or UUIDs for simplicity and ordering.
  • Keep version lookup fast—store it in the same row or document as tenant metadata.
  • Wrap flag evaluation in a dedicated service class to centralize caching logic and reduce duplication.
  • Log the cache key and version used in evaluation during debugging to trace stale data sources.
  • Test version rollover scenarios in staging, especially during high-frequency flag changes.

Troubleshooting

If tenants receive stale flag values, first verify that the settings_version was incremented during the update. Check logs to confirm the new version is being read by the application. If versioning is correct, inspect cache key construction—ensure the version is included and formatted properly. In distributed systems, confirm that version change events are being published and received. If cache hit rates are low, review TTL settings and version churn rate; too-frequent version changes can reduce cache effectiveness. If the system serves stale data after a version increment, ensure all instances pull the latest version before building cache keys. Lastly, disable caching temporarily to isolate whether the issue lies in the flag service or the caching layer.

Frequently Asked Questions

Why not just shorten the TTL instead of using versioning?

Shorter TTLs increase backend load and may still delay updates. Versioning ensures immediate consistency when changes occur, while TTL acts as a backup.

Can I use timestamps instead of version numbers?

Yes, but ensure timestamps are synchronized across systems. A monotonically increasing counter is more reliable in distributed environments.

What if my cache layer doesn’t support TTL?

Use a separate cleanup job to periodically remove old entries, but prefer migrating to a TTL-capable system like Redis for reliability.

Should I cache every flag evaluation?

Only cache flags evaluated frequently per tenant. Rarely accessed flags may not benefit and could bloat the cache.

How do I handle multi-region deployments?

Use a globally consistent store for settings_version and replicate changes quickly. Consider regional caches with version-aware invalidation.