Repository navigation
fetch exposures from counter in db instead of scanning and counting - #3420
Open
danoswaltCL wants to merge 1 commit into
Open
danoswaltCL wants to merge 1 commit into
danoswaltCL wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Exposure counters can permanently undercount because synchronized databases skip the backfill and legacy writers can add exposures during or after backfilling without updating the counter.
2 open issues
What changed in this PR
Replaces exposure-table scans in paginated feature-flag reads with maintained per-flag counters to reduce list latency.
Changes:
- Adds a counter entity and migration that backfills existing exposure totals.
- Atomically increments counters for newly inserted exposures and uses them in paginated responses.
- Updates unit tests and documents counter consistency and repair procedures.
| File | Description |
|---|---|
packages/backend/test/unit/services/FeatureFlagService.test.ts |
Tests count attachment and zero defaults. |
packages/backend/test/unit/repositories/FeatureFlagExposureRepository.test.ts |
Tests SQL construction, counter lookup, and empty inputs. |
packages/backend/src/database/migrations/1794418860625-featureFlagExposureCount.ts |
Creates and backfills the counter table with cascade deletion. |
packages/backend/src/api/services/FeatureFlagService.ts |
Reads maintained counts for paginated flags. |
packages/backend/src/api/repositories/FeatureFlagExposureRepository.ts |
Adds atomic exposure/count writes and counter lookups. |
packages/backend/src/api/models/FeatureFlagExposureCount.ts |
Defines the separate per-flag counter entity. |
packages/backend/src/api/models/FeatureFlag.ts |
Converts exposureCount to a plain response property. |
packages/backend/CLAUDE.md |
Documents counter maintenance, invariants, and reconciliation. |
Review effort: Balanced
| return { | ||
| ...featureFlag, | ||
| // A flag with no exposures yet has no counter row | ||
| exposureCount: exposureCounts.get(featureFlag.id) ?? 0, |
Comment on lines
+25
to
+28
| INSERT INTO "feature_flag_exposure_count" ("featureFlagId", "count") | ||
| SELECT "featureFlagId", COUNT(*)::int | ||
| FROM "feature_flag_exposure" | ||
| GROUP BY "featureFlagId" |
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.

The migration backfills existing counts.
Also to note this should have minimal impact even though it moves a db operation to a frequent call; only first-time callers will be registering an exposure and thus incrementing the counter, which can't be much added work, but we should keep it in my for performance tests.