feat(feature-flags): TTL evaluation cache with per-tenant invalidation#650
Open
almuslavish wants to merge 1 commit into
Open
feat(feature-flags): TTL evaluation cache with per-tenant invalidation#650almuslavish wants to merge 1 commit into
almuslavish wants to merge 1 commit into
Conversation
Adds an in-process TtlCache layer to FeatureFlagService.isEnabled with
update-driven invalidation and hit/miss/invalidation metrics.
- TtlCache: Map-based, configurable ttlMs + maxSize, no external deps
- Cache key is tenant-scoped (tenantId:flagKey[:userId]) — cross-tenant
leakage is impossible
- updateFlag and setOverride invalidate affected entries before returning
- bustFlag(flagKey, tenantId) exposed for the invalidation bus
- prom-client Counters: feature_flag_cache_{hits,misses,invalidations}_total
- Metrics re-exported from src/observability/index.ts
- Tests: cache-hit, TTL-expiry, two-tenant isolation, invalidation-on-update,
setOverride invalidation, bustFlag, disabled cache, maxSize eviction,
override evaluation
Resolves CredenceOrg#639
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
Closes #639
Adds an in-process TTL evaluation cache to
FeatureFlagServiceso that hot-pathisEnabledcalls avoid repeated backing-store reads.Changes
src/services/featureFlags/index.tsTtlCache<T>– lightweightMap-based cache with per-entry TTL and LRU-style max-size eviction (no new dependencies).FeatureFlagService– wraps aFeatureFlagStorewith:isEnabled(flagKey, tenantId, userId?)– cached; key istenantId:flagKey[:userId](cross-tenant leakage is impossible by construction)updateFlag– callsstore.updateFlagthenbustFlagbefore returning (correctness over speed)setOverride– callsstore.setOverridethen evicts the user-scoped cache entrybustFlag(flagKey, tenantId)– evicts all entries under thetenantId:flagKeyprefix; callable from the invalidation bus (src/cache/invalidationBus.ts)clearCache()– full flush (shutdown / testing)tenant_id+flag_keylabels):feature_flag_cache_hits_totalfeature_flag_cache_misses_totalfeature_flag_cache_invalidations_totalttlMs(default 30 s),maxSize(default 1 000),disabledflagsrc/services/featureFlags/index.test.tsVitest suite covering:
updateFlaginvalidates base and user-scoped entriessetOverrideinvalidates the specific user entry; override added after a cached miss is honoured immediatelybustFlagevicts all scoped entries; no metric emitted when nothing was evictedmaxSizeeviction: oldest entry is dropped when capacity is reachedgetFlag,listFlags,listFlagsWithOverridessrc/observability/index.tsRe-exports
featureFlagCacheHits,featureFlagCacheMisses,featureFlagCacheInvalidations.Testing
Notes
lru-cacheis not inpackage.json; the implementation uses a plainMapwhich satisfies the same requirements with zero new dependencies.