Migrations
Read time: 25 minutes
Last edited: Nov 12, 2024
Overview
This topic explains how to use LaunchDarkly SDKs to manage migrations or modernizations. You might use this feature if you are optimizing queries, upgrading to new tech stacks, migrating from one database to another, or other similar technology changes. This feature is available for server-side and edge SDKs only.
Prerequisites
Before you configure your SDK to manage a migration, you must complete the following prerequisites:
- Create a migration feature flag. This is a temporary flag used to migrate data or systems while keeping your application available and disruption free. Migration flags break up the switch from an old to a new implementation into a series of recommended stages where movement from one stage to the next is done in incremental steps.
- Determine how many stages your migration will have. You can select from the following options as part of creating a migration feature flag:
- Two stages: For migrations where you cannot run the new system and old system at the same time
- Four stages: For migrations that can run both the new and old systems at the same time
- Six stages: For migrations where you need to migrate
READS
andWRITES
separately
To learn more, read Migration flags.
Use SDKs to manage a migration
Depending on how you created your migration feature flag, your migration will have two, four, or six stages. At each stage, you will be reading data from the old destination, the new destination, or both. You will also be writing data to the old destination, the new destination, or both. At each stage, only one of these destinations is considered the authoritative source. In the LaunchDarkly SDK, you can determine which stage of the migration your application is currently in, execute the appropriate read and write methods, and then compare the results to check correctness and view any errors or changes in latency.
The following table describes the stages and which destination is authoritative. Remember that not all migrations will use all stages.
Stage | Read from | Write to | Authoritative |
---|---|---|---|
off | old | old | old |
dualwrite | old | old, new | old |
shadow | both | old, new | old |
live | both | new, old | new |
rampdown | new | new, old | new |
complete | new | new | new |
To manage your migration:
- Configure the migration. In your SDK configuration, define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
- Call the read and write methods you defined, using the SDK's migrator. The migrator determines the migration stage of the feature flag controlling the migration, and performs reads and writes to the old and new systems based on the migration stage.
For details of how to perform each step, read Server-side SDKs or Edge SDKs, below.
During the migration, you can check the consistency, errors, and latency as you manage your migration. This information is available from the flag's Targeting tab. To learn more, read Migration flags.
Customize your migration
Customizing your migration is rare. If you have additional metrics that you want to track, or if your migration or modernization involves reading and writing from the new and old systems in a different configuration than the two, four, or six -stage migrations provided, you can also use the SDK to customize your migration.
Here's how:
- Configure the migration. In your SDK configuration, define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
- Use the
migrationVariation
method to evaluate your feature flag and determine the migration stage. - Use your own logic to perform the appropriate migration operations for the stage. Record any metrics that you are interested in.
- When the migration operation is complete, call the
trackMigration
method to record your metrics.
For details of how to perform each step, read Server-side SDKs or Edge SDKs, below.
Server-side SDKs
This feature is available in the following server-side SDKs:
.NET (server-side)
Expand .NET (server-side) code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call Read
or Write
. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
LDContext context = Context.Builder("context-key-123abc").Build();// this is the migration stage to use if the flag's migration stage// is not available from LaunchDarklyvar defaultStage = MigrationStage.Offvar readResult = migration.Read("migration-flag-key-123abc", context, defaultStage, payload);var writeResult = migration.Write("migration-flag-key-123abc", context, defaultStage, payload);
To learn more, read Read
and Write
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the MigrationVariation
method to evaluate your feature flag and determine its migration stage. This method returns the migration stage and a tracker that you can use to build the analytics event to send back to LaunchDarkly.
Here's how:
LDContext context = Context.Builder("context-key-123abc").Build();var (stage, tracker) = client.MigrationVariation("migration-flag-key-123abc", context, MigrationStage.Off);
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
// define the combination of reads and writes from the new and old systems// that should occur at each migration stageswitch (stage){case MigrationStage.Off:case MigrationStage.DualWrite:case MigrationStage.Shadow:case MigrationStage.Live:case MigrationStage.RampDown:case MigrationStage.Complete:default:// throw an error}
Finally, when the migration operation is complete, call the TrackMigration
method to record your metrics:
client.TrackMigration(tracker);
To learn more, read IMigration
, MigrationVariation
, and TrackMigration
.
Go
Expand Go code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call Read
or Write
. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
context := ldcontext.New("context-key-123abc")// this is the migration stage to use if the flag's migration stage// is not available from LaunchDarklydefaultStage := ldmigration.OffreadResult := migrator.Read("migration-flag-key-123abc", context, defaultStage, nil)writeResult := migrator.Write("migration-flag-key-123abc", context, defaultStage, nil)
To learn more, read Read
and Write
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the MigrationVariation
method to evaluate your feature flag and determine its migration stage. This method returns the migration stage, a tracker that you can use to build the analytics event to send back to LaunchDarkly, and an error.
Here's how:
context := ldcontext.New("context-key-123abc")stage, tracker, err := client.MigrationVariation("migration-flag-key-123abc", context, ldmigration.Off)
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
// define the combination of reads and writes from the new and old systems// that should occur at each migration stageswitch stage {case ldmigration.Off:case ldmigration.DualWrite:case ldmigration.Shadow:case ldmigration.Live:case ldmigration.RampDown:case ldmigration.Complete:default: {// throw an error}}
Finally, when the migration operation is complete, call the TrackMigrationOp
method to record your metrics:
event, _ := tracker.Build();err := client.TrackMigrationOp(*event);
To learn more, read Migration
and TrackMigrationOp
.
Java
Expand Java code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call read
or write
. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
LDContext context = LDContext.builder("context-key-123abc").build();// this is the migration stage to use if the flag's migration stage// is not available from LaunchDarklyMigrationStage defaultStage = MigrationStage.OFFMigration.MigrationResult<String> readResult = migration.read("migration-flag-key-123abc", context, defaultStage);Migration.MigrationWriteResult<String> writeResult = migration.write("migration-flag-key-123abc", context, defaultStage);
To learn more, read Migration
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the migrationVariation
method to evaluate your feature flag and determine its migration stage. This method returns the migration stage, a tracker that you can use to build the analytics event to send back to LaunchDarkly, and an error.
Here's how:
LDContext context = LDContext.builder("context-key-123abc").build();MigrationVariation migrationVariation = client.migrationVariation("migration-flag-key-123abc", context, MigrationStage.OFF);
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
// define the combination of reads and writes from the new and old systems// that should occur at each migration stageswitch (migrationVariation.getStage()) {case OFF:case DUAL_WRITE:case SHADOW:case LIVE:case RAMP_DOWN:case COMPLETE:default: {// throw an error}}
Finally, when the migration operation is complete, call the trackMigration
method to record your metrics:
MigrationOpTracker tracker = migrationVariation.getTracker();client.trackMigration(tracker);
To learn more, read migrationVariation
and trackMigration
.
Node.js (server-side)
Expand Node.js (server-side) code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call the read
or write
methods from the LDMigration
interface. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
const ld = require('@launchdarkly/node-server-sdk');const context: ld.LDContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',};// this is the migration stage to use if the flag's migration stage// is not available from LaunchDarklylet defaultStage: ld.LDMigrationStage = LDMigrationStage.Off;const migration = ld.createMigration(client, options);// when you need to perform a read in your applicationmigration.read('migration-flag-key-123abc',context,defaultStage);// when you need to perform a write in your applicationmigration.write('migration-flag-key-123abc',context,defaultStage);
To learn more, read LDMigration
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the migrationVariation
method to evaluate your feature flag and determine its migration stage. This method returns a promise that is resolved with the result LDMigrationVariation
. This result includes the migration stage and a tracker that you can use to build the analytics event to send back to LaunchDarkly.
Here's how:
const ld = require('@launchdarkly/node-server-sdk');const context: ld.LDContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',};const { value, tracker } = await client.migrationVariation('migration-flag-key-123abc',context,false);
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
// define the combination of reads and writes from the new and old systems// that should occur at each migration stageswitch (value) {case LDMigrationStage.Off: { },case LDMigrationStage.DualWrite: { },case LDMigrationStage.Shadow: { },case LDMigrationStage.Live: { },case LDMigrationStage.RampDown: { },case LDMigrationStage.Complete: { },default: {// throw an error}}
Finally, when the migration operation is complete, call the trackMigration
method to record your metrics:
const event = tracker.createEvent();if (event) {client.trackMigration(event);}
To learn more, read LDMigration
. and LDMigrationOpEvent
.
PHP
Expand PHP code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call the read
or write
methods from the Migrator
interface. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
$context = LaunchDarkly\LDContext::builder("context-key-123abc")->build();// this is the migration stage to use if the flag's migration stage// is not available from LaunchDarkly$defaultStage = Migrations\Stage::OFF;$result = $builder->build();if (!$result->isSuccessful()) {throw new \Exception($result->error);}$migrator = $result->value;// if you need to pass additional information from the call site// to your read/write methods, use a mixed type payload$payload = ['index' => 'useful information'];// when you need to perform a read in your application$migrator->read('migration-flag-key-123abc', $context, $defaultStage, $payload);// when you need to perform a write in your application$migrator->write('migration-flag-key-123abc', $context, $defaultStage, $payload);
To learn more, read Migrator
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the migrationVariation
method to evaluate your feature flag and determine its migration stage. This method returns the migration stage and a tracker that you can use to build the analytics event to send back to LaunchDarkly.
Here's how:
$context = LaunchDarkly\LDContext::builder("context-key-123abc")->build();$result = $client->migrationVariation('migration-flag-key-123abc', $context, Migrations\Stage::OFF);/** @var Migrations\Stage */$stage = $result['stage'];/** @var Migrations\OpTracker */$tracker = $result['tracker'];
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
// define the combination of reads and writes from the new and old systems// that should occur at each migration stageswitch ($stage) {case Migrations\Stage::OFF:case Migrations\Stage::DUALWRITE:case Migrations\Stage::SHADOW:case Migrations\Stage::LIVE:case Migrations\Stage::RAMPDOWN:case Migrations\Stage::COMPLETE:default:// throw an error}
Finally, when the migration operation is complete, call the trackMigrationOperation
method to record your metrics:
$client->trackMigrationOperation($tracker);
To learn more, read Migrator
and trackMigrationOperation
.
Python
Expand Python code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call the read
or write
methods from the Migrator
interface. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
from ldclient import Stagecontext = Context.builder("context-key-123abc").build()# this is the migration stage to use if the flag's migration stage# is not available from LaunchDarklydefault_stage = Stage.OFFmigrator = builder.build()# when you need to perform a read in your applicationmigrator.read('migration-flag-key-123abc',context,default_stage)# when you need to perform a write in your applicationmigrator.write('migration-flag-key-123abc',context,default_stage)
To learn more, read ldclient.migrations
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the migration_variation
method to evaluate your feature flag and determine its migration stage. This method returns the migration stage and a tracker that you can use to build the analytics event to send back to LaunchDarkly.
Here's how:
context = Context.builder("context-key-123abc").build()stage, tracker = ldclient.get().migration_variation('migration-flag-key-123abc', context, Stage.OFF)
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
# define the combination of reads and writes from the new and old systems# that should occur at each migration stageif stage == Stage.OFF:elif stage == Stage.DUALWRITE:elif stage == Stage.SHADOW:elif stage == Stage.LIVE:elif stage == Stage.RAMPDOWN:elif stage == Stage.COMPLETE:else:# throw an error
Finally, when the migration operation is complete, call the track_migration_op
method to record your metrics:
ldclient.get().track_migration_op(tracker)
To learn more, read ldclient.migrations
and track_migration_op
.
Ruby
Expand Ruby code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call read
or write
. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
context = LaunchDarkly::LDContext.create({key: "user-key-123abc", kind:"user"})# this is the migration stage to use if the flag's migration stage# is not available from LaunchDarklydefault_stage = LaunchDarkly::Migrations::STAGE_OFFread_result = migrator.read("migration-flag-key-123abc", context, default_stage, payload)write_result = migration.write("migration-flag-key-123abc", context, default_stage, payload)
To learn more, read read
and write
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the migration_variation
method to evaluate your feature flag and determine its migration stage. This method returns the migration stage and a tracker that you can use to build the analytics event to send back to LaunchDarkly.
Here's how:
context = LaunchDarkly::LDContext.create({key: "user-key-123abc", kind:"user"})stage, tracker = client.migration_variation("migration-flag-key-123abc",context,LaunchDarkly::Migrations::STAGE_OFF)
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
# define the combination of reads and writes from the new and old systems# that should occur at each migration stagecase stagewhen LaunchDarkly::Migrations::STAGE_OFFwhen LaunchDarkly::Migrations::STAGE_DUALWRITEwhen LaunchDarkly::Migrations::STAGE_SHADOWwhen LaunchDarkly::Migrations::STAGE_LIVEwhen LaunchDarkly::Migrations::STAGE_RAMPDOWNwhen LaunchDarkly::Migrations::STAGE_COMPLETEelse# throw an errorend
Finally, when the migration operation is complete, call the track_migration_op
method to record your metrics:
client.track_migration_op(tracker);
To learn more, read Migrator
and track_migration_op
.
Rust
Expand Rust code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call read
or write
. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
let context = ContextBuilder::new("user-key-123abc").kind("user").build().expect("Context failed to build");// this is the migration stage to use if the flag's migration stage// is not available from LaunchDarklylet default_stage = Stage::Off;let read_result = migrator.read(&context,"migration-flag-key-123abc".into(),default_stage,"example-payload".into(),).await;let write_result = migrator.write(&context,"migration-flag-key-123abc".into(),default_stage,"example-payload".into(),).await;
To learn more, read read
and write
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the migration_variation
method to evaluate your feature flag and determine its migration stage. This method returns the migration stage and a tracker that you can use to build the analytics event to send back to LaunchDarkly.
Here's how:
let context = ContextBuilder::new("user-key-123abc").kind("user").build().expect("Context failed to build");let (stage, tracker) =client.migration_variation(&context, "migration-flag-key-123abc", Stage::Off);
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
// define the combination of reads and writes from the new and old systems// that should occur at each migration stagematch stage {Stage::Off => todo!(),Stage::DualWrite => todo!(),Stage::Live => todo!(),Stage::Shadow => todo!(),Stage::Rampdown => todo!(),Stage::Complete => todo!(),_ => todo!(),};
Finally, when the migration operation is complete, call the track_migration_op
method to record your metrics:
client.track_migration_op(tracker);
To learn more, read Migrator
and track_migration_op
.
Edge SDKs
This feature is available in the following edge SDKs:
Akamai
Expand Akamai code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call the read
or write
methods from the LDMigration
interface. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
import {createMigration,LDContext,LDMigrationStage,} from '@launchdarkly/akamai-server-edgekv-sdk';const context: LDContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',};// this is the migration stage to use if the flag's migration stage// is not available from LaunchDarklylet defaultStage: LDMigrationStage = LDMigrationStage.Off;const migration = createMigration(client, options);// when you need to perform a read in your applicationmigration.read('migration-flag-key-123abc',context,defaultStage);// when you need to perform a write in your applicationmigration.write('migration-flag-key-123abc',context,defaultStage);
To learn more, read LDMigration
.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the migrationVariation
method to evaluate your feature flag and determine its migration stage. This method returns a promise that is resolved with the result LDMigrationVariation
. This result includes the migration stage. It also returns a tracker, which you can ignore. (The tracker is normally used to build an analytics event to send back to LaunchDarkly. However, the Akamai SDK does not support sending events, so there is no need to build one.)
Here's how:
import {LDContext,} from '@launchdarkly/akamai-server-edgekv-sdk';const context: LDContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',};const { value, tracker } = await client.migrationVariation('migration-flag-key-123abc',context,false);
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
import { LDMigrationStage } from '@launchdarkly/akamai-server-edgekv-sdk';// define the combination of reads and writes from the new and old systems// that should occur at each migration stageswitch (value) {case LDMigrationStage.Off: { },case LDMigrationStage.DualWrite: { },case LDMigrationStage.Shadow: { },case LDMigrationStage.Live: { },case LDMigrationStage.RampDown: { },case LDMigrationStage.Complete: { },default: {// throw an error}}
To learn more, read LDMigration
.
Cloudflare
Expand Cloudflare code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call the read
or write
methods from the LDMigration
interface. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
import {createMigration,LDContext,LDMigrationStage,} from '@launchdarkly/cloudflare-server-sdk';const context: LDContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',};// this is the migration stage to use if the flag's migration stage// is not available from LaunchDarklylet defaultStage: LDMigrationStage = LDMigrationStage.Off;const migration = createMigration(client, options);// when you need to perform a read in your applicationmigration.read('migration-flag-key-123abc',context,defaultStage);// when you need to perform a write in your applicationmigration.write('migration-flag-key-123abc',context,defaultStage);
To learn more, read LDMigration
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the migrationVariation
method to evaluate your feature flag and determine its migration stage. This method returns a promise that is resolved with the result LDMigrationVariation
. This result includes the migration stage and a tracker that you can use to build the analytics event to send back to LaunchDarkly.
Here's how:
import {LDContext,} from '@launchdarkly/cloudflare-server-sdk'const context: LDContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',};const { value, tracker } = await client.migrationVariation('migration-flag-key-123abc',context,false);
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
import { LDMigrationStage } from '@launchdarkly/cloudflare-server-sdk';// define the combination of reads and writes from the new and old systems// that should occur at each migration stageswitch (value) {case LDMigrationStage.Off: { },case LDMigrationStage.DualWrite: { },case LDMigrationStage.Shadow: { },case LDMigrationStage.Live: { },case LDMigrationStage.RampDown: { },case LDMigrationStage.Complete: { },default: {// throw an error}}
Finally, when the migration operation is complete, call the trackMigration
method to record your metrics:
import {LDClient,LDMigrationTracker,} from '@launchdarkly/cloudflare-server-sdk';const event = tracker.createEvent();if (event) {client.trackMigration(event);}
To learn more, read LDMigration
and LDMigrationOpEvent
.
Vercel
Expand Vercel code sample
To manage your migration, first you need to define how to read from and write to the old and new systems, how to check whether two reads are a match, and whether to track errors and latency metrics. To learn more, read Migration configuration.
Then, whenever you need to perform a read or write in the systems that you are modernizing or migrating, call the read
or write
methods from the LDMigration
interface. The SDK evaluates the flag, determines which migration stage the flag is in, and performs the reads or writes in the appropriate system.
Here's how:
import {createMigration,LDContext,LDMigrationStage,} from '@launchdarkly/vercel-server-sdk';const context: LDContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',};// this is the migration stage to use if the flag's migration stage// is not available from LaunchDarklylet defaultStage: LDMigrationStage = LDMigrationStage.Off;const migration = createMigration(client, options);// when you need to perform a read in your applicationmigration.read('migration-flag-key-123abc',context,defaultStage);// when you need to perform a write in your applicationmigration.write('migration-flag-key-123abc',context,defaultStage);
To learn more, read LDMigration
.
You can check for consistency, errors, or latency under "Migration insights" on the Targeting tab of your migration flag in the LaunchDarkly user interface. To learn more, read Migration flags.
Expand Customizing your migration
Customizing your migration
Customizing your migration is rare. If you want to customize your migration, configure your migration information as before.
Then, use the migrationVariation
method to evaluate your feature flag and determine its migration stage. This method returns a promise that is resolved with the result LDMigrationVariation
. This result includes the migration stage and a tracker that you can use to build the analytics event to send back to LaunchDarkly.
Here's how:
import {LDContext,} from '@launchdarkly/vercel-server-sdk';const context: LDContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',};const { value, tracker } = await client.migrationVariation('migration-flag-key-123abc',context,false);
Next, perform the migration for the appropriate stage. At each stage, the migration may involve reading or writing from one or both systems. You must define the behavior for each stage. To learn more about how LaunchDarkly defines the stages, read Use SDKs to manage a migration, above.
The structure looks like this:
import { LDMigrationStage } from '@launchdarkly/vercel-server-sdk';// define the combination of reads and writes from the new and old systems// that should occur at each migration stageswitch (value) {case LDMigrationStage.Off: { },case LDMigrationStage.DualWrite: { },case LDMigrationStage.Shadow: { },case LDMigrationStage.Live: { },case LDMigrationStage.RampDown: { },case LDMigrationStage.Complete: { },default: {// throw an error}}
Finally, when the migration operation is complete, call the trackMigration
method to record your metrics:
import {LDMigrationTracker,LDClient} from '@launchdarkly/vercel-server-sdk';const event = tracker.createEvent();if (event) {client.trackMigration(event);}
To learn more, read LDMigration
and LDMigrationOpEvent
.