The problem
A feature flag service is exactly the kind of shared platform capability where the real risk
isn't writing the service -- it's whether every other team that depends on it can trust it
without becoming the reason it breaks. I built one with that lens: a Spring Boot REST service, a
client library other services would import, API-key security, and request-level observability.
What it enforces, and how
- Atomic flag storage: the full flag set lives behind a single
AtomicReference, so a bulk refresh swaps the whole map in one operation -- readers either see the old map in full or the new one in full, never a half-updated state. Proven with a 50-reader concurrency test running against a background thread that refreshes continuously. - Deterministic percentage rollout: the same user lands in the same 0-99 bucket on every call, so a 30% rollout is a stable cohort, not a coin flip per request.
- A resilient client library: a fresh value comes from local cache without a network call, a stale cached value is served if the flag service is unreachable, and only a key that's never been fetched falls through to the caller's own default.
- API-key security, two roles: a read-only key can evaluate flags; only an admin key can create or change them.
- Observability: every request gets a trace ID (generated or propagated), echoed back in the response header and attached to every structured JSON log line for that request.
The real bug the test suite caught
The first version of the rollout bucketing picked a fresh random number on every call:
private int bucketFor(String flagKey, String userId) { return new java.util.Random().nextInt(100); } That looks fine in a quick manual check -- ask for a 50% rollout, get roughly half true, half
false. What it actually does is turn every evaluation into an independent coin flip for the same
user: someone could see a feature enabled on one page load and disabled on a refresh of the exact
same page, with no deploy and no config change in between. The kind of "why does this flicker"
bug report that's maddening to track down precisely because nothing in the system actually
changed between the two requests.
@Test void rolloutIsConsistentForTheSameUserAcrossManyCalls() { repository.upsert(new FlagDefinition("new-checkout", true, 50, Map.of())); boolean first = evaluator.evaluate("new-checkout", "tenant-a", "user-42"); for (int i = 0; i < 100; i++) { assertThat(evaluator.evaluate("new-checkout", "tenant-a", "user-42")) .isEqualTo(first); // failed against the random version } } The fix replaces the random draw with a deterministic hash of (flagKey, userId), so the same
user always lands in the same bucket for that flag.
Stack
Java 17, Spring Boot 3, Spring Security, Micrometer/Prometheus, structured JSON logging via
Logstash Logback Encoder. 17 tests covering evaluator correctness, repository concurrency, client
resilience, and the full request flow through the controller. CI runs the exact same mvn test
invocation as local.

