A flag is configuration with a lifecycle
This is the free lesson
Next: The default is mandatory
The rest of this course is part of Pro, together with every other course and the ones added later.
$25 a month billed yearly, or $29 month to month
Until 30 November: $210 for the first year, or $14.50 for the first month, with code LAUNCH.
A free account keeps your progress across devices. The paid lessons need Pro.
Code Snippets
{
"$schema": "https://flagd.dev/schema/v0/flags.json",
"flags": {
"v2_enabled": {
"state": "ENABLED", "variants": { "on": true, "off": false }, "defaultVariant": "on"
}
}
}package demo;
import dev.openfeature.contrib.providers.flagd.Config;
import dev.openfeature.contrib.providers.flagd.FlagdOptions;
import dev.openfeature.contrib.providers.flagd.FlagdProvider;
import dev.openfeature.sdk.Client;
import dev.openfeature.sdk.OpenFeatureAPI;
public class Checkout {
public static void main(String[] args) {
FlagdProvider provider = new FlagdProvider(FlagdOptions.builder()
.resolverType(Config.Resolver.FILE)
.offlineFlagSourcePath("flags.json")
.build());
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProviderAndWait(provider);
Client client = api.getClient("checkout");
boolean on = client.getBooleanValue("v2_enabled", false);
System.out.println(on ? "checkout: new flow (v2)" : "checkout: old flow (v1)");
api.shutdown();
}
}#!/bin/bash
# Compile, then run one class: ./run Checkout
set -e
mvn -q compile dependency:build-classpath -Dmdep.outputFile=cp.txt
java -cp "target/classes:$(cat cp.txt)" "demo.$1" "${@:2}"java -version 2>&1 | grep Runtime
mvn -v | head -1
find . -type f
grep -E '<(artifactId|version)>' pom.xml
mkdir -p src/main/java/demo
chmod +x run
./run Checkout
sed -i '' 's/"defaultVariant": "on"/"defaultVariant": "off"/' flags.json
./run Checkout
grep -rn v2_enabled src flags.jsonIn this video you'll move a feature flag out of an if statement and behind OpenFeature, with flagd reading the flag from a file. Your code asks the OpenFeature SDK, the software development kit, for a flag, through its application programming interface, the Evaluation API, and always passes a default value. Behind the API, a provider fetches the value from the flag source: here the flagd provider, which reads a JSON file, in JavaScript Object Notation. The code never talks to the flag source directly. In this lesson you will meet the four kinds of feature flags, evaluate a flag with the OpenFeature Java SDK and flagd, and change it in its file instead of the code. Before you start, you need a Java 25 JDK, the Java Development Kit, and Maven. Homebrew installs both. Java 25 from Eclipse Temurin, and Maven 3.9.16. The project is a plain Maven project, with a pom file and the settings of a small logger. The pom file lists three libraries. The OpenFeature SDK 1.23.0, the software development kit that your code calls. The flagd provider 0.15.0, the adapter to where the flags live. And a simple logger. At the start, a feature flag is a glorified conditional: an if statement around the new code. In his talk at the Amsterdam Java User Group, Simon Schrottner showed where the homegrown path ends: an API, a user interface, targeting and observability. His slide said: Congratulations, you're a vendor. flagd reads its flags from a JSON file, JavaScript Object Notation. The flag v2_enabled has two variants, on and off, and the default variant is on. The state ENABLED means that flagd serves it. Checkout builds a flagd provider in file mode, on flags.json, sets it on the OpenFeature API, and waits until it is ready. Then a client named checkout asks for the boolean v2_enabled. The second argument, false, is the default value. You cannot call it without one. Maven can run a class with exec java, but flagd compiles its targeting rules at run time, and that fails in Maven's class loader. So a small script compiles the project, writes the classpath to a file, and starts java with it. New flow, v2. The value came from the file, through the provider. Now I change the flag, and not the code: sed switched the default variant to off. Old flow, from the same compiled code. In his Udienza interview, Simon describes OpenFeature as a specification and standard for feature flagging: changing services at runtime without a code deployment. So a flag is production configuration with a lifecycle. Simon opened his talk with a trading firm that lost half a billion US dollars in one hour, because of a flag it repurposed: an old flag name now switched on new code, and one server still ran the old code. His source was Statsig's write-up about Knight Capital, in 2012. How long a flag should live depends on its kind. Pete Hodgson's classification has four. Release flags live for days or weeks. Ops flags, the kill switches, can live for years. Experiment flags change with each request, and permission flags are long-lived, per user. v2_enabled is a release flag, and it lives in two places: the code that evaluates it, and the file that serves it. Once the new flow is out, both go. A flag you treat as a throwaway if stays in the code long after you forget it. In this lesson you learned that a flag is production configuration with a lifecycle, that the OpenFeature SDK asks a provider and always takes a default value, and that release, ops, experiment and permission flags live for very different times.