javascript-sdk icon indicating copy to clipboard operation
javascript-sdk copied to clipboard

sending SDK to createInstance() in Server-side context causes memory leaks

Open dbridenbeck opened this issue 1 year ago • 5 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

SDK Version

4.9.2

Current Behavior

When passing the SDK key into createInstance we saw memory leaks happening. In the react-sdk it's advised not to pass the SDK key in, and I think that the javascript-sdk documentation should be updated to provide a similar warning for the section in node.js.

Current documentation: https://github.com/optimizely/javascript-sdk?tab=readme-ov-file#use-the-javascript-sdk-node image

Expected Behavior

Expected documentation is to be a-la react-sdk: https://github.com/optimizely/react-sdk?tab=readme-ov-file#server-side-rendering

image

Steps To Reproduce

  1. In a server environment, pass SDK instead of datafile into createInstance(), deploy
  2. Use k6 or some other way to pass large traffic to the deployed environment
  3. Observe containers running out of memory and restarting

SDK Type

Browser

Node Version

16.10.0

Browsers impacted

n/a

Link

No response

Logs

No response

Severity

Minor issue

Workaround/Solution

Pass dataFile instead of SDK into createInstance()

Recent Change

Not certain.

Conflicts

Not likely, although we are on next 12.

dbridenbeck avatar Jan 17 '24 19:01 dbridenbeck

@dbridenbeck , yes, for server side rendering, Javascript sdk should have similar restrictions as react sdk. We will look into this a bit further and update this issue accordingly.

raju-opti avatar Jan 19 '24 18:01 raju-opti

We released version v5.3.0. Can you review this version in an SSR test environment?

mikechu-optimizely avatar Apr 15 '24 16:04 mikechu-optimizely

I can confirm that this still happens on v5.3.4.

cupofjoakim avatar Dec 01 '24 11:12 cupofjoakim

Still happening.

SonnyFishback avatar May 23 '25 01:05 SonnyFishback

I was able to reduce the memory utilization significantly by implementing a wrapper around Optimizely SDK to ensure that only one instance was created on the start of my node server and remains a singleton until the server is shutdown.

SonnyFishback avatar May 23 '25 03:05 SonnyFishback

We have added explanation in our readme and official docs that each sdk instance needs to be closed using the .close() method, otherwise memory leaks can happen. Also, we have released v6 with a new disposable option to createInstance, which will come in handy in SSR scenarios.

https://github.com/optimizely/javascript-sdk?tab=readme-ov-file#closing-the-sdk-instance

closing this issue.

raju-opti avatar Jun 05 '25 15:06 raju-opti