cloudoperators / heureka

Security and compliance management
Apache License 2.0
3 stars 1 forks source link

research(performance): dataloaders #258

Open drochow opened 1 week ago

drochow commented 1 week ago

Task Description

The GraphQL API is currently not optimized for performance. Getting nested entities from a list, especially, results in a lot of queries.

Example.:

query ($filter: IssueMatchFilter) {
    IssueMatches (filter: $filter) {
        edges { 
            node { 
                componentInstance { 
                    id
                    ccrn     
                    component version {
                        id
                    }
                }
            cursor
        }
    }
}

This query would lead to a separate SQL query in the backend for each IssueMatch in the list and each of the component instances. When we get a list of 10 issue matches, we get 1+10 SQL queries. Like.:

SELECT * FROM IssueMatch WHERE $filter;
SELECT * FROM ComponenInstance WHERE ID=1;
SELECT * FROM ComponenInstance WHERE ID=2;
....
SELECT * FROM ComponenInstance WHERE ID=10;

This can be a major performance bottleneck, especially for deeper nested structures and long lists.

There is the concept of Dataloaders https://github.com/graphql/dataloader that circumvents this bottleneck by providing a "loader" that waits for a short period and collects all IDS used to request a result and instead provides an aggregated query and returns the individual results to the resolvers.

The result is that the queries in the above example will be reduced to 2:

SELECT * FROM IssueMatch WHERE $filter;
SELECT * FROM ComponenInstance WHERE ID IN(1, 2,3,4,5,6,7,8,9,10);

⚠️ The queries above are only for illustration purpose.

Therefore we need to investigate how we can do the above using gqlgen and how high the effort of implementation is.

Acceptance Criteria:

Expected Test:

drochow commented 1 week ago

@MR2011 please review ticket! Do you agree with the task?

MR2011 commented 1 week ago

Yes, let's add it to the current sprint