Using Statsig in Serverless Environments
In this guide, we will walk you through how to leverage Statsig’s platform in serverless environments. The examples in this guide use the statsig-node
SDK in a Google Cloud Function.
These details shouldn't matter - but you may have to adjust your implementation based on your cloud provider of choice.
Usage
Statsig server SDKs can be used in serverless environments. If you are unable to initialize
Statsig outside of a serverless function, you will incur a server round trip to Statsig servers to initialize the SDK in every function.
After this point, all checks should be locally evaluated in the function.
Finally, you'll need to ensure any enqueued events (exposure and custom events) are flushed to Statsig prior to the function exiting. You'll need to call statsig.flush()
as demonstrated here using the Cloudflare flavor.
Example
The following example has some debugging information built in to show the cost of initializing the SDK in a cloud function. In our experience, the initialize
call takes less than a second, and subsequent function invocations should proceed without this startup cost.
Alternatives
If this doesn't work for you, a custom integration may help but would require additional setup.
For example, you could create a background function which fetches your definitions from Statsig servers periodically, and stores them in your database. Then, when initializing a Statsig server SDK, you can fetch the values from your database, rather than issuing a request to Statsig servers.
Reach out to us in Slack if you want to talk through how to make Statsig play nicely with your architecture and requirements!
Fastly Implementation
Depending on your Fastly account settings, you may have an extra layer of security enabled which requires a custom fetch
method to be implemented for any network requests that need to be made to 3rd party domains. You must implement a custom fetch
method that uses Fastly's required backend parameter.
To solve for this, first define a module in its own file that implements a global override of fetch
:
Next, in the main function entrypoint, simply import the fetch override module prior to importing the Statsig SDK: