Skip to content

fix: N dynamodb queries in feature list endpoint#6758

Open
matthewelwell wants to merge 13 commits intomainfrom
fix/features-list-dynamodb-query-count
Open

fix: N dynamodb queries in feature list endpoint#6758
matthewelwell wants to merge 13 commits intomainfrom
fix/features-list-dynamodb-query-count

Conversation

@matthewelwell
Copy link
Contributor

@matthewelwell matthewelwell commented Feb 23, 2026

Changes

This PR refactors the logic for retrieving the number of identity overrides when retrieving the list of features within the context of an environment such that, instead of making a query to dynamodb for every feature in the page of features that are returned, it gets all of the overrides for the environment upfront and uses that information to populate the data.

The reason for doing this is to improve performance of the features list endpoint which is currently making multiple 'parallel' (depending on thread resources) calls to dynamodb when in 99% of cases, it only needs to make a single call.

How did you test this code?

Added a new test to verify the N queries issue is resolved

I actually wanted to also add another test to verify the logic using moto for dynamodb, but the issue is that (as far as I can tell), the test database, and moto do not support multithreading, and since the requests are executed via the ThreadPoolExecutor, we end up with bad results.

@vercel
Copy link

vercel bot commented Feb 23, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs Ignored Ignored Preview Feb 26, 2026 8:14pm
flagsmith-frontend-preview Ignored Ignored Preview Feb 26, 2026 8:14pm
flagsmith-frontend-staging Ignored Ignored Preview Feb 26, 2026 8:14pm

Request Review

@github-actions github-actions bot added api Issue related to the REST API fix labels Feb 23, 2026
@github-actions github-actions bot added fix and removed fix labels Feb 24, 2026
@github-actions github-actions bot added fix and removed fix labels Feb 24, 2026
@matthewelwell matthewelwell changed the title fix: N+1 dynamodb queries in feature list endpoint fix: N dynamodb queries in feature list endpoint Feb 24, 2026
@matthewelwell matthewelwell marked this pull request as ready for review February 24, 2026 22:43
@matthewelwell matthewelwell requested a review from a team as a code owner February 24, 2026 22:43
@matthewelwell matthewelwell requested review from gagantrivedi and removed request for a team February 24, 2026 22:43
@github-actions github-actions bot added fix and removed fix labels Feb 24, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 24, 2026

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-e2e:pr-6758 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api-test:pr-6758 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-6758 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api:pr-6758 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-6758 Finished ✅ Results

@codecov
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.27%. Comparing base (b98c31a) to head (017c032).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6758   +/-   ##
=======================================
  Coverage   98.27%   98.27%           
=======================================
  Files        1328     1328           
  Lines       48984    48938   -46     
=======================================
- Hits        48138    48094   -44     
+ Misses        846      844    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

# Conflicts:
#	api/edge_api/identities/edge_identity_service.py
)
get_overrides_data_future = executor.submit(
get_edge_identity_overrides_for_feature_ids,
get_edge_identity_overrides,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were discussing just getting the document_key to get the counts. Is the network gain not worth in the end? It looks like with a dedicated method we would also save on Pydantic overhead
https://github.com/Flagsmith/flagsmith/pull/6758/changes#diff-9bbfb0e97f54f08b1dec94a5e2f7233a9b9f86b4729795d089b20f88713f488bR23-R26

But I can understand it's neglictable unless tons of overrides

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout, sorry, I forgot about that piece! Let me see what I can do...

@github-actions github-actions bot added fix and removed fix labels Feb 26, 2026
Copy link
Contributor

@Zaimwa9 Zaimwa9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly the e2e failing to fix but that's gonna be a good improvement overall



def get_feature_id_from_identity_override_document_key(document_key: str) -> int:
return int(document_key.split(".")[1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that's the origin of the e2e failing

Suggested change
return int(document_key.split(".")[1])
return int(document_key.split(":")[1])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - fixed in 5a341cd

assert second_feature_metadata_after[0]["field_value"] == "200"


def test_create_feature__required_metadata_on_other_project__returns_201(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird that this got included

@@ -34,7 +33,6 @@
@dataclass
class IdentityOverridesQueryResponse:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Just checked and it looks safe to be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - removed in ae2a2b0

@github-actions github-actions bot added fix and removed fix labels Feb 26, 2026
@github-actions github-actions bot added fix and removed fix labels Feb 26, 2026
@matthewelwell matthewelwell force-pushed the fix/features-list-dynamodb-query-count branch from 7ca4c59 to e7dd086 Compare February 26, 2026 20:13
@github-actions github-actions bot added fix and removed fix labels Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

List features endpoint performance is slow on projects with large amounts of features

2 participants