This topic explains how LaunchDarkly SDKs work. Our SDKs are all open source, and we encourage pull requests and other contributions from the community.
This topic is intended to help anyone create a LaunchDarkly server-side or client-side SDK for a new platform from scratch, or contribute to an existing SDK implementation. We encourage you to reach out to us for additional details if you are interested in contributing to an edge SDK or AI SDK implementation.
There are three main components to a LaunchDarkly SDK implementation:
LaunchDarkly SDKs store all feature flags in memory. SDKs typically receive flag updates asynchronously through our streaming protocol, as is the default case in most SDKs. While LaunchDarkly’s various SDKs typically support the same two mechanisms for receiving feature flag updates, their implementation details vary.
The SDKs can use either of the following mechanisms to populate a feature flag store, which is a thread-safe, in-memory map from feature flag keys to feature flag JSON. Evaluating a feature flag is a very lightweight and inexpensive effort and requires no per-evaluation requests to LaunchDarkly’s servers.
Server-side SDKs can read flags from the in-memory store and evaluate them using the evaluation algorithm, and client-side SDKs can retrieve pre-evaluated flag values from the in-memory store. To learn more, read Evaluating feature flags.
Flag updates are triggered when you make changes on your Flags list, and arrive by one of two mechanisms:
Expose the store itself as an interface so that you can easily implement alternative configurations, for example, a Redis-backed store.
Additionally, server-side SDKs often support a mechanism for receiving feature flag updates without establishing a direct connection to LaunchDarkly. This mechanism relies on the LaunchDarkly Relay Proxy to connect to LaunchDarkly, receive feature flag updates, and keep a persistent feature store up-to-date. When a server-side SDK is configured to operate in this mode, it will defer to the Relay Proxy to update the persistent feature store whenever a feature flag is updated. This mode should be configurable and disabled by default. To learn more, read Using a persistent feature store without connecting to LaunchDarkly.
This section corresponds to a subset of LaunchDarkly’s client-side SDKs. To learn more about the client-side ID, read Keys.
Flag updates are triggered when you make changes on your Flags list, and arrive by one of two mechanisms:
There are two ways to use this method:
There are two ways to use this method:
This section corresponds to a subset of LaunchDarkly’s client-side SDKs. To learn more about the mobile key, read Keys.
Flag updates are triggered when you make changes on your Flags list, and arrive by one of two mechanisms:
${mobile_key}, where ${mobile_key} is the mobile key the client application passes to the LaunchDarkly client configuration.There are two ways to use this method:
Authorization header with value ${mobile_key}, where ${mobile_key} is the mobile key the client application passed to the LaunchDarkly client configuration.There are two ways to use this method:
Server-side and client-side SDKs significantly differ in how they evaluate feature flags. For a high-level explanation, read Flag evaluations.
To implement the variation method in a server-side SDK, a feature flag is retrieved from the store and evaluated directly in the SDK. To learn more about evaluation rules, read Flag evaluation rules in server-side SDKs.
The LaunchDarkly Go SDK serves as our reference evaluation algorithm.
As described in Flag evaluations, client-side SDKs do not evaluate flags on their own. LaunchDarkly’s services are responsible for evaluating flag rules.
To implement the variation method in a client-side SDK, return the last-known evaluated value from the store for the specified flag key.
This page is targeted at contributors to LaunchDarkly SDKs. If you are a LaunchDarkly customer looking for a way to import your metric events into LaunchDarkly from an existing data source, or a data provider looking to integrate with LaunchDarkly, use the metric import API to record your events instead. To learn more, read Importing metric events.
Most SDKs send the following kinds of events:
summary events describe a set of individual feature evaluations over an interval.feature events include additional feature flag evaluation details for flags used in Experimentation and flags you enable detailed tracking for.debug events describe feature flag evaluations when debugging mode is on.index and identify events push context data to LaunchDarkly.page view and click events are sent by JavaScript-based SDKs when pages are visited or clicked as part of an experiment.custom events are sent when an application calls the SDK’s track method.migration_op events report on metrics specific to migration flags.diagnostic events report on how the SDK is configured and operating.Server-side, client-side, and mobile SDKs must batch and send events to the LaunchDarkly server asynchronously. This is a requirement.
For server-side, client-side, and mobile SDKs, do this by creating a background job that runs every 30 seconds and makes a POST call to /bulk for server-side SDKs, to /events/bulk/{envId} for client-side JavaScript SDKs, and to /mobile/events/bulk for mobile SDKs.
This call must have a Content-Type header set to application/json and must include a JSON body consisting of a list of events. Calls to server-side and mobile SDKs must have an Authorization header set to api_key ${api_key}. Client-side JavaScript SDKs do not need an Authorization header.
The time between batch calls to the event API should be configurable. The SDK should have a configuration element for the maximum number of events to store between batch calls. If this capacity is exceeded before a batch call, events should be discarded and a warning logged.
A feature event should only be generated in one of these scenarios:
trackEvents or trackEventsFallthrough attribute on the flag configuration is senttrackEvents attribute on a targeting rule is sent"experiment" and the variation associated with the event is included in the tracked variations for that rolloutFor feature events, the kind attribute should always be "feature". Evaluating flag prerequisites generates one additional feature event per evaluation. These feature events should have the prereqOf attribute set to the originating flag’s key. The prereqOf attribute should only be set for flags which are evaluated to determine a prerequisite value.
Depending on your SDK configuration, feature events may include all of the context attribute values instead of the contextKey. In this case, the SDK sends the context attributes in a context property. If feature events include all context attribute values, the SDK will not send a corresponding index event.
For an example and the full list of feature event properties, read Feature events.
The debug event is a variant on the feature event, with two differences. It has kind set to "debug" and it inlines the context value. It is only sent if the debugEventsUntilDate attribute is set for a feature flag and indicates a UNIX epoch timestamp in milliseconds that has not yet elapsed. debug events are not controlled by the trackEvents field.
For an example and the full list of debug event properties, read Debug events.
SDKs periodically transmit details for the context object used in a feature flag evaluation as reported by the feature event with a separate index event. The SDK will only send index events when a context’s attributes change.
The kind for an index event is index. The value of context is the same as described above for a debug event.
For an example and the full list of index event properties, read Index events.
identify events are produced when the client application calls an SDK method called identify. identify events have an identical structure to index events, except the kind is set to identify.
For an example and the full list of identify event properties, read Identify events.
SDKs produce custom events when the client application calls the SDK’s track method.
Custom events can contain the following optional attributes:
data can contain any arbitrary JSON.metricValue can contain a number.Depending on your SDK configuration and version, custom events may include all of the context attribute values instead of the contextKey. This capability is referred to as “Inline contexts in custom events” in the Supported features table, and enables additional features in the LaunchDarkly UI, such as Filtering custom metric events. In this case, the SDK sends the context attributes in a context property. If custom events include all context attribute values, the SDK will not send a corresponding index event.
For an example and the full list of custom event properties, read Custom events.
The SDK is expected to send summary events for every flush interval describing all of the feature evaluations that occurred during that interval. Summary events include all feature evaluations, regardless of whether the trackEvents field was set for individual flags.
The summary event represents a set of feature flag evaluations occurring during an interval defined by startDate and endDate, broken out by feature flag.
The SDK should generate a separate entry in counters for each unique value/version/variation combination encountered during the interval.
The call to /api/events/bulk should contain a JSON list of events.
For an example and the full list of summary event properties, including counters properties, read Summary events.
SDKs that support migration flags send a migration_op event back to LaunchDarkly after each migration operation to record the migration metrics, such as consistency rate, P99 latency, and error rate. To learn more, read the product documentation on Migration flags and the SDK feature documentation on Migrations.
Here is an example migration_op event:
Here is a list of the migration_op event properties:
kind: The kind of a migration_op event is migration_op.creationDate: When the SDK requested the migration operation, as UNIX epoch time in milliseconds.contextKeys: The keys of the context object used in a feature flag evaluation.operation: The kind of migration operation, either read or write.evaluation: The results of the migration feature flag evaluation.measurements: The migration flag metrics for this flag evaluation, for the consistency, p99 latency, and error rate. To learn more, read Migration flag metrics.diagnostic events are optional events which report on the SDK’s configuration and operational behavior. These events are strictly for use by LaunchDarkly’s internal teams and are intended to provide insights into how SDKs are operating in the field. Diagnostic events are not used for any customer-facing features nor is the resulting data accessible to customers. Support for diagnostic events varies across LaunchDarkly SDKs.
diagnostic events have two variants:
diagnostic event is sent upon SDK initialization. This event summarizes high-level information about the underlying platform such as the operating system name and version and the platform version name and version (e.g. Java 1.8 or Python 3.5). Additionally, this event includes information about how the SDK is configured.diagnostic event is sent periodically throughout the SDK’s lifetime. By default this event is sent every 15 minutes. This event describes the SDK’s operational behavior in the time lapsed since the prior diagnostic event’s submission. For example, the event includes information about the SDK’s attempts to establish a streaming connection to LaunchDarkly and a counter of how many events were most recently sent to LaunchDarkly.The technical details of how to implement diagnostic events in a LaunchDarkly SDK are beyond the scope of this document. It is not expected that a third-party would implement diagnostic events when building a new SDK from scratch.
Content-Type header set to application/json.We’ve observed a few common mistakes in integrating the LaunchDarkly SDK at customer sites. It’s helpful for the SDKs to introduce specific logging around these errors and handle them as gracefully as possible:
null or the language equivalent as the context key, or omitting the key property altogether. SDKs should log this at error level and return the default value.Here are some examples from other LaunchDarkly SDK implementations:
The LaunchDarkly REST API documentation may be helpful as well.
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: “users.”
Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.