Skip to content

Commit dca2bd7

Browse files
authored
feat(node-sdk): Introduce flag fallback providers (#558)
1 parent 2d7c7f0 commit dca2bd7

File tree

14 files changed

+2961
-194
lines changed

14 files changed

+2961
-194
lines changed

.changeset/cute-ties-sink.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"@reflag/node-sdk": minor
3+
---
4+
5+
Introduce flag fallback providers
6+
7+
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.

packages/node-sdk/README.md

Lines changed: 191 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,8 @@ await client.flush();
175175
### Rate Limiting
176176

177177
The SDK includes automatic rate limiting for flag events to prevent overwhelming the API.
178-
Rate limiting is applied per unique combination of flag key and context. The rate limiter window size is configurable:
179-
180-
```typescript
181-
const client = new ReflagClient({
182-
rateLimiterOptions: {
183-
windowSizeMs: 60000, // Rate limiting window size in milliseconds
184-
},
185-
});
186-
```
178+
Rate limiting is applied per unique combination of flag key and evaluation context.
179+
This behavior is built in and does not currently require configuration.
187180

188181
### Flag definitions
189182

@@ -206,9 +199,182 @@ const flagDefs = await client.getFlagDefinitions();
206199
// }]
207200
```
208201

202+
### Fallback provider
203+
204+
`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:
236+
237+
- `fallbackProviders.static(...)`
238+
- `fallbackProviders.file(...)`
239+
- `fallbackProviders.redis(...)`
240+
- `fallbackProviders.s3(...)`
241+
- `fallbackProviders.gcs(...)`
242+
243+
##### File provider
244+
245+
```typescript
246+
import { ReflagClient, fallbackProviders } from "@reflag/node-sdk";
247+
248+
const client = new ReflagClient({
249+
secretKey: process.env.REFLAG_SECRET_KEY,
250+
flagsFallbackProvider: fallbackProviders.file({
251+
directory: ".reflag",
252+
}),
253+
});
254+
255+
await client.initialize();
256+
```
257+
258+
The file provider stores one snapshot file per environment in the configured
259+
`directory`.
260+
261+
##### Static provider
262+
263+
If you just want a fixed fallback copy of simple enabled/disabled flags, you can provide a static map:
264+
265+
```typescript
266+
import { ReflagClient, fallbackProviders } from "@reflag/node-sdk";
267+
268+
const client = new ReflagClient({
269+
secretKey: process.env.REFLAG_SECRET_KEY,
270+
flagsFallbackProvider: fallbackProviders.static({
271+
flags: {
272+
huddle: true,
273+
"smart-summaries": false,
274+
},
275+
}),
276+
});
277+
278+
await client.initialize();
279+
```
280+
281+
##### Redis provider
282+
283+
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.
284+
285+
```typescript
286+
import { ReflagClient, fallbackProviders } from "@reflag/node-sdk";
287+
288+
const client = new ReflagClient({
289+
secretKey: process.env.REFLAG_SECRET_KEY,
290+
flagsFallbackProvider: fallbackProviders.redis(),
291+
});
292+
293+
await client.initialize();
294+
```
295+
296+
##### S3 provider
297+
298+
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.
299+
300+
```typescript
301+
import { ReflagClient, fallbackProviders } from "@reflag/node-sdk";
302+
303+
const client = new ReflagClient({
304+
secretKey: process.env.REFLAG_SECRET_KEY,
305+
flagsFallbackProvider: fallbackProviders.s3({
306+
bucket: process.env.REFLAG_SNAPSHOT_BUCKET!,
307+
}),
308+
});
309+
310+
await client.initialize();
311+
```
312+
313+
##### GCS provider
314+
315+
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.
316+
317+
```typescript
318+
import { ReflagClient, fallbackProviders } from "@reflag/node-sdk";
319+
320+
const client = new ReflagClient({
321+
secretKey: process.env.REFLAG_SECRET_KEY,
322+
flagsFallbackProvider: fallbackProviders.gcs({
323+
bucket: process.env.REFLAG_SNAPSHOT_BUCKET!,
324+
}),
325+
});
326+
327+
await client.initialize();
328+
```
329+
330+
#### Testing fallback startup locally
331+
332+
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:
337+
338+
```typescript
339+
import type {
340+
FlagsFallbackProvider,
341+
FlagsFallbackSnapshot,
342+
} from "@reflag/node-sdk";
343+
344+
const fallbackSnapshot: FlagsFallbackSnapshot = {
345+
version: 1,
346+
savedAt: "2026-03-10T00:00:00.000Z",
347+
flags: [
348+
{
349+
key: "huddle",
350+
description: "Fallback example",
351+
targeting: {
352+
version: 1,
353+
rules: [],
354+
},
355+
},
356+
],
357+
};
358+
359+
export const staticFallbackProvider: FlagsFallbackProvider = {
360+
async load() {
361+
return fallbackSnapshot;
362+
},
363+
364+
async save() {
365+
// no-op
366+
},
367+
};
368+
```
369+
370+
> [!NOTE]
371+
>
372+
> `fallbackFlags` is deprecated. Prefer `flagsFallbackProvider` for startup fallback and outage recovery.
373+
> `flagsFallbackProvider` is not used in offline mode.
374+
209375
## Bootstrapping client-side applications
210376

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.
212378

213379
```typescript
214380
const client = new ReflagClient();
@@ -340,7 +506,8 @@ fallback behavior:
340506
4. **Offline Mode**:
341507

342508
```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.
344511
const client = new ReflagClient({
345512
offline: true,
346513
flagOverrides: () => ({
@@ -400,16 +567,19 @@ a configuration file on disk or by passing options to the `ReflagClient`
400567
constructor. By default, the SDK searches for `reflag.config.json` in the
401568
current working directory.
402569

403-
| Option | Type | Description | Env Var |
404-
| --------------- | ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
405-
| `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.
570+
| Option | Type | Description | Env Var |
571+
| ----------------------- | ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
572+
| `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.
413583
414584
`reflag.config.json` example:
415585

packages/node-sdk/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
"vitest": "~1.6.0"
4646
},
4747
"dependencies": {
48+
"@aws-sdk/client-s3": "^3.888.0",
49+
"@google-cloud/storage": "^7.19.0",
50+
"@redis/client": "^5.11.0",
4851
"@reflag/flag-evaluation": "1.0.0"
4952
}
5053
}

0 commit comments

Comments
 (0)