Skip to content
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

Investigate potential memory leaks in SSR scenarios #11242

Closed
phryneas opened this issue Sep 22, 2023 · 10 comments
Closed

Investigate potential memory leaks in SSR scenarios #11242

phryneas opened this issue Sep 22, 2023 · 10 comments

Comments

@phryneas
Copy link
Member

phryneas commented Sep 22, 2023

Over time, we had different reports of potential memory leaks in SSR scenarios.

I'm going to spike out for a while and try to create some benchmarks and test scenarios that allow us to detect such cases.

See this Milestone for a list of related Apollo Client PRs: https://github.com/apollographql/apollo-client/milestone/37

Related "external" PRs for visibility:

External discussions: react-native-community/discussions-and-proposals#741

@rflament
Copy link

Not sure if this is the right place to report this, but we're investigating a memory issue in our next.js server and one of the cause seems to be SSR + useLazyQuery. It looks like even if the options ssr: false is passed to useLazyQuery, the QueryInfo is added to the queries set of QueryManager in the function watchQuery, but since the query is never executed on the server side, it stays in the queries variable forever.

As we use one single instance of ApolloClient, the queries variable of the QueryManager grows until OOM.

@phryneas
Copy link
Member Author

@rflament Thank you for the report, I'll look into that!

As we use one single instance of ApolloClient, the queries variable of the QueryManager grows until OOM.

Independently of the outcome of that - please never do that! You will end up mixing private data of multiple users. When you're doing SSR, please create a new instance of Apollo Client for every request!

@rflament
Copy link

Thanks!

In our case the data fetched on the SSR side do not depend on the user, we don't pass the cookies to the apollo client, so the fact that the cache would be reused in all requests seemed a good thing.

@phryneas
Copy link
Member Author

I'd still recommend the single store per request, since (as you noticed), Apollo Client isn't really tested for potentially thousands of requests per second on one instance, but only for "normal application usage" scenarios.

@phryneas
Copy link
Member Author

@rflament I've moved this out into #11445. This is a bigger conceptual problem and might need quite a lot of reworking of internals, so I can't give you a timeframe for this at this point.

So far, my recommendation is really to create a new AC instance per request.

@phryneas
Copy link
Member Author

The beta is out, and we're now waiting for feedback to dial in the right default cache limits.

For everyone who was following along: please give it a try and report back - you can find all the details in this blog post: https://www.apollographql.com/blog/apollo-3-9-beta-feature-spotlight-the-memory-story

@rflament
Copy link

I confirm that creating a new client instance per request fixed the memory leak, thanks!

@phryneas
Copy link
Member Author

phryneas commented Feb 6, 2024

Going to close this with the release of 3.9.

@phryneas phryneas closed this as completed Feb 6, 2024
Copy link
Contributor

github-actions bot commented Feb 6, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Copy link
Contributor

github-actions bot commented Mar 9, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants