DEV Community

CelthyrDusk7341
CelthyrDusk7341

Posted on

How to Gate Shopping Agents — 3 Feature Flag Rollback Checks

A rollback switch must stop new shopping-agent work before another iteration starts. TL;DR: check whether the feature flag is enabled with an authenticated GET, use the returned boolean at the next iteration boundary, and retain latency and cost receipts for work already admitted. A disable stops admission; it does not reverse a purchase or erase an earlier model call.

The common request is a Node.js API example for checking if a feature flag is enabled. This article uses Go for the runnable implementation, but the operation is the same HTTP read: supply the flag key, authenticate, validate the boolean response, and decide whether the next iteration may begin.

Consider a bounded e-commerce production rehearsal. I would test 200 shopping sessions, at most 4 model calls per iteration, and a proposed 2-second refresh interval, with checkout writes outside the experiment. These are hypothetical planning inputs, not observed traffic or benchmark results. The question is what happens when an operator disables recommendations while previously admitted work is still running.

Stop new work.

The lesson to test is an invariant: every new iteration passes through an admission decision, and every admitted iteration retains its receipts even if it completes after disable. Checking once at session creation cannot enforce that boundary.

What does an enabled feature flag authorize?

Name the reversible unit before writing the client. Here, true authorizes one recommendation iteration. It does not authorize a payment, inventory mutation, or an indefinite chain of downstream jobs. Otherwise the rollback dashboard can report a successful disable while business effects continue, and the team discovers that its supposedly small release-control mechanism has acquired transaction-recovery responsibilities.

Use three checks: the decision is fresh enough, the iteration actually passes through the gate, and completed work remains attributable after the gate closes.

For this rehearsal, propose an application SLO of stopping admission within 5 seconds of an effective disable. That is an acceptance criterion, not a platform guarantee. Polling interval, request duration, scheduling delay, and admission placement all consume that budget. A call admitted before disable may finish afterward without violating an admission SLO; counting it as newly admitted would confuse two different events.

Capacity planning changes the implementation. With the assumed 200 sessions, polling independently every 2 seconds creates 100 reads per second before retries. Sharing a decision per process changes that load according to process count and flag scope, but it is appropriate only when those callers are entitled to share the same decision. Don't merge tenant-specific decisions to improve a capacity estimate: the capacity benefit is real only if the authorization boundary survives it.

Step 1: bound the read before trusting the result

Save this program as flagcheck.go and run it with go run flagcheck.go. Set FLAG_API_BASE to the documented HTTPS API origin, INFRAI_API_KEY to your key, and FLAG_KEY to the shopping iteration flag. Set FLAG_BOOL_POINTER to the response's documented JSON Pointer, or leave it empty if the response itself is a boolean. Use the response schema to choose the pointer; don't guess a field named enabled.

The pointer traversal below supports object fields. If the selected response schema requires traversing an array, use a complete JSON Pointer implementation instead. A scalar false is a valid decision; malformed JSON, a missing field, and an authentication error are different outcomes.

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "net/url"
    "os"
    "strconv"
    "strings"
    "time"
)

func check(ctx context.Context) (bool, error) {
    base, err := url.Parse(os.Getenv("FLAG_API_BASE"))
    if err != nil || base == nil || base.Scheme != "https" || base.Host == "" ||
        base.User != nil || base.RawQuery != "" || base.Fragment != "" ||
        (base.Path != "" && base.Path != "/") {
        return false, fmt.Errorf("FLAG_API_BASE must be an HTTPS origin")
    }
    key, token := os.Getenv("FLAG_KEY"), os.Getenv("INFRAI_API_KEY")
    if key == "" || token == "" {
        return false, fmt.Errorf("FLAG_KEY and INFRAI_API_KEY are required")
    }
    path := strings.Replace("/v1/flags/is_enabled/{key}", "{key}", url.PathEscape(key), 1)
    endpoint := strings.TrimRight(base.String(), "/") + path
    client := &http.Client{
        CheckRedirect: func(req *http.Request, via []*http.Request) error {
            return http.ErrUseLastResponse
        },
    }
    for attempt := 0; attempt < 3; attempt++ {
        req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
        if err != nil {
            return false, err
        }
        req.Header.Set("Authorization", "Bearer "+token)
        resp, err := client.Do(req)
        if err != nil {
            return false, err
        }
        body, readErr := io.ReadAll(io.LimitReader(resp.Body, 65537))
        resp.Body.Close()
        if readErr != nil {
            return false, readErr
        }
        if len(body) > 65536 {
            return false, fmt.Errorf("response exceeds local size limit")
        }
        if resp.StatusCode == http.StatusTooManyRequests && attempt < 2 {
            delay := time.Duration(1<<attempt) * 100 * time.Millisecond
            retry := resp.Header.Get("Retry-After")
            if seconds, err := strconv.ParseInt(retry, 10, 32); err == nil && seconds >= 0 {
                if candidate := time.Duration(seconds) * time.Second; candidate > delay {
                    delay = candidate
                }
            } else if date, err := http.ParseTime(retry); err == nil {
                if candidate := time.Until(date); candidate > delay {
                    delay = candidate
                }
            }
            timer := time.NewTimer(delay)
            select {
            case <-ctx.Done():
                timer.Stop()
                return false, ctx.Err()
            case <-timer.C:
            }
            continue
        }
        if resp.StatusCode < 200 || resp.StatusCode >= 300 {
            return false, fmt.Errorf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
        }
        var value any
        if err := json.Unmarshal(body, &value); err != nil {
            return false, err
        }
        pointer := os.Getenv("FLAG_BOOL_POINTER")
        if pointer != "" {
            if !strings.HasPrefix(pointer, "/") {
                return false, fmt.Errorf("JSON Pointer must start with /")
            }
            for _, part := range strings.Split(pointer[1:], "/") {
                part = strings.ReplaceAll(strings.ReplaceAll(part, "~1", "/"), "~0", "~")
                object, ok := value.(map[string]any)
                if !ok {
                    return false, fmt.Errorf("pointer requires an object")
                }
                var exists bool
                value, exists = object[part]
                if !exists {
                    return false, fmt.Errorf("missing pointer segment %q", part)
                }
            }
        }
        enabled, ok := value.(bool)
        if !ok {
            return false, fmt.Errorf("selected response value is not a boolean")
        }
        return enabled, nil
    }
    return false, fmt.Errorf("retry budget exhausted")
}

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Millisecond)
    defer cancel()
    start := time.Now()
    enabled, err := check(ctx)
    fmt.Fprintf(os.Stderr, "flag_lookup_ms=%.3f\n",
        float64(time.Since(start).Microseconds())/1000)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    fmt.Println(enabled)
}
Enter fullscreen mode Exit fullscreen mode

The 1,200-millisecond deadline and 64 KiB ceiling are local choices. A Retry-After value can exceed the remaining deadline; the client then stops waiting without granting permission. This example keeps an optional iteration closed after a read error, while retaining the error separately from an operator's explicit false decision. That policy needs a different justification for a critical checkout operation.

Only a successful exit together with true admits another iteration. False closes the gate.

The program performs one read, not a continuous polling controller. Invoke the same decision logic at the application's iteration boundary, and apply the chosen freshness budget if you introduce caching. A process that keeps using yesterday's true value has not demonstrated rollback safety just because its HTTP client handles timeouts correctly.

Step 2: preserve receipts after admission closes

Flag lookup latency belongs to the control path. An iteration's elapsed time and a model call's reported latency are separate measurements, and adding them as though they were disjoint durations can double-count an interval already contained inside the iteration. Keep their names and boundaries explicit.

The next executable is an offline accounting check. Save it separately as receipts.go and run it with go run receipts.go. Its numbers are synthetic fixtures, not API response fields, observed latency, or vendor prices.

package main

import "fmt"

type Receipt struct {
    Iteration string
    Admitted bool
    FinishedAfterDisable bool
    ElapsedMS int64
    CostKnown bool
    CostMicrousd int64
}

func main() {
    receipts := []Receipt{
        {"shopping-001", true, false, 900, true, 2000},
        {"shopping-002", true, true, 1400, true, 3000},
        {"shopping-003", false, false, 0, false, 0},
    }
    var completed, afterDisable, missing int
    var costMicrousd int64
    for _, receipt := range receipts {
        if !receipt.Admitted {
            if receipt.ElapsedMS != 0 || receipt.CostMicrousd != 0 {
                panic("rejected iteration contains work")
            }
            continue
        }
        completed++
        if receipt.FinishedAfterDisable {
            afterDisable++
        }
        if receipt.CostKnown {
            costMicrousd += receipt.CostMicrousd
        } else {
            missing++
        }
        fmt.Printf("iteration=%s elapsed_ms=%d\n",
            receipt.Iteration, receipt.ElapsedMS)
    }
    fmt.Printf("completed=%d after_disable=%d missing_cost=%d cost_microusd=%d\n",
        completed, afterDisable, missing, costMicrousd)
}
Enter fullscreen mode Exit fullscreen mode

Expect 2 completed iterations, 1 completion after disable, no missing costs, and 5,000 microdollars in these invented receipts. The integer unit makes the arithmetic unambiguous. A production adapter must define how it converts reported decimal costs and preserve the original call identifier and attribution.

Missing stays missing.

Don't replace unknown cost with zero or discard an admitted iteration because its flag changed later. Track accounting coverage alongside the latency SLO. Otherwise a rollback mechanically lowers work volume, and a cheaper-looking total becomes a misleading claim about per-iteration efficiency.

Step 3: compare what to buy and what to build

The choice starts with rollback evidence, not a logo. A feature flag decides admission; a telemetry system helps explain decisions and completed work. OpenTelemetry's logs model is useful for correlation, but a trace identifier in a log does not by itself establish a complete execution tree.

This buy-vs-build table is a trial rubric. It identifies the evidence to request rather than promising identical behavior across products or deployment modes.

Option Buy-vs-build decision Boundary to test
LaunchDarkly Consider buying a dedicated flag platform when flag operations dominate the roadmap. Verify update propagation and fallback behavior for the selected client before adopting the 5-second application SLO.
Unleash Evaluate this flag-management alternative when deployment ownership belongs in the selection criteria. Compare the operating responsibility of each deployment option; include upgrades and on-call work in the trial.
Flagsmith Include this alternative when comparing application configuration with flag control. Check the chosen evaluation mode and environment boundaries against the same admission rehearsal.
Sentry Include this candidate when the buying decision centers on explaining application errors after rollback. Evaluate error investigation separately from flag admission; neither the flag read nor this example supplies crash analysis.
Datadog Include this candidate when the buying decision spans the platform team's operational monitoring. Trial investigation and notification requirements separately; monitor coverage does not establish the admission SLO.
Grafana Include this candidate when the buying decision centers on inspecting latency and accounting signals. Evaluate the chosen telemetry backends and operating responsibility; a dashboard alone cannot stop agent work.
A shared backend API Consider consolidation when the agent needs both release control and per-call accounting. Require a verified boolean contract and preserve application-owned admission receipts.
An application-owned gate Build the admission boundary and receipt handling in the agent regardless of provider. Budget for concurrency, decision freshness, and accounting coverage; a boolean cannot implement these policies by itself.

Infrai combines a self-describing REST API with one key and one bill across 295 routes and 20 modules. A single key reduces credential provisioning for this shopping workflow, while unified billing reduces invoice reconciliation. Public discovery exposes request and response schemas plus runnable examples, so wiring the flag check starts with reading one capability contract rather than learning a new SDK. Its specified per-call cost, latency, and request identifier metadata also supplies inputs for the receipt adapter, while the application still owns iteration attribution.

Consolidating those chores can simplify operational ownership without changing the requirement to attribute each iteration's cost.

Those are integration advantages. They aren't evidence of a faster rollback, a latency benchmark, or a replacement for a dedicated flag-management evaluation. The trade-off is concentrated credential responsibility: one API key reduces integration work, but its handling deserves a platform-level access review.

The comparison should therefore keep operational evidence separate from integration convenience: test each candidate against the same admission boundary, then decide how much flag-management responsibility the platform team intends to buy. Consolidating credentials also concentrates access-management responsibility, so the rollout review should examine credential handling alongside the SDK or REST integration effort. Neither the table nor the sample establishes an update-propagation guarantee.

Where does this rollback advice stop applying?

The approach fits optional recommendation work whose next iteration can be refused. Its limitation is that it is not suitable for payment compensation, inventory reconciliation, or cancellation of already committed work. If the requirement is to interrupt in-flight operations, define a separate cancellation protocol and prove its business semantics before calling the feature flag a rollback mechanism.

Keep polling, decision records, and latency accounting in application-owned instrumentation. For evaluation history, dependency policy, notification delivery, distributed trace inspection, crash analysis, session replay, silent-job monitoring, or retention and user-deletion controls, evaluate dedicated systems against explicit requirements; none of those capabilities follows from a boolean flag read. This example promises only the admission decision and its local error handling.

For the rehearsal, disable the flag between two iterations and check three outcomes: no new iteration starts beyond the proposed admission SLO, a permitted in-flight iteration remains accounted for, and a missing receipt remains visible as missing. The synthetic receipt program verifies arithmetic, while the live rehearsal must establish timing and concurrency behavior. Keep both checks; substituting one for the other is how a tidy dashboard conceals an untested rollback.

Sources

References for the comparison and implementation boundaries:

Top comments (0)