You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for `flagsFallbackProvider`, a reliability feature that lets the Node SDK persist the latest successfully fetched flag definitions to fallback storage such as a local file, S3, Redis, or a custom backend.
8
+
9
+
Reflag servers remain the primary source of truth. On startup, the SDK still tries to fetch a live snapshot first. If that initial fetch fails, it can load the last saved snapshot from the fallback provider so new processes can still initialize in the exceedingly rare case that Reflag has an outage.
10
+
11
+
After successfully fetching updated flag definitions, the SDK saves the latest definitions back through the provider to keep the fallback snapshot up to date.
12
+
13
+
This improves service startup reliability and outage recovery without changing normal flag evaluation behavior.
`flagsFallbackProvider` is a reliability feature that lets the SDK persist the latest successfully fetched raw flag definitions to fallback storage such as a local file, Redis, S3, GCS, or a custom backend.
205
+
206
+
#### How it works
207
+
208
+
Reflag servers remain the primary source of truth. On `initialize()`, the SDK always tries to fetch a live copy of the flag definitions first, and it continues refreshing those definitions from the Reflag servers over time.
209
+
210
+
If that initial live fetch fails, the SDK can call `flagsFallbackProvider.load()` and start with the last saved snapshot instead. This is mainly useful for cold starts in the exceedingly rare case that Reflag has an outage.
211
+
212
+
If Reflag becomes unavailable after the SDK has already initialized successfully, the SDK keeps using the last successfully fetched definitions it already has in memory. In other words, the fallback provider is mainly what helps future processes start, not what keeps an already running process alive.
213
+
214
+
After successfully fetching updated flag definitions, the SDK calls `flagsFallbackProvider.save()` to keep the stored snapshot up to date.
215
+
216
+
Typical reliability flow:
217
+
218
+
1. The SDK starts and tries to fetch live flag definitions from Reflag.
219
+
2. If that succeeds, those definitions are used immediately and the SDK continues operating normally.
220
+
3. After successfully fetching updated flag definitions, the SDK saves the latest snapshot through the fallback provider so a recent copy is available if needed later.
221
+
4. If a future process starts while Reflag is unavailable, it can load the last saved snapshot from the fallback provider and still initialize.
222
+
5. Once Reflag becomes available again, the SDK resumes using live data and refreshes the fallback snapshot.
223
+
224
+
Most deployments run multiple SDK processes, so more than one process may save identical flag definitions to the fallback storage at roughly the same time. This is expected and generally harmless for backends like a local file, Redis, S3, or GCS because the operation is cheap. In practice, this only becomes worth thinking about once you have many thousands of SDK processes writing to the same fallback storage.
225
+
226
+
> [!TIP]
227
+
> If you are building a web or client-side application and want the most resilient setup, combine `flagsFallbackProvider` on the server with bootstrapped flags on the client.
228
+
>
229
+
> `flagsFallbackProvider` helps new server processes start if they cannot reach Reflag during initialization. Bootstrapping helps clients render from server-provided flags instead of depending on an initial client-side fetch from the Reflag servers.
230
+
>
231
+
> This applies to React (`getFlagsForBootstrap()` + `ReflagBootstrappedProvider`), the Browser SDK (`bootstrappedFlags`), and the Vue SDK (bootstrapped flags via the provider).
232
+
233
+
#### Built-in providers
234
+
235
+
You can access the built-in providers through the `fallbackProviders` namespace:
The built-in Redis provider creates a Redis client automatically when omitted and uses `REDIS_URL` from the environment. It stores snapshots under the configured `keyPrefix` and uses the first 16 characters of the secret key hash in the Redis key.
The built-in S3 provider works out of the box using the AWS SDK's default credential chain and region resolution. It stores the snapshot object under the configured `keyPrefix` and uses a hash of the secret key in the object name.
The built-in GCS provider works out of the box using Google Cloud's default application credentials. It stores the snapshot object under the configured `keyPrefix` and uses a hash of the secret key in the object name.
To test fallback startup in your own app, first run it once with a working Reflag connection so a snapshot is saved. Then restart it with the same secret key and fallback provider configuration, but set `apiBaseUrl` to `http://127.0.0.1:65535`. That forces the live fetch to fail and lets you verify that the SDK initializes from the saved snapshot instead.
333
+
334
+
#### Writing a custom provider
335
+
336
+
If you just want a fixed fallback copy of the flag definitions, a custom provider can be very small:
> `fallbackFlags` is deprecated. Prefer `flagsFallbackProvider` for startup fallback and outage recovery.
373
+
> `flagsFallbackProvider` is not used in offline mode.
374
+
209
375
## Bootstrapping client-side applications
210
376
211
-
The `getFlagsForBootstrap()` method is designed for server-side rendering (SSR) scenarios where you need to pass flag data to client-side applications. This method returns raw flag data without wrapper functions, making it suitable for serialization and client-side hydration.
377
+
The `getFlagsForBootstrap()` method is useful whenever you need to pass flag data to another runtime or serialize it without wrapper functions. Server-side rendering (SSR) is a common example, but it is also useful for other bootstrapping and hydration flows.
212
378
213
379
```typescript
214
380
const client =newReflagClient();
@@ -340,7 +506,8 @@ fallback behavior:
340
506
4.**Offline Mode**:
341
507
342
508
```typescript
343
-
// In offline mode, the SDK uses flag overrides
509
+
// In offline mode, the SDK uses explicit local configuration only.
510
+
// It does not fetch from Reflag or use flagsFallbackProvider.
344
511
const client =newReflagClient({
345
512
offline: true,
346
513
flagOverrides: () => ({
@@ -400,16 +567,19 @@ a configuration file on disk or by passing options to the `ReflagClient`
400
567
constructor. By default, the SDK searches for `reflag.config.json` in the
|`secretKey`| string | The secret key used for authentication with Reflag's servers. | REFLAG_SECRET_KEY |
406
-
|`logLevel`| string | The log level for the SDK (e.g., `"DEBUG"`, `"INFO"`, `"WARN"`, `"ERROR"`). Default: `INFO`| REFLAG_LOG_LEVEL |
407
-
|`offline`| boolean | Operate in offline mode. Default: `false`, except in tests it will default to `true` based off of the `TEST` env. var. | REFLAG_OFFLINE |
408
-
|`apiBaseUrl`| string | The base API URL for the Reflag servers. | REFLAG_API_BASE_URL |
409
-
|`flagOverrides`| Record<string, boolean> | An object specifying flag overrides for testing or local development. See [examples/express/app.test.ts](https://github.com/reflagcom/javascript/tree/main/packages/node-sdk/examples/express/app.test.ts) for how to use `flagOverrides` in tests. | REFLAG_FLAGS_ENABLED, REFLAG_FLAGS_DISABLED |
410
-
|`configFile`| string | Load this config file from disk. Default: `reflag.config.json`| REFLAG_CONFIG_FILE |
411
-
412
-
> [!NOTE] > `REFLAG_FLAGS_ENABLED` and `REFLAG_FLAGS_DISABLED` are comma separated lists of flags which will be enabled or disabled respectively.
|`secretKey`| string | The secret key used for authentication with Reflag's servers. | REFLAG_SECRET_KEY |
573
+
|`logLevel`| string | The log level for the SDK (e.g., `"DEBUG"`, `"INFO"`, `"WARN"`, `"ERROR"`). Default: `INFO`| REFLAG_LOG_LEVEL |
574
+
|`offline`| boolean | Operate in offline mode. Default: `false`, except in tests it will default to `true` based off of the `TEST` env. var. In offline mode the SDK does not fetch from Reflag and does not use `flagsFallbackProvider`. | REFLAG_OFFLINE |
575
+
|`apiBaseUrl`| string | The base API URL for the Reflag servers. | REFLAG_API_BASE_URL |
576
+
|`flagOverrides`| Record<string, boolean> | An object specifying flag overrides for testing or local development. See [examples/express/app.test.ts](https://github.com/reflagcom/javascript/tree/main/packages/node-sdk/examples/express/app.test.ts) for how to use `flagOverrides` in tests. | REFLAG_FLAGS_ENABLED, REFLAG_FLAGS_DISABLED |
577
+
|`flagsFallbackProvider`|`FlagsFallbackProvider`| Optional provider used to load and save raw flag definitions for fallback startup when the initial live fetch fails. Available only through the constructor. Ignored in offline mode. | - |
578
+
|`configFile`| string | Load this config file from disk. Default: `reflag.config.json`| REFLAG_CONFIG_FILE |
579
+
580
+
> [!NOTE]
581
+
>
582
+
> `REFLAG_FLAGS_ENABLED` and `REFLAG_FLAGS_DISABLED` are comma separated lists of flags which will be enabled or disabled respectively.
0 commit comments