Overview
Statsig’s Fastly integration pushes Statsig Configs to Fastly KV, providing low latency for gate and experiment evaluations in your Fastly project.Configure Integration
First, enable the Fastly integration in the Statsig Console. Navigate to Project Settings -> Integrations, and then select Fastly You will need to input the following:- Fastly API Key - Can be found in Fastly portal under Account -> API Tokens. We need an “Automation token” with “Engineer Roles” and the following scopes: “global” and “global:read”
- Store Type - Select “Config Store” or “KV Store”
- either Config Store ID OR KV Store ID- You can create a new Config Store or KV Store for this integration, or reuse an existing one
statsig-. The ending part of that key is your project ID. You’ll need this later.
Add the Statsig SDK to your Worker
Now lets hook up the SDK to read that config payload and use it for gate and experiment checks in your fastly compute. The remainder of this document assumes you are using nodejs. If you are using another language, see the sdk language specific documentation for how to apply the implementation to that language. You may need to implement a custom data adapter for your language - we currently only offer thestatsig-node-fastly-kv package in node.
First up, you’ll need to install the statsig sdk and the fastly kv data adapter.
- Creating a new
FastlyDataAdapterwith the namespace you set up previously and your statsig project id - Initializing the statsig sdk with the DataAdapter
- Checking a Gate
- Flushing events to statsig
1. The FastlyDataAdapter
- The KV store name you set up previously. This must be accessible and linked to the compute service
- Your statsig project id. This is the string that comes after
statsig-in the KV key your config is stored under. You can also find it in any url when you are on the Statsig Console. For example,https://console.statsig.com/7LhuarZImmfNdtl9IsDJeX/gates/test_fastly/diagnosticshas a project ID of7LhuarZImmfNdtl9IsDJeX
2. SDK Initialization
- Your statsig secret key. This is available from the Project Settings page in the Statsig Console. This is used to authenticate your requests to the statsig backend. In this example, we’ve configured it as an environment variable
- An options object. We are using the
dataAdapterproperty to hook up the Fastly KV store to the SDK. We’re also disabling the ID list sync to speed up initialization
3. Checking a Gate
StatsigUser object you are checking, and the second is the gate name. Refer to the node sdk documentation for how to check other entities like experiments and dynamic configs. Here, we have created a user with a random userID for every evaluation to illustrate a gate with a partial rollout working.
4. Flushing Events
Putting it all together
Other Considerations
Polling for updates v5.13.0+
The SDK cannot poll for updates across requests since Fastly does not allow for timers. To solve for this, a manual sync API is available for independently updating the SDK internal store. For example, you could persist the last time you synced, and define an interval with which you are okay using a potentially stale config. There is a tradeoff here between the frequency with which your integration will make an external request to update the config, and the likelihood that your evaluation results are up to date.Flushing events v4.16.0+
The SDK enqueues logged events and flushes them in batches. In order to ensure events are properly flushed, we recommend calling flush using event.waitUntil. This will keep the request handler alive until events are flushed without blocking the response.Size Limits
Fastly Config Store has maximum size limits that may prevent Statsig from pushing configs into Fastly. See here for the latest Config Store limits. If your payload continues to grow, you will need to set the option to filter the payload by a [/sdk-keys/target-apps](Target App) in the integration settings.Unsupported Features
Statsig ID Lists are not currently synced into Fastly KVs or Config Stores. If you rely on large (>1000) ID lists, you will not be able to check them in your Fastly compute services. This is why we setinitStrategyForIDLists: 'none' in the SDK initialization.