ADR 0003: Graceful Default Value Fallbacks
Author: AirCommerce Architecture Team • Scope: util-configuration
1. Context and Problem Statement
Section titled “1. Context and Problem Statement”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, theConfigurationInjectorBeanPostProcessorthrew a fatalConfigurationNotFoundException. 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
DELETEDevent was published. TheConfigurationChangedEventListenercleared 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.
2. Decision Drivers
Section titled “2. Decision Drivers”- 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.
3. Considered Options
Section titled “3. Considered Options”- Option 1: Retain strict validation (Crash on missing, stick on delete)
- Option 2: Fallback to
nullfor missing or deleted keys - Option 3 (Chosen): Capture initial object state and use as intelligent fallback
4. Decision Outcome
Section titled “4. Decision Outcome”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.
4.1. Implementation Mechanics
Section titled “4.1. Implementation Mechanics”Capturing the Baseline State
Section titled “Capturing the Baseline State”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.
Graceful Injection & Restoration
Section titled “Graceful Injection & Restoration”- 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
DELETEDevent arrives from the Configuration Events Interface, the listener intercepts the deletion, pulls the storeddefaultValueout of theConfigurationFieldRegistry, and re-injects it into the running bean using Reflection, guaranteeing the application instantly restores its baseline state with zero downtime.
Usage Pattern
Section titled “Usage Pattern”Downstream developers simply assign a standard default value to their annotated fields:
@Componentpublic 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:
- Startup (Key Missing): The remote server lacks the key.
util-configurationlogs a warning.newUiEnabledremainsTRUE. App starts safely. - Runtime (Key Created): An admin creates the key remotely and sets it to
FALSE. AnUPDATEDevent is fired.util-configurationinstantly re-injectsFALSEinto the bean. - Runtime (Key Deleted): An admin deletes the key entirely. A
DELETEDevent is fired.util-configurationdetects the absence, fetches the baseline (TRUE), and re-injects it. The app seamlessly reverts to the new UI logic.
5. Pros and Cons of the Options
Section titled “5. Pros and Cons of the Options”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
nullchecks 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
ConfigurationFieldRegistrymemory (negligible memory overhead).
6. Validation and Verification Strategy
Section titled “6. Validation and Verification Strategy”- 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.
7. Migration and Rollout Consequences
Section titled “7. Migration and Rollout Consequences”- 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.
8. Links and Traceability
Section titled “8. Links and Traceability”- Configuration Overview
- Configuration Service
- Configuration Events Interface
one.aircommerce.util.configuration.injector.ConfigurationValueInjectorone.aircommerce.util.configuration.registry.ConfigurationFieldRegistryone.aircommerce.util.configuration.listener.ConfigurationChangedEventListener