Multi-contexts
Read time: 2 minutes
Last edited: Aug 28, 2024
Overview
This topic includes information about multi-contexts. Contexts are people, services, machines, or other resources that encounter feature flags in your product. Multi-contexts are combinations of several context kinds into one context. Multi-contexts allow your feature flags to target entities based on data from multiple contexts. For multi-contexts, the context kind
is set to multi
.
This is a representation of what two multi-contexts might look like:
Example multi-context: Anna at Global Health Services
For example, imagine Anna is a doctor who works for a hospital chain called Global Health Services. Anna works at two different locations, Springfield and Midway. Jesse is a nurse that works with Anna at the Springfield location.
Maybe you want to serve one variation of a flag to doctors working at the Global Health Services Springfield location, and a different variation to nurses at the Springfield location, and you want to serve a third variation to doctors at the Midway location. In this situation, you can have the LaunchDarkly SDK send multiple contexts at once during the flag evaluation, so that LaunchDarkly can evaluate targeting rules using data from both the "user" and "organization" contexts at the same time.
In this example, the multi-contexts contain two associated contexts: one for user, and one for organization.
Here is an example of what the data structure might look like for these two multi-contexts, though each SDK sends context data to LaunchDarkly in a slightly different format:
// Anna's multi-context"context": {"kind": "multi","user": {"key": "user-key-123abc","name": "Anna","email": "anna@globalhealthexample.com","jobFunction": "doctor"},"organization": {"key": "org-key-123abc","name": "Global Health Services","address": {"street": "123 Main Street","city": "Springfield"}}}// Jesse's multi-context"context": {"kind": "multi","user": {"key": "user-key-456def","name": "Jesse","email": "jesse@globalhealthexample.com","jobFunction": "nurse"},"organization": {"key": "org-key-123abc","name": "Global Health Services","address": {"street": "123 Main Street","city": "Springfield"}}}
This configuration allows you to target the user and organization together to ensure you serve the correct variation. This helps eliminate risk and ensure accuracy in complex deliveries.
The multiple contexts that appear in a multi-context are called "associated contexts." For example, the Anna
user context and the Global Health Services
organization context are associated contexts.