Storing data

Overview

This topic explains which external databases each server-side SDK can use to store flag data.

By default, all our SDKs work with an in-memory feature store. This feature store requires no additional configuration. However, data in in-memory feature stores are not persistent. This means when you restart your application, your SDK reloads the entire store’s contents.

Persistent feature stores solve this problem by persisting their data so that they can be used across application restarts.

Using external databases as persistent feature stores

By default, LaunchDarkly’s server-side SDKs connect to LaunchDarkly and receive feature flag data, store the flags in local memory, and update them when prompted to by LaunchDarkly. This collection of last known flag data is cached in the “feature store” or “data store.”

To learn more, read Persistent data stores.

Alternatively, you can configure an external database to act as a feature store. The SDKs in this topic can use one or more of these three caching options to hold their flag data:

Whichever database you use, there are two ways to use it:

In both configurations, you can control when the SDK checks the database, as opposed to in-memory caching, using the cache time-to-live (TTL) configuration option for your SDK. There is a tradeoff here that will be different for each customer. Most customers find that it’s generally unacceptable from a performance standpoint to read from the database on every flag evaluation. If you prefer faster evaluations and can accept some stale data, you can have the SDK check the in-memory caching more frequently. If you prefer fresher data and can accept slower evaluations, you can have the SDK check the database more frequently.

Using a persistent feature store while still connecting to LaunchDarkly

In this configuration, the SDK receives feature flag data from LaunchDarkly and puts it in the feature store. The only difference is that the store is in a database.

When flags are evaluated, the SDK checks the database to get the latest flag state, usually with some form of in-memory caching. If you have a persistent feature store that has already been populated, the SDK can still evaluate flags using the last known flag state from the store until newer data is available from LaunchDarkly.

To set up this configuration, most people create some kind of object for the specific type of database and put it in the client configuration’s feature store property. In PHP, this property is called the “feature requester.”

If there are multiple instances of your application configured in this way with the same database, the same data gets written to the database multiple times, because each instance receives feature flags from LaunchDarkly. This is harmless, but it is inefficient, so you may want to use a persistent feature store without connecting to LaunchDarkly, as described below.

Using a persistent feature store without connecting to LaunchDarkly

This is similar to the previous configuration: When flags are evaluated, the SDK checks the database to get the latest flag state, usually with some form of in-memory caching. However, in this configuration the SDK does not connect to LaunchDarkly at all. Instead, it relies on some other process which does have a LaunchDarkly connection to write the latest flag data to the database, where the SDK will then read it.

The other process could be the Relay Proxy in offline or daemon mode, or any other application that creates an SDK client with the same persistent store. To learn more about the Relay Proxy, read The Relay Proxy.

The Relay Proxy is also known as the LaunchDarkly Daemon, so some versions of the SDKs refer to this mode as “LDD mode.” Creating the client is the same as above in terms of specifying the persistent store, but you must also add an option to make the SDK not connect to LaunchDarkly.

Server-side SDKs

This feature is available in the following server-side SDKs:

.NET (server-side)

The .NET SDK can use Consul, DynamoDB, or Redis to hold flag data.

To use a persistent feature store while connecting to LaunchDarkly:

C#
1using LaunchDarkly.Sdk.Server;
2using LaunchDarkly.Sdk.Server.Integrations;
3
4var config = Configuration.Builder(sdkKey)
5 .DataStore(
6 Components.PersistentDataStore(
7 SomeDatabaseName.DataStore()
8 )
9 )
10 .Build();

To learn more, read ConfigurationBuilder.DataStore.

To use a persistent feature store without connecting to LaunchDarkly, use the DataStore builder method as above, and then use Components.ExternalUpdatesOnly to configure daemon mode. To learn more, read Using daemon mode.

C++ (server-side)

The C++ SDK can use Redis to hold flag data.

In the C++ SDK v3.0, you cannot use a persistent feature store while connecting to LaunchDarkly.

To use a persistent feature store without having the SDK connect to LaunchDarkly, use the LazyLoadBuilder. The SDK reads flag data from the persistent store lazily, in the background.

Here’s how:

C++ SDK v3.0 (native)
1using LazyLoad = server_side::config::builders::LazyLoadBuilder;
2
3auto config_builder = server_side::ConfigBuilder(sdk_key);
4
5auto some_source = YourDatabaseIntegration();
6
7config_builder.DataSystem().Method(
8 LazyLoad().Source(some_source)
9);
10
11auto config = config_builder.Build();
12if (!config) {
13 /* an error occurred, config is not valid */
14}

To learn more, read Using daemon mode and LazyLoadBuilder.

Erlang

The Erlang SDK can use Redis to hold flag data.

To use a persistent feature store while connecting to LaunchDarkly:

Erlang
1LdOptions = #{
2 feature_store => your_feature_store
3},
4ldclient:start_instance("sdk-key-123abc", LdOptions).

To use a persistent feature store without connecting to LaunchDarkly, set the feature_store property as above, and then set the use_ldd option to configure daemon mode. To learn more, read Using daemon mode.

Go

The Go SDK can use Consul, DynamoDB, or Redis to hold flag data.

To use a persistent feature store while connecting to LaunchDarkly:

1import (
2 "time"
3
4 ld "github.com/launchdarkly/go-server-sdk/v6"
5 "github.com/launchdarkly/go-server-sdk/v6/ldcomponents"
6 examplepackage "github.com/launchdarkly/go-server-sdk-some-example-database"
7)
8
9var config ld.Config
10config.DataStore = ldcomponents.PersistentDataStore(
11 examplepackage.DataStore().SomeStoreOptions(),
12)
13client, _ := ld.MakeCustomClient(sdkKey, config, 5*time.Second)

To learn more, read PersistentDataStore.

To use a persistent feature store without connecting to LaunchDarkly, use PersistentDataStore() as above, and then use ExternalUpdatesOnly() to configure daemon mode. To learn more, read Using daemon mode.

Haskell

The Haskell SDK can use Redis to hold flag data.

To use a persistent feature store while connecting to LaunchDarkly:

Haskell
1import LaunchDarkly.Server
2
3main = do
4 backend <- makeYourBackendInterface
5
6 let config = configSetStoreBackend backend $ makeConfig "sdk-key-123abc"
7
8 client <- makeClient config

To use a persistent feature store without connecting to LaunchDarkly, use configSetStoreBackend as above, and then use configSetUseLdd to configure daemon mode. To learn more, read Using daemon mode.

Java

The Java SDK can use Consul, DynamoDB, or Redis to hold flag data.

To use a persistent feature store while connecting to LaunchDarkly:

Java
1import com.launchdarkly.sdk.server.*;
2import com.launchdarkly.sdk.server.integrations.*;
3
4LDConfig config = new LDConfig.Builder()
5 .dataStore(
6 Components.persistentDataStore(
7 SomeDatabaseName.dataStore(storeOptions)
8 )
9 )
10 .build();
11LDClient client = new LDClient(sdkKey, config);

To learn more, read Components.persistentDataStore.

To use a persistent feature store without connecting to LaunchDarkly, use persistentDataStore as above, and then use externalUpdatesOnly to configure daemon mode. To learn more, read Using daemon mode.

Lua

The Lua SDK can use Redis to hold flag data. The module launchdarkly_server_sdk_redis caches the feature flag data.

In the Lua SDK v2, you cannot use a persistent feature store while connecting to LaunchDarkly.

To use a persistent feature store without having the SDK connect to LaunchDarkly, specify a lazyLoad data source. The SDK reads flag data from the persistent store lazily, in the background.

Here’s how:

Lua SDK v2
1local config = {
2 dataSystem = {
3 lazyLoad = {
4 source = makeYourSource()
5 }
6 }
7}

In the Lua SDK v1, you can use a feature store while connecting to LaunchDarkly.

To use a persistent feature store while connecting to LaunchDarkly:

Lua SDK v1.x
1local l = require("launchdarkly_server_sdk")
2
3local backend = makeYourBackendInterface()
4
5local c = l.clientInit({
6 key = "sdk-key-123abc",
7 featureStoreBackend = backend
8)}

To learn more, read Using daemon mode.

Node.js (server-side)

The Node.js SDK can use Consul, DynamoDB, or Redis to hold flag data.

To use a persistent feature store while connecting to LaunchDarkly:

1const ld = require('@launchdarkly/node-server-sdk');
2
3const store = SomeKindOfFeatureStore(storeOptions);
4const options = {
5 featureStore: store
6};
7const client = ld.init('sdk-key-123abc', options);

To use a persistent feature store without connecting to LaunchDarkly, set the featureStore property as above, and then set the useLdd property to configure daemon mode. To learn more, read Using daemon mode.

PHP

The PHP SDK can use Consul, DynamoDB, or Redis to get flag data.

Unlike other server-side SDKs, the PHP SDK cannot connect to LaunchDarkly while using a database as a backing store. It can only read from a database that has been populated by the Relay Proxy, or by another application that uses a server-side LaunchDarkly SDK. To learn more, read Using daemon mode.

Another difference from other SDKs is that the PHP SDK does not have an in-memory cache for database queries. This is because in PHP, the entire in-memory application state is normally discarded after each request. Therefore, code examples for PHP do not include a cache TTL parameter.

To read from a persistent feature store without connecting to LaunchDarkly:

PHP
1$client = new LaunchDarkly\LDClient("sdk-key-123abc", [
2 'feature_requester' => LaunchDarkly\Integrations\NameOfDatabase::featureRequester()
3]);

Prior to version 4.0, the database integrations were included in the main PHP SDK package. Starting in version 4.0, they are in separate packages.

Python

The Python SDK can use Consul, DynamoDB, or Redis to hold flag data.

To use a persistent feature store while connecting to LaunchDarkly:

Python
1import ldclient
2from ldclient.config import Config
3
4store = SomeKindOfFeatureStore(store_options)
5config = Config(feature_store=store)
6ldclient.set_config(config)

To use a persistent feature store without connecting to LaunchDarkly, set the feature_store property as above, and then set the use_ldd property to configure daemon mode. To learn more, read Using daemon mode.

Ruby

The Ruby SDK can use Consul, DynamoDB, or Redis to hold flag data.

These adapters are implemented in the LaunchDarkly::Integrations::Redis, LaunchDarkly::Integrations::DynamoDB, and LaunchDarkly::Integrations::Consul modules. To use them, call the new_feature_store method in the module, and put the returned object in the feature_store property of your client configuration.

To use a persistent feature store while connecting to LaunchDarkly:

Ruby
1require 'ldclient-rb'
2
3store = SomeKindOfFeatureStore.new(storeOptions)
4config = LaunchDarkly::Config.new(
5 feature_store: store
6)
7client = LaunchDarkly::Client.new(sdk_key, config)

To use a persistent feature store without connecting to LaunchDarkly, set the feature_store property as above, and then set the use_ldd property to configure daemon mode. To learn more, read Using daemon mode.

Built with