• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
    No results for ""
    EXPAND ALL

    EDIT ON GITHUB

    Redis

    Read time: 3 minutes
    Last edited: May 13, 2022

    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 (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.
    • 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 30 seconds.

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

    .NET (server-side)

    Expand .NET (server-side) code sample

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

    using LaunchDarkly.Sdk.Server;
    using LaunchDarkly.Sdk.Server.Integrations;
    var config = Configuration.Builder(sdkKey)
    .DataStore(
    Components.PersistentDataStore(
    Redis.DataStore()
    .HostAndPort("my-redis", 6379)
    .Prefix("my-key-prefix")
    ).CacheSeconds(30)
    )
    .Build();
    var client = new LDClient(config);
    // Before version 6.0, the syntax was different:
    using LaunchDarkly.Client;
    using LaunchDarkly.Client.Redis;
    var store = RedisComponents.RedisFeatureStore()
    .WithRedisHostAndPort("my-redis", 6379)
    .WithPrefix("my-key-prefix")
    .WithCacheExpiration(TimeSpan.FromSeconds(30));
    var config = Configuration.Default(sdkKey)
    .WithFeatureStoreFactory(store);
    var client = new LDClient(config);

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

    C/C++ (server-side)

    Expand C/C++ (server-side) code sample

    The C/C++ integration is part of the main SDK distribution.

    #include <launchdarkly/api.h>
    #include <launchdarkly/store/redis.h>
    struct LDConfig *config = LDConfigNew("my-key");
    struct LDRedisConfig *redisConfig = LDRedisConfigNew();
    struct LDStoreInterface *store = LDStoreInterfaceRedisNew(redisConfig);
    LDConfigSetFeatureStoreBackend(config, store);

    Erlang

    Expand Erlang code sample
    LdOptions = #{
    redis_host => "redis",
    redis_port => "6379",
    redis_prefix => "default",
    feature_store => ldclient_storage_redis
    },
    ldclient:start_instance("your_sdk_key", LdOptions).

    Go

    Expand Go code sample

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

    import (
    ld "gopkg.in/launchdarkly/go-server-sdk.v5"
    "gopkg.in/launchdarkly/go-server-sdk.v5/ldcomponents"
    ldredis "github.com/launchdarkly/go-server-sdk-redis"
    )
    var config ld.Config
    config.DataStore = ldcomponents.PersistentDataStore(
    ldredis.DataStore().
    HostAndPort("my-redis", 6379).
    Prefix("my-key-prefix"),
    ).CacheSeconds(30)
    client := ld.MakeCustomClient(sdkKey, config, waitTime)

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

    Haskell

    Expand Haskell code sample

    If using the Haskell SDK you must install an additional package named launchdarkly-server-sdk-redis.

    import qualified Database.Redis as R
    import LaunchDarkly.Server
    import LaunchDarkly.Server.Store.Redis
    main = do
    con <- R.checkedConnect R.defaultConnectInfo { R.connectHost = "my-redis:6379" }
    backend <- makeRedisStore $ redisConfigSetNamespace "my-key-prefix" $ makeRedisStoreConfig con
    let config = configSetStoreBackend backend $ makeConfig "your_sdk_key"
    client <- makeClient config

    Java

    Expand Java code sample

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

    import com.launchdarkly.sdk.server.*;
    import com.launchdarkly.sdk.server.integrations.*;
    LDConfig config = new LDConfig.Builder()
    .dataStore(
    Components.persistentDataStore(
    Redis.dataStore().uri(URI.create("redis://my-redis:6379"))
    .prefix("my-key-prefix")
    ).cacheSeconds(30)
    )
    .build();
    LDClient client = new LDClient(sdkKey, config);

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

    Lua

    Expand Lua code sample
    local l = require("launchdarkly_server_sdk")
    local r = require("launchdarkly_server_sdk_redis")
    local backend = r.makeStore({
    host = "my-redis",
    port = 6379,
    prefix = "my-key-prefix"
    })
    local c = l.clientInit({
    key = "your_sdk_key",
    featureStoreBackend = backend
    )}

    Node.js (server-side)

    Expand Node.js (server-side) code sample

    In version 6.0.0 and higher of the Node.js SDK, you must install the additional package launchdarkly-node-server-sdk-redis. In Node.js SDK 5.x and earlier, this integration is part of the main SDK distribution.

    const ld = require('launchdarkly-node-server-sdk');
    const RedisFeatureStore = require('launchdarkly-node-server-sdk-redis');
    const redisOpts = {
    url: 'redis://your-redis:6379'
    };
    const store = RedisFeatureStore(redisOpts, 30, 'your-key-prefix');
    const options = {
    featureStore: store
    };
    const client = LaunchDarkly.init(sdkKey, options);

    PHP

    Expand PHP code sample

    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.

    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.

    In versions 3.x and earlier, the Redis integrations are built into the main SDK package, but you must add a package dependency for predis/predis if you are using Predis.

    To use the Predis integration:

    $fr = LaunchDarkly\Integrations\Redis::featureRequester([
    'redis_host' => 'my-redis',
    'redis_port' => 6379,
    'redis_prefix' => 'my-key-prefix'
    ]);
    $client = new LaunchDarkly\LDClient("your_sdk_key", [
    'feature_requester' => $fr
    ]);

    To use the phpredis integration:

    $fr = LaunchDarkly\Integrations\PHPRedis::featureRequester([
    'redis_host' => 'my-redis',
    'redis_port' => 6379,
    'redis_prefix' => 'my-key-prefix'
    ]);
    $client = new LaunchDarkly\LDClient("your_sdk_key", [
    'feature_requester' => $fr
    ]);

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

    Python

    Expand Python code sample

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

    import ldclient
    from ldclient.config import Config
    from ldclient.feature_store import CacheConfig
    from ldclient.integrations import Redis
    store = Redis.new_feature_store(url='redis://my-redis:6379',
    prefix='my-key-prefix', caching=CacheConfig(expiration=30))
    # Prior to version 6.7.0, use this syntax:
    # store = RedisFeatureStore(url='redis://my-redis:6379', prefix='my-key-prefix',
    # expiration=30)
    config = Config(feature_store=store)
    ldclient.set_config(config)

    Ruby

    Expand Ruby code sample

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

    require 'ldclient-rb'
    store = LaunchDarkly::Integrations::Redis.new_feature_store(
    redis_url: 'redis://my-redis:6379',
    prefix: 'my-key-prefix',
    expiration: 30
    )
    # Prior to version 5.5.0, use "LaunchDarkly::RedisFeatureStore.new" instead
    # of "LaunchDarkly::Integrations::Redis.new_feature_store"
    config = LaunchDarkly::Config.new(
    feature_store: store
    )
    client = LaunchDarkly::Client.new(sdk_key, config)