For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sign inTry it free
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
  • SDKs
    • SDK concepts
    • SDK features
      • Aliasing users
      • Anonymous contexts and users
      • Automatic environment attributes
      • Big segments
      • Bootstrapping
      • Caching contexts to local storage
      • Configuration
      • Context configuration
      • Customizing AgentControl
      • Data saving mode
      • Evaluating flags
      • Flag evaluation reasons
      • Flushing events
      • Getting all flags
      • Hooks
      • Identifying and changing contexts
      • Inspectors
      • Logging configuration
      • Migrations
      • Monitoring SDK status
      • Multiple environments
      • Offline mode
      • OpenTelemetry in server-side SDKs
      • Private attributes
      • User feedback
      • Reading flags from a file
      • Relay Proxy configuration
      • Secure mode
      • Sending custom events
      • Shutting down
      • Storing data
      • Subscribing to flag changes
      • Test data sources
      • Tracking AI metrics
      • Web proxy configuration
    • Client-side SDKs
    • Server-side SDKs
    • AI SDKs
    • Edge SDKs
    • OpenFeature providers
    • Observability SDKs
    • Relay Proxy
Sign inTry it free
LogoLogo
On this page
  • About context caching
  • Client-side SDKs
  • Electron
  • JavaScript
  • Node.js (client-side)
  • React Native
  • iOS
  • Android
  • Flutter
  • React Web
SDKsSDK features

Caching contexts to local storage

Was this page helpful?
Previous

Configuration

Next
Built with

This topic explains how context caching to local storage works for SDKs that support the feature.

About context caching

Most newer client SDKs use local storage to cache flag values for unique contexts by default. On subsequent launches, the SDK reads the cached values before any network connection is established. The client still initializes and connects to LaunchDarkly’s service to fetch the most recent flag values, but it is not dependent on those values to reach its ready state. The SDK updates values in the background after connecting to LaunchDarkly.

Some SDKs let you configure the maximum number of unique contexts that can be cached locally by setting maxCachedContexts at startup. This allows you to control the amount of local storage used for storing contexts.

When you call identify, the number of cached contexts increments. If the number of cached contexts exceeds maxCachedContexts (5 contexts by default), the SDK deletes context data in excess of maxCachedContexts, starting with the oldest context first.

For SDKs that support configuration, set maxCachedContexts to 0 to disable caching flag values for contexts in local storage and clear any stored data. Setting disableCache to true disables caching context data to local storage but leaves any existing local storage in place.

Client-side SDKs

These SDKs enable local storage caching by default, and you can optionally configure the size of the cache as necessary:

  • JavaScript v4.x
  • iOS
  • Android
  • Flutter
  • React Native
  • React Web

Some SDKs use the same implementation for both bootstrapping and local storage caching. For these SDKs you cannot configure both bootstrapped flag values and local storage caching. The following SDKs provide the option to use either bootstrap flag values or enable local storage caching, but not both:

  • JavaScript v3.x
  • Electron
  • Node.js (client-side)

For the above SDKs, local storage is not enabled by default. You can optionally enable local storage caching, but you cannot configure the cache size.

Electron

Expand Electron code sample

If you set bootstrap to the string "localStorage", the client tries to get flag values from persistent storage, using a unique key that is based on the user properties.

In Electron, persistent storage consists of files in the userData directory. If the client finds flag values stored for this user, it uses them and starts up immediately in a ready state, but also makes a background request to LaunchDarkly to get the latest values and stores them as soon as it receives them.

You can choose to enable caching to local storage, but you cannot configure the cache size.

Here’s how to use this mode:

1const client = LaunchDarkly.initializeInMain(
2 'example-client-side-id',
3 user,
4 {
5 bootstrap: 'localStorage'
6 }
7);

If you enable local storage you cannot also provide bootstrap flag values. To learn more, read Bootstrapping.

JavaScript

Expand JavaScript code sample

The JavaScript client SDK v4.x stores flag values for unique contexts in local storage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts at startup.

To learn more, read Interface LDOptions.

JavaScript SDK v3.x requires you to enable caching contexts to local storage at initialization using the bootstrap option, and does not provide the ability to configure the cache size.

Here is an example of how to configure caching to local storage with JavaScript:

1import { createClient } from '@launchdarkly/js-client-sdk';
2
3const context = { kind: 'user', key: 'example-user-key'};
4
5// Local storage is enabled by default
6// You can optionally configure the maximum number of cached contexts (default is 5)
7const options = { maxCachedContexts: 3 };
8
9const client = createClient(
10 'example-client-side-id',
11 context,
12 options
13);
14
15client.start();

If you enable local storage with JavaScript SDK v3.x, you cannot also provide bootstrap flag values. To learn more, read Bootstrapping.

Node.js (client-side)

Expand Node.js (client-side) code sample

The Node.js (client-side) SDK requires you to configure local storage caching using the bootstrap option. In this mode, if no values have been cached yet, the SDK obtains flags from LaunchDarkly and then caches them in persistent storage. The next time you start, the cached flags are immediately available, and the SDK also contacts LaunchDarkly in the background to obtain updated values.

You can choose to enable caching to local storage, but you cannot configure the cache size.

To activate this mode, use the special string "localStorage":

1const client = LDClient.initialize(
2 'example-client-side-id',
3 context,
4 {
5 bootstrap: 'localStorage'
6 }
7);

If you enable local storage you cannot also provide bootstrap flag values. To learn more, read Bootstrapping.

React Native

Expand React Native code sample

The React Native SDK stores flag values for unique contexts in AsyncStorage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts at startup.

To learn more, read Interface LDOptions.

Here is an example of how to configure caching to local storage with React Native:

TypeScript
1import { ReactNativeLDClient, AutoEnvAttributes } from '@launchdarkly/react-native-client-sdk';
2
3// Local storage is enabled by default using AsyncStorage
4// You can optionally configure the maximum number of cached contexts (default is 5)
5const options = { maxCachedContexts: 3 };
6
7const client = new ReactNativeLDClient(
8 'example-mobile-key',
9 AutoEnvAttributes.Enabled,
10 options
11);
12
13await client.start();

iOS

Expand iOS code sample

The iOS SDK stores flag values for unique contexts in local storage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts on your LDConfig before starting the client.

To learn more, read Struct LDConfig.

Here is an example of how to configure caching to local storage with iOS:

Swift
1var config = LDConfig(mobileKey: "example-mobile-key", autoEnvAttributes: .enabled)
2
3// Local storage is enabled by default
4// You can optionally configure the maximum number of cached contexts (default is 5)
5config.maxCachedContexts = 3
6
7let contextBuilder = LDContextBuilder(key: "example-context-key")
8guard case .success(let context) = contextBuilder.build() else { return }
9
10LDClient.start(config: config, context: context)

Android

Expand Android code sample

The Android SDK stores flag values for unique contexts in SharedPreferences by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts in your LDConfig before starting the client.

To learn more, read Class LDConfig.Builder.

Here is an example of how to configure caching to local storage with Android:

1LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
2 .mobileKey("example-mobile-key")
3 // Local storage is enabled by default
4 // You can optionally configure the maximum number of cached contexts (default is 5)
5 .maxCachedContexts(3)
6 .build();
7
8LDContext context = LDContext.create("example-context-key");
9
10LDClient client = LDClient.init(this.getApplication(), ldConfig, context, 0);

Flutter

Expand Flutter code sample

The Flutter SDK stores flag values for unique contexts in on-device storage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts in a PersistenceConfig passed to your LDConfig.

To learn more, read Class PersistenceConfig.

Here is an example of how to configure caching to local storage with Flutter:

Dart
1// Local storage is enabled by default
2// You can optionally configure the maximum number of cached contexts (default is 5)
3final config = LDConfig(
4 'example-mobile-key',
5 AutoEnvAttributes.enabled,
6 persistence: PersistenceConfig(maxCachedContexts: 3),
7);
8
9final context = LDContextBuilder()
10 .kind('user', 'example-context-key')
11 .build();
12
13final client = LDClient(config, context);
14await client.start();

React Web

Expand React Web code sample

The React Web SDK stores flag values for unique contexts in local storage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts inside ldOptions when you create your provider.

To learn more, read Interface LDReactClientOptions.

Here is an example of how to configure caching to local storage with React Web:

TypeScript
1import { createLDReactProvider } from '@launchdarkly/react-sdk';
2
3const context = { kind: 'user', key: 'example-user-key' };
4
5// Local storage is enabled by default
6// You can optionally configure the maximum number of cached contexts (default is 5)
7const LDProvider = createLDReactProvider(
8 'example-client-side-id',
9 context,
10 {
11 ldOptions: {
12 maxCachedContexts: 3,
13 },
14 }
15);