Skip to main content

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!