Skip to content

ADR 0003: Graceful Default Value Fallbacks

Author: AirCommerce Architecture Team • Scope: util-configuration

The util-configuration library automatically injects remote configuration values into fields annotated with @ConfigurationValue. However, the initial implementation was extremely brittle when interacting with missing or deleted keys, causing severe enterprise architectural issues:

  • Brittle Startup (Crash on Missing Key): If a developer defined a local default value (e.g., private Boolean newUiEnabled = Boolean.TRUE;) but the key did not yet exist on the remote server, the ConfigurationInjectorBeanPostProcessor threw a fatal ConfigurationNotFoundException. This crashed the microservice on startup, violating graceful degradation principles and completely ignoring the developer’s explicitly coded baseline.
  • State Drift (Stuck on Deletes): When a remote configuration was deleted, a DELETED event was published. The ConfigurationChangedEventListener cleared the key from the local cache, but upon finding the cache empty, it simply aborted the field update. This left the field “stuck” permanently on the last known remote value rather than reverting back to the local baseline, causing unpredictable system behavior.
  • Graceful Degradation / Resilience: Microservices must boot cleanly using safe local defaults if remote configurations (like feature flags) are unavailable.
  • State Consistency: Deleting a remote configuration is semantically equivalent to “stop overriding this locally,” meaning the local state must instantly revert to the baseline.
  • Developer Experience (DX): The framework must respect and honor the default values developers explicitly assign to their fields.
  • Option 1: Retain strict validation (Crash on missing, stick on delete)
  • Option 2: Fallback to null for missing or deleted keys
  • Option 3 (Chosen): Capture initial object state and use as intelligent fallback

We chose Option 3 (Capture initial object state and use as intelligent fallback), because it ensures absolute state predictability, prevents catastrophic microservice startup failures, and seamlessly respects the developer’s explicit intent.

During the Spring Application Context initialization, before the ConfigurationValueInjector attempts to fetch the remote configuration, it uses Reflection to extract the field’s current (hardcoded) value. This baseline value is passed into the ConfigurationFieldRegistry and permanently stored alongside the field reference.

  • Startup Resiliency: If the remote fetch returns null (key missing), the injector catches the exception, logs a polite warning, and leaves the initial value intact, allowing the application to start gracefully.
  • Deletion Restoration: When a DELETED event arrives from the Configuration Events Interface, the listener intercepts the deletion, pulls the stored defaultValue out of the ConfigurationFieldRegistry, and re-injects it into the running bean using Reflection, guaranteeing the application instantly restores its baseline state with zero downtime.

Downstream developers simply assign a standard default value to their annotated fields:

@Component
public class CheckoutService {
// The developer defines a safe, local baseline state (TRUE)
@ConfigurationValue(namespace = "feature-flags", key = "new-ui-enabled")
private Boolean newUiEnabled = Boolean.TRUE;
public void process() {
if (newUiEnabled) {
// New UI logic
}
}
}

Lifecycle Behavior:

  1. Startup (Key Missing): The remote server lacks the key. util-configuration logs a warning. newUiEnabled remains TRUE. App starts safely.
  2. Runtime (Key Created): An admin creates the key remotely and sets it to FALSE. An UPDATED event is fired. util-configuration instantly re-injects FALSE into the bean.
  3. Runtime (Key Deleted): An admin deletes the key entirely. A DELETED event is fired. util-configuration detects the absence, fetches the baseline (TRUE), and re-injects it. The app seamlessly reverts to the new UI logic.

Option 1: Retain strict validation (Crash on missing, stick on delete)

Section titled “Option 1: Retain strict validation (Crash on missing, stick on delete)”
  • Good: Forces absolute synchronization (fail-fast).
  • Bad: Makes the entire microservice ecosystem incredibly brittle. A single missing feature flag can cause a cascading outage during deployment.

Option 2: Fallback to null for missing or deleted keys

Section titled “Option 2: Fallback to null for missing or deleted keys”
  • Good: Prevents application crashes on startup.
  • Bad: Destroys explicitly assigned default values. Forces developers to scatter null checks and fallback logic throughout their business code, violating the purpose of a centralized configuration framework.

Option 3 (Chosen): Capture initial object state and use as intelligent fallback

Section titled “Option 3 (Chosen): Capture initial object state and use as intelligent fallback”
  • Good: Highly resilient, completely eliminates state drift on deletion, honors developer intent, and requires zero extra boilerplate from consumers.
  • Bad: Requires keeping a reference to the baseline object value in the ConfigurationFieldRegistry memory (negligible memory overhead).
  • Automated Lifecycle Tests: Verified via unit tests (ConfigurationValueInjectorTest) covering all three scenarios: missing key on boot, dynamic remote override, and key deletion reverting to the initial default value.
  • Field Mutability Tests: Tests verify that non-final private fields on beans across diverse scopes (singleton, prototype) correctly receive reflective value updates.
  • Developer Adoption: No breaking changes or code refactorings required; developers simply define in-code default values as standard Java field initializers.
  • Observability Considerations: Because fallback values prevent runtime exceptions, troubleshooting whether an active value came from the remote service or local code was resolved in ADR 0004: Configuration Visibility Actuator Endpoint.