Overview

This topic explains how to use the SDK Redis integration as a persistent feature store.

Many of our server-side SDKs support Redis. The available options are slightly different in each language, but you can always specify the following:

  • The Redis host address, which defaults to localhost:6379
  • A prefix string to add to all keys used by the store, to avoid collisions in case the database is also being used for some other purpose
  • The length of time that recently read or updated data should be cached in memory
Supported Redis configurations

The LaunchDarkly SDKs use third-party open-source libraries to connect to Redis. Not all of these have the same level of support for advanced Redis configurations. Specifically, most of the SDKs and the Relay Proxy do not support connecting to a Redis cluster or using Redis Sentinel for service discovery.

To learn more, read the documentation for the individual SDKs below, or their Redis integration add-on libraries for SDKs that do not have this integration built in.

How the SDKs store data in Redis

The Redis integrations for all LaunchDarkly server-side SDKs use the same conventions, so that SDK instances and Relay Proxy instances sharing a single Redis store can interoperate correctly.

The storage schema is as follows:

  • There is always a “prefix” string that provides a namespace for the overall data set. If you do not specify a prefix in your configuration, it is launchdarkly. If you are using a persistent store integration, it must be the client-side ID for the environment.
  • For each type of data that the SDK can store, there is a hash whose key is PREFIX:TYPE. PREFIX is the configured prefix string. TYPE denotes the type of data such as features and segments.
  • Within each hash, there is one field per data item. For instance, the hash PREFIX:features has one field per feature flag. The field name is the unique key of the item, such as the flag key for a feature flag, and the value is a serialized representation of that item, in a format that is determined by the SDK.
  • An additional key, PREFIX:$inited, is created with an arbitrary value when the SDK stores a full set of feature flag data. This allows a new SDK instance to check whether there is already a valid data set that was stored earlier.
  • The SDK may use additional keys starting with the PREFIX string, so you should not assume that the TYPE values mentioned above and $inited are the only possible keys. The SDK never adds, modifies, or removes any keys in Redis other than ones starting with the PREFIX, so it is safe to share a Redis instance that is also being used for other purposes.

Server-side SDKs

In the following examples, the Redis feature store is set to use a host address of my-redis:6379, a prefix string of "my-key-prefix", and a cache TTL of 15 or 30 seconds, depending on the SDK.

Using a persistent store for segments requires a specific key prefix

If you are using a persistent store integration, the value of the key prefix for the persistent store must be the client-side ID of your environment.

Your environment’s client-side ID is available in the Environments list for your project. To learn more about key types, read Keys.

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

.NET (server-side)

To use Redis with the .NET SDK you must install an additional package named LaunchDarkly.ServerSdk.Redis.

C#
1using LaunchDarkly.Sdk.Server;
2using LaunchDarkly.Sdk.Server.Integrations;
3var config = Configuration.Builder(sdkKey)
4 .DataStore(
5 Components.PersistentDataStore(
6 Redis.DataStore()
7 .HostAndPort("my-redis", 6379)
8 .Prefix("my-key-prefix")
9 ).CacheSeconds(30)
10 )
11 .Build();
12var client = new LDClient(config);

To learn more, read dotnet-server-sdk-redis.

C++ (server-side)

The C++ integration is part of the main SDK distribution, but is disabled by default in order to avoid bringing in unnecessary dependencies (redis++, a wrapper for hiredis).

To ensure the integration header and library are available for your build, set the CMake option LD_BUILD_REDIS_SUPPORT=ON.

1// Make sure to include the redis source's header.
2#include <launchdarkly/server_side/integrations/redis/redis_source.hpp>
3
4using namespace launchdarkly::server_side;
5
6using LazyLoad = config::builders::LazyLoadBuilder;
7
8ConfigBuilder config_builder(sdk_key);
9
10auto redis_source = integrations::RedisDataSource::Create("redis://localhost:6379", "my-key-prefix");
11
12if (!redis_source) {
13 /* redis config is invalid, cannot proceed */
14}
15
16config_builder.DataSystem().Method(
17 LazyLoad().Source(*redis_source).CacheRefresh(std::chrono::seconds(15))
18);
19
20auto config = config_builder.Build();
21if (!config) {
22 /* an error occurred, config is not valid */
23}

Erlang

The Erlang integration is part of the main SDK distribution.

1LdOptions = #{
2 redis_host => "redis",
3 redis_port => "6379",
4 redis_prefix => "default",
5 feature_store => ldclient_storage_redis,
6 cache_ttl => 15
7},
8ldclient:start_instance("sdk-key-123abc", LdOptions).

Go

The Go integration is in github.com/launchdarkly/go-server-sdk-redis-redigo for 5.0.0 or higher of the SDK. In earlier SDK versions, it is in the main SDK distribution as the subpackage redis.

1import (
2 "time"
3
4 ld "github.com/launchdarkly/go-server-sdk/v6"
5 "github.com/launchdarkly/go-server-sdk/v6/ldcomponents"
6 ldredis "github.com/launchdarkly/go-server-sdk-redis-redigo"
7)
8
9var config ld.Config
10config.DataStore = ldcomponents.PersistentDataStore(
11 ldredis.DataStore().
12 HostAndPort("my-redis", 6379).
13 Prefix("my-key-prefix"),
14).CacheSeconds(30)
15client, _ := ld.MakeCustomClient(sdkKey, config, 5*time.Second)

To learn more, read go-server-sdk-redis-redigo.

Haskell

To use Redis with the Haskell SDK, you must install an additional package.

If you are working with version 4.0 of the Haskell SDK, use launchdarkly-server-sdk-redis-hedis, which is in a separate repository. It is compatible with version 4.0 and higher of the Haskell SDK.

If you are working with versions 3.x and earlier, use launchdarkly-server-sdk-redis.

Haskell
1import qualified Database.Redis as R
2import LaunchDarkly.Server
3import LaunchDarkly.Server.Store.Redis
4
5main = do
6 con <- R.checkedConnect R.defaultConnectInfo { R.connectHost = "my-redis:6379" }
7 backend <- makeRedisStore $ redisConfigSetNamespace "my-key-prefix" $ makeRedisStoreConfig con
8
9 let config = configSetStoreBackend backend $ makeConfig "sdk-key-123abc"
10
11 client <- makeClient config

Java

You must install the additional package com.launchdarkly.launchdarkly-java-server-sdk-redis-store.

Java
1import com.launchdarkly.sdk.server.*;
2import com.launchdarkly.sdk.server.integrations.*;
3
4LDConfig config = new LDConfig.Builder()
5 .dataStore(
6 Components.persistentDataStore(
7 Redis.dataStore().uri(URI.create("redis://my-redis:6379"))
8 .prefix("my-key-prefix")
9 ).cacheSeconds(30)
10 )
11 .build();
12LDClient client = new LDClient(sdkKey, config);

To learn more, read java-server-sdk-redis.

Lua

When you use Redis for storing data, you also need to include launchdarkly_server_sdk_redis:

1local l = require("launchdarkly_server_sdk")
2local r = require("launchdarkly_server_sdk_redis")
3
4local redis = r.makeRedisSource("redis://localhost:6379", "your-key-prefix")
5local config = {
6 dataSystem = {
7 lazyLoad = {
8 source = redis
9 }
10 }
11}
12
13local c = l.clientInit("sdk-key-123abc", 1000, config)

Node.js (server-side)

In version 8.0.0 and higher of the Node.js SDK, you must install the additional package @launchdarkly/node-server-sdk-redis. In versions 6.0.0-7.x of the Node.js SDK, you must install the additional package launchdarkly-node-server-sdk-redis.

In version 3.0 and higher of the Node.js SDK Redis integration, the ioredis package is used for Redis operations. In versions 2.x of the Node.js SDK Redis integration, the redis package is used for Redis operations.

1const ld = require('@launchdarkly/node-server-sdk');
2const RedisFeatureStore = require('@launchdarkly/node-server-sdk-redis');
3
4const store = RedisFeatureStore({
5 redisOpts: { host: 'redis-host', port: 6379 },
6 prefix: 'your-key-prefix',
7 cacheTTL: 30,
8});
9
10const options = {
11 featureStore: store
12};
13const client = ld.init(sdkKey, options);

PHP

There are two Redis integrations for the PHP SDK. One uses the Predis package, which can be used in any PHP environment. The other uses the more efficient phpredis extension, which must be installed in PHP itself. Both of these integrations are in v2, which is compatible with the PHP SDK v6.4 and later.

In version 4.0 and higher of the PHP SDK, you must add a package to your application’s Composer dependencies to use one of these two integrations:

  • For the Predis integration, add launchdarkly/server-sdk-redis-predis.
  • For the phpredis integration, add launchdarkly/server-sdk-redis-phpredis.

To use the Predis integration:

1$redisClient = new Predis\Client([
2 'host' => 'my-redis',
3 'port' => 6379
4]);
5
6$fr = LaunchDarkly\Integrations\Redis::featureRequester($redisClient, ['prefix' => 'my-key-prefix']);
7$client = new LaunchDarkly\LDClient("sdk-key-123abc", [
8 'feature_requester' => $fr
9]);

To use the phpredis integration:

PHP
1$fr = LaunchDarkly\Integrations\PHPRedis::featureRequester([
2 'redis_host' => 'my-redis',
3 'redis_port' => 6379,
4 'redis_prefix' => 'my-key-prefix'
5]);
6$client = new LaunchDarkly\LDClient("sdk-key-123abc", [
7 'feature_requester' => $fr
8]);

To learn more, read php-server-sdk-redis-predis or php-server-sdk-redis-phpredis.

Python

The Python integration is part of the main SDK distribution, but you must also install the package redis.

Python
1import ldclient
2from ldclient.config import Config
3from ldclient.feature_store import CacheConfig
4from ldclient.integrations import Redis
5
6store = Redis.new_feature_store(url='redis://my-redis:6379',
7 prefix='my-key-prefix', caching=CacheConfig(expiration=30))
8
9# Prior to version 6.7.0, use this syntax:
10# store = RedisFeatureStore(url='redis://my-redis:6379', prefix='my-key-prefix',
11# expiration=30)
12
13config = Config(feature_store=store)
14ldclient.set_config(config)

Ruby

The Ruby integration is part of the main SDK distribution, but you must also install the gems redis and connection_pool.

Ruby
1require 'ldclient-rb'
2
3store = LaunchDarkly::Integrations::Redis.new_feature_store(
4 redis_url: 'redis://my-redis:6379',
5 prefix: 'my-key-prefix',
6 expiration: 30
7)
8
9# Prior to version 5.5.0, use "LaunchDarkly::RedisFeatureStore.new" instead
10# of "LaunchDarkly::Integrations::Redis.new_feature_store"
11
12config = LaunchDarkly::Config.new(
13 feature_store: store
14)
15client = LaunchDarkly::Client.new(sdk_key, config)
Built with