Add distributed flag definition cache provider interface#114
Open
Add distributed flag definition cache provider interface#114
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an experimental distributed cache-provider interface for feature flag definitions so multi-instance deployments can coordinate polling (leader fetch) and share results via an external cache (e.g., Redis), reducing redundant calls to PostHog.
Changes:
- Introduces
PostHog::FlagDefinitionCacheProviderinterface + validation helper. - Integrates cache-provider read/leadership/write/shutdown into
FeatureFlagsPoller’s polling lifecycle. - Adds deep symbolization utility + Redis example + comprehensive integration specs.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
lib/posthog/flag_definition_cache.rb |
Defines the cache-provider interface contract and validation. |
lib/posthog/feature_flags.rb |
Adds provider hooks for leader election, cached reads, cache writes, and shutdown handling. |
lib/posthog/utils.rb |
Adds deep_symbolize_keys to normalize cached JSON data back to symbol keys. |
lib/posthog/client.rb |
Plumbs flag_definition_cache_provider option through to the poller. |
lib/posthog.rb |
Requires the new cache provider interface file. |
examples/redis_flag_cache.rb |
Provides a Redis + Lua leader-election reference implementation. |
spec/posthog/flag_definition_cache_spec.rb |
Adds tests for cache integration, fallback behavior, and shutdown lifecycle. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Fix cache miss detection: treat nil as cache miss, allow empty flags arrays as valid cached values - Add explicit require for flag_definition_cache in feature_flags.rb - Fix test description to match actual method name
Remove the `@flag_definition_cache_provider` nil check that duplicates the `should_fetch` logic. Restructure cache specs to use `let` blocks instead of module-level constants and move MockCacheProvider outside the PostHog module.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This is a port of the
FlagDefinitionCacheProviderinterface implemented forposthog-pythonin this PR toposthog-ruby.FlagDefinitionCacheProviderinterface that enables multi-instance deployments (Kubernetes, serverless, load-balanced servers) to share feature flag definitions via an external cache (e.g., Redis), reducing redundant API calls to PostHogshould_fetch_flag_definitions?, shared reads viaflag_definitions, and cache writes viaon_flag_definitions_receivedChanges
lib/posthog/flag_definition_cache.rb— newFlagDefinitionCacheProvidermodule with interface validationlib/posthog/feature_flags.rb— integrate cache provider into_load_feature_flags, extract_fetch_and_apply_flag_definitions,_apply_flag_definitions, and_store_in_cache_providerlib/posthog/utils.rb— adddeep_symbolize_keysfor round-tripping JSON-serialized cache datalib/posthog/client.rb— accept and forwardflag_definition_cache_provideroptionexamples/redis_flag_cache.rb— reference Redis implementation with leader electionspec/posthog/flag_definition_cache_spec.rb— comprehensive tests covering cache hits, misses, error fallbacks, data integrity, and shutdown lifecycleError handling
All cache provider calls are wrapped in
begin/rescueso cache failures never break flag evaluation:should_fetch_flag_definitions?errors → defaults to fetching from APIflag_definitionserrors → falls back to API fetchon_flag_definitions_receivederrors → logged, flags remain in memoryshutdownerrors → logged, shutdown continuesTest plan
bundle exec rspec spec/posthog/flag_definition_cache_spec.rbbundle exec rspec