-
Notifications
You must be signed in to change notification settings - Fork 28
Add distributed flag definition cache provider interface #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
haacked
wants to merge
3
commits into
main
Choose a base branch
from
haacked/byoc-flag-eval
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+777
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Redis-based distributed cache for PostHog feature flag definitions. | ||
| # | ||
| # This example demonstrates how to implement a FlagDefinitionCacheProvider | ||
| # using Redis for multi-instance deployments (leader election pattern). | ||
| # | ||
| # Usage: | ||
| # require 'redis' | ||
| # require 'posthog' | ||
| # require_relative 'redis_flag_cache' | ||
| # | ||
| # redis = Redis.new(host: 'localhost', port: 6379) | ||
| # cache = RedisFlagCache.new(redis, service_key: 'my-service') | ||
| # | ||
| # posthog = PostHog::Client.new( | ||
| # api_key: '<project_api_key>', | ||
| # personal_api_key: '<personal_api_key>', | ||
| # flag_definition_cache_provider: cache | ||
| # ) | ||
| # | ||
| # Requirements: | ||
| # gem install redis | ||
|
|
||
| require 'json' | ||
| require 'securerandom' | ||
|
|
||
| # A distributed cache for PostHog feature flag definitions using Redis. | ||
| # | ||
| # In a multi-instance deployment (e.g., multiple serverless functions or | ||
| # containers), we want only ONE instance to poll PostHog for flag updates, | ||
| # while all instances share the cached results. This prevents N instances | ||
| # from making N redundant API calls. | ||
| # | ||
| # Uses leader election: | ||
| # - One instance "wins" and becomes responsible for fetching | ||
| # - Other instances read from the shared cache | ||
| # - If the leader dies, the lock expires (TTL) and another instance takes over | ||
| # | ||
| # Uses Lua scripts for atomic operations, following Redis distributed lock | ||
| # best practices: https://redis.io/docs/latest/develop/clients/patterns/distributed-locks/ | ||
| class RedisFlagCache | ||
| LOCK_TTL_MS = 60 * 1000 # 60 seconds, should be longer than the flags poll interval | ||
| CACHE_TTL_SECONDS = 60 * 60 * 24 # 24 hours | ||
|
|
||
| # Lua script: acquire lock if free, or extend if we own it | ||
| LUA_TRY_LEAD = <<~LUA | ||
| local current = redis.call('GET', KEYS[1]) | ||
| if current == false then | ||
| redis.call('SET', KEYS[1], ARGV[1], 'PX', ARGV[2]) | ||
| return 1 | ||
| elseif current == ARGV[1] then | ||
| redis.call('PEXPIRE', KEYS[1], ARGV[2]) | ||
| return 1 | ||
| end | ||
| return 0 | ||
| LUA | ||
|
|
||
| # Lua script: release lock only if we own it | ||
| LUA_STOP_LEAD = <<~LUA | ||
| if redis.call('GET', KEYS[1]) == ARGV[1] then | ||
| return redis.call('DEL', KEYS[1]) | ||
| end | ||
| return 0 | ||
| LUA | ||
|
|
||
| # @param redis [Redis] A redis client instance | ||
| # @param service_key [String] Unique identifier for this service/environment, | ||
| # used to scope Redis keys. Examples: "my-api-prod", "checkout-service" | ||
| # | ||
| # Redis keys created: | ||
| # - posthog:flags:{service_key} — cached flag definitions (JSON) | ||
| # - posthog:flags:{service_key}:lock — leader election lock | ||
| def initialize(redis, service_key:) | ||
| @redis = redis | ||
| @cache_key = "posthog:flags:#{service_key}" | ||
| @lock_key = "posthog:flags:#{service_key}:lock" | ||
| @instance_id = SecureRandom.uuid | ||
| end | ||
|
|
||
| # Retrieve cached flag definitions from Redis. | ||
| # | ||
| # @return [Hash, nil] Cached flag definitions, or nil if cache is empty | ||
| def flag_definitions | ||
| cached = @redis.get(@cache_key) | ||
| return nil unless cached | ||
|
|
||
| JSON.parse(cached) | ||
| end | ||
|
|
||
| # Determine if this instance should fetch flag definitions from PostHog. | ||
| # | ||
| # Atomically either acquires the lock (if free) or extends it (if we own it). | ||
| # | ||
| # @return [Boolean] true if this instance is the leader and should fetch | ||
| def should_fetch_flag_definitions? | ||
| result = @redis.eval(LUA_TRY_LEAD, keys: [@lock_key], argv: [@instance_id, LOCK_TTL_MS.to_s]) | ||
| result == 1 | ||
| end | ||
|
|
||
| # Store fetched flag definitions in Redis. | ||
| # | ||
| # @param data [Hash] Flag definitions to cache | ||
| def on_flag_definitions_received(data) | ||
| @redis.set(@cache_key, JSON.dump(data), ex: CACHE_TTL_SECONDS) | ||
| end | ||
|
|
||
| # Release leadership if we hold it. Safe to call even if not the leader. | ||
| def shutdown | ||
| @redis.eval(LUA_STOP_LEAD, keys: [@lock_key], argv: [@instance_id]) | ||
| end | ||
| end |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module PostHog | ||
| # Interface for external caching of feature flag definitions. | ||
| # | ||
| # EXPERIMENTAL: This API may change in future minor version bumps. | ||
| # | ||
| # Enables multi-worker environments (Kubernetes, load-balanced servers, | ||
| # serverless functions) to share flag definitions via an external cache, | ||
| # reducing redundant API calls. | ||
| # | ||
| # Implement the four required methods on any object and pass it as the | ||
| # +:flag_definition_cache_provider+ option when creating a {Client}. | ||
| # | ||
| # == Required Methods | ||
| # | ||
| # [+flag_definitions+] | ||
| # Retrieve cached flag definitions. Return a Hash with +:flags+, | ||
| # +:group_type_mapping+, and +:cohorts+ keys, or +nil+ if the cache | ||
| # is empty. Returning +nil+ triggers an API fetch when no flags are | ||
| # loaded yet (emergency fallback). | ||
| # | ||
| # [+should_fetch_flag_definitions?+] | ||
| # Return +true+ if this instance should fetch new definitions from the | ||
| # API, +false+ to read from cache instead. Use for distributed lock | ||
| # coordination so only one worker fetches at a time. | ||
| # | ||
| # [+on_flag_definitions_received(data)+] | ||
| # Called after successfully fetching new definitions from the API. | ||
| # +data+ is a Hash with +:flags+, +:group_type_mapping+, and +:cohorts+ | ||
| # keys (plain Ruby types, not Concurrent:: wrappers). Store it in your | ||
| # external cache. | ||
| # | ||
| # [+shutdown+] | ||
| # Called when the PostHog client shuts down. Release any distributed | ||
| # locks and clean up resources. | ||
| # | ||
| # == Error Handling | ||
| # | ||
| # All methods are wrapped in +begin/rescue+. Errors are logged but never | ||
| # break flag evaluation: | ||
| # - +should_fetch_flag_definitions?+ errors default to fetching (fail-safe) | ||
| # - +flag_definitions+ errors fall back to API fetch | ||
| # - +on_flag_definitions_received+ errors are logged; flags remain in memory | ||
| # - +shutdown+ errors are logged; shutdown continues | ||
| # | ||
| # == Example | ||
| # | ||
| # cache = RedisFlagCache.new(redis, service_key: 'my-service') | ||
| # client = PostHog::Client.new( | ||
| # api_key: '<project_api_key>', | ||
| # personal_api_key: '<personal_api_key>', | ||
| # flag_definition_cache_provider: cache | ||
| # ) | ||
| # | ||
| module FlagDefinitionCacheProvider | ||
| REQUIRED_METHODS = %i[ | ||
| flag_definitions | ||
| should_fetch_flag_definitions? | ||
| on_flag_definitions_received | ||
| shutdown | ||
| ].freeze | ||
|
|
||
| # Validates that +provider+ implements all required methods. | ||
| # Raises +ArgumentError+ listing any missing methods. | ||
| # | ||
| # @param provider [Object] the cache provider to validate | ||
| # @raise [ArgumentError] if any required methods are missing | ||
| def self.validate!(provider) | ||
| missing = REQUIRED_METHODS.reject { |m| provider.respond_to?(m) } | ||
| return if missing.empty? | ||
|
|
||
| raise ArgumentError, | ||
| "Flag definition cache provider is missing required methods: #{missing.join(', ')}. " \ | ||
| 'See PostHog::FlagDefinitionCacheProvider for the required interface.' | ||
| end | ||
| end | ||
| end |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.