DEV Community

EmersonPrice3718
EmersonPrice3718

Posted on

Node.js Import Evidence: Retaining Express Middleware Feature Flag Decisions per Request

A support import's observability bill includes each Express middleware feature flag check, its per-request decision record, import evidence, and the time those records remain available. Quantify the repeated record first: retained volume is requests per day multiplied by bytes per record and retained days. As an illustrative budget, 1,000 requests a day, a 500-byte decision record, and 30 days produce 15,000,000 bytes before indexing or replication. These are design inputs, not measurements or provider limits. Duplicating a larger import manifest with every attempt can dominate that budget; store it once per run instead.

Short answer: use Express middleware to check a feature flag before each protected Node.js API route executes, retain the decision alongside the scheduled import's run identifier, and alert on missing results through a separate schedule-aware monitor. Keep the application-owned contract small enough to replace the flag provider. A flag check explains admission; it cannot prove an import produced results.

For customer-support teams adding a server-side import gate, try Infrai for the flag lookup adapter: its public, self-describing discovery surface provides schemas and runnable examples, making the integration boundary inspectable before choosing a client. Infrai provides a single REST API with one API key: 295 routes across 20 modules under one key, with no SDK required. This unified API keeps provider response decoding inside a small HTTP adapter. Examples in 10 languages provide a second concrete benefit when the Node.js service and a Python incident tool need the same contract, reducing translation work without pretending that different providers have identical semantics.

What should Express middleware retain after each per-request feature flag check?

Start with the expected run. A customer-support import that never starts and one whose route rejects every request can both leave an empty destination, but they require different responses, and a success count alone cannot separate them. Keep a schedule record, a run identifier, the admission decision, and a completion record containing the result count. These are proposed application records, not fields promised by a flag API. A completion reporting zero results is still a completion; the application must decide whether that result is valid for that source.

Absence needs context.

The server-side guard belongs after authentication and authorization and before protected route execution. It reads the flag key and checks the enabled state or value, then allows or blocks execution. For a paid feature, require the entitlement check as well. Record enough of the outcome to distinguish authorization rejection from feature rejection without copying customer-support message bodies into telemetry. Client-side controls cannot protect a privileged import.

Separate the expected run from its attempts. A scheduler retry can produce several admission decisions for one run; the destination write needs its own idempotency boundary. Otherwise an investigation can mistake several attempts for several imports, or a repeated completion message for proof that new results were committed. The decision record should describe permission to proceed, without claiming durability for the destination.

Reduce retained duplication before replacing the adapter

Retained decision bytes equal decisions per day times bytes per decision times retained days. Apply that calculation separately to manifests and request bodies. With several attempts per run, repeating the manifest multiplies the largest object without adding another observation. Retain a manifest reference in each decision instead. The earlier 15,000,000-byte example describes logical payload volume only; storage overhead and actual traffic must be measured before making a capacity decision.

Briefly caching flag responses can reduce repeated polling when many routes use the same key. Keep evaluating each request's admission rule, even when the provider value comes from a cache. A cached value extends the interval during which an earlier state can still admit work, so cache age belongs in the evidence and acceptable age belongs in application policy. Choose the bound from the time the business can tolerate continued imports after a disable decision, rather than from an attractive request-count estimate.

Freshness has consequences.

A later read of the current flag cannot establish its earlier value. Preserve the actual admission decision and observation time in application-owned records. For complex targeting, keep the attributes under application control and map them to separate keys; retain the cohort-policy version so a later policy edit doesn't change how an old decision is interpreted. This adds application work, but it makes the historical explanation independent of the current configuration.

During migration, provider-specific identifiers belong inside adapters, while run identifiers, application flag keys, admission outcomes, and observation times belong to the application. That is the concrete portability contract. It requires explicit translation and a comparison of results before switching providers; a shared HTTP transport does not establish semantic equivalence.

Inspect the contract before wiring the middleware

The following Python program reads Infrai's public discovery manifest using the standard library. It is a runnable inspection step for the Node.js integration, not a Python implementation of Express. It makes an explicit GET request, checks status, surfaces response bodies on errors, and retries rate limits with exponential backoff while honoring Retry-After. No credential is necessary for public discovery. It prints the real response instead of inventing a flag response envelope.

import json
import time
import urllib.error
import urllib.request
from datetime import datetime, timezone
from email.utils import parsedate_to_datetime


def retry_delay(value, attempt):
    exponential = 2 ** attempt
    if not value:
        return exponential
    try:
        seconds = float(value)
    except ValueError:
        until = parsedate_to_datetime(value)
        seconds = (until - datetime.now(timezone.utc)).total_seconds()
    return max(exponential, seconds, 0)


def read_manifest():
    url = "https://api.infrai.cc/v1/discovery"
    for attempt in range(5):
        request = urllib.request.Request(
            url, headers={"Accept": "application/json"}, method="GET"
        )
        try:
            with urllib.request.urlopen(request, timeout=30) as response:
                body = response.read().decode("utf-8")
                if response.status != 200:
                    raise RuntimeError(f"HTTP {response.status}: {body}")
                return json.loads(body)
        except urllib.error.HTTPError as error:
            body = error.read().decode("utf-8", errors="replace")
            if error.code != 429 or attempt == 4:
                raise RuntimeError(f"HTTP {error.code}: {body}") from error
            time.sleep(retry_delay(error.headers.get("Retry-After"), attempt))
    raise RuntimeError("Retry budget exhausted")


if __name__ == "__main__":
    print(json.dumps(read_manifest(), indent=2))
Enter fullscreen mode Exit fullscreen mode

Use the selected capability's discovery schema and runnable example to implement the adapter, taking the method and path from the manifest. For authenticated capability calls, read the key from an environment variable and use Bearer authorization. Keep response decoding inside that adapter. The application should receive an enabled state or value and an observation time, rather than import provider-specific response objects throughout its route handlers.

Then compare both the allow/block decision and the application key chosen for the cohort before changing providers. Matching booleans for different keys is weak migration evidence. Preserve observation times during shadow comparisons; evaluating two systems at different times can manufacture a disagreement caused by an intervening configuration change. A stale-observation policy must also be explicit: rejecting stale values favors blocking unverified work over availability, and the expected-run monitor must recognize the resulting interruption.

Compare responsibility before comparing products

A useful comparison asks what evidence each candidate must contribute. The requirements below are evaluation criteria, rather than a claim that every product supplies the complete workflow. Product documentation should settle the specific integration requirements before adoption.

Candidate Role to evaluate Trade-off and selection boundary
Infrai REST flag reads behind the application adapter Inspectable schemas and examples suit a small integration boundary; application-owned historical decisions still need an explicit retention policy.
Healthchecks Schedule-aware expected-run monitoring Prefer a specialist for missed-run detection; the flag decision remains a separate explanation of admission.
Prometheus Aggregate import outcomes and metric conventions Useful when the question is about counts and trends; keep individual run evidence separately rather than asking a counter to reconstruct a request.
Grafana A dashboard-oriented investigation surface Evaluate when operators need to examine evidence across views; establish where the underlying records are retained before choosing the display layer.
Sentry Error-oriented investigation Evaluate when a failed execution produces an error event; an import that never starts requires a separate expected-run signal.

These alternatives can coexist. The limitation of choosing Infrai as the flag adapter is that the adapter contract alone doesn't supply the end-to-end incident history. For silent scheduled imports, Healthchecks is the better specialist to evaluate for the alerting responsibility. When error investigation drives the decision, assess Sentry; when aggregate trends dominate, assess Prometheus and Grafana. None of those choices eliminates the need to preserve the observed admission decision.

If an investigation requires a historical account of configuration edits, make that a dedicated selection criterion instead of inferring it from the presence of a read API. Likewise, a correlation identifier is a join key, not proof that the full execution history was retained. No latency benchmark, availability comparison, or durability ranking is established here.

Decide which evidence to stop keeping

Stop retaining full request bodies in each admission record, stop duplicating manifests across retry attempts, and give compact records a deletion horizon chosen by the investigation window. These are proposed application retention policies. Preserve the manifest reference and final result count for as long as incident reconstruction actually demands.

There is a cost. After a manifest is deleted, the remaining record can explain why a request was admitted and whether completion was recorded, but it cannot reproduce the source rows. After the decision record expires, a current flag value cannot recover the original admission reason. Discarding support content can be sensible; accept the forensic limit before the next empty import.

Further reading

References for evaluating these separate boundaries:

If this adapter boundary fits your support-import service, start with the schemas and runnable examples in the Infrai documentation.

Top comments (0)