Skip to content

Add REDIRECT control frame to defer affinity routing to sender #1753

Closed

Description

@jfallows

Overview

Introduce a new engine-level control frame REDIRECT that allows a consumer binding to instruct its parent (producer) binding to abort the current stream and reissue it through the affinity resolver with a different hash key. This pushes protocol-specific affinity extraction out of generic transport bindings (e.g. binding-http server) and into the protocol-aware binding (e.g. binding-mcp server) that actually understands the routing key.

Motivation

Today, HttpAffinityConfig requires HTTP server binding configuration to express protocol-specific extraction rules — e.g. extract mcp-session-id from a header, run regex [^\.]+ over a state query parameter. This bakes downstream protocol knowledge into the upstream transport binding, and grows linearly with the number of session-bearing protocols layered on HTTP.

The protocol-aware binding (binding-mcp server, WebSocket session bindings, etc.) is the natural place for this knowledge — it already parses the protocol and knows which fields constitute the affinity key. But by the time the protocol-aware binding receives a stream, the engine has already selected a worker via the upstream binding's affinity hash. Moving affinity extraction "down" requires a way to revisit the routing decision after BEGIN inspection.

REDIRECT is that primitive.

Frame shape

REDIRECT flows upstream (consumer → producer), same direction as WINDOW, RESET, and CHALLENGE. It closes the original stream like RESET. Fields:

REDIRECT
  ├── streamId, sequence, acknowledge, maximum   # same as BEGIN
  ├── traceId, authorization                     # carry-through
  ├── affinity                                   # original BEGIN's affinity
  ├── timestamp
  ├── hash : DirectBuffer                        # the new affinity key bytes
  └── extension : OctetsFW                       # round-tripped from BEGIN

The round-tripped extension means the parent doesn't need to buffer BEGIN's extension data while awaiting WINDOW — the consumer returns it via REDIRECT if it decides to redirect.

Timing rule

REDIRECT is mutually exclusive with WINDOW for the first response from the consumer:

producer                                     consumer
  BEGIN ──────────────────────────────────────►
                                                inspects extension
                                                decides
  ◄────────────────────────────── WINDOW   (commit, normal flow)
       OR
  ◄──────────────────────────── REDIRECT   (abort, reissue)

Once the consumer issues WINDOW, the producer is permitted to send DATA. Allowing REDIRECT after that would lose in-flight bytes. Mutual exclusion makes the contract crisp.

For protocols where the consumer needs DATA before deciding, future extensions could relax this with explicit reissue semantics — out of scope for v1.

Parent handling

parent.onRedirect(redirect):
  closeStream(redirect.streamId)            // RESET-equivalent semantics
  beginEx = redirect.extension
  hashKey = redirect.hash
  consumer = affinityResolver.resolve(routedBindingId, hashKey)
  newStreamId = supplyInitialId(...)
  consumer.accept(BEGIN { ext = beginEx, traceId, authorization, ... })

AffinityResolver is the existing/proposed pluggable SPI; for local resolution it picks a worker, for clustered routing it returns a remote consumer. REDIRECT does not depend on clustering, but composes with it.

Constraints

  • Loop bound. At most one REDIRECT per request lineage. Enforced engine-side via a per-trace counter, or by convention. For MCP specifically the second BEGIN lands on the right worker and resolves locally with no further redirect.
  • Capability discovery. Parent bindings opt in to handling REDIRECT. Emitting REDIRECT against a non-supporting parent yields RESET and an operator config error — bindings that emit REDIRECT should declare a parent that supports it.
  • Trace continuity. The reissued BEGIN preserves traceId. Observability sees one logical request crossing a worker boundary, not two.
  • Authorization carry-through. authorization is on REDIRECT and forwarded onto the new BEGIN. No re-auth.

Beneficiaries beyond MCP

This is a generic primitive. Any binding with protocol-specific session affinity sitting behind a generic transport can use it:

  • WebSocket-over-HTTP with session-shaped routing
  • MQTT clientId-based affinity
  • gRPC with session metadata
  • MCP (motivating use case)

In each case, the protocol-aware binding extracts the affinity key after seeing BEGIN; the upstream HTTP/TCP/etc. binding stays generic. HttpAffinityConfig's protocol-shaped extraction options become optional once consumers can drive their own affinity via REDIRECT.

k3po test support

The zilla engine's k3po-zilla transport needs REDIRECT support so test scripts can exercise the new frame. This change lives in zilla engine's test scope, not in aklivity/k3po.

DSL syntax mirrors the existing advisory verbs for upstream-flowing frames (e.g. challenge / challenged):

# consumer-side script (reading data stream, advising producer)
read advise zilla:redirect
  hash "abc123xyz"
  ext ${zilla:beginExHttp(...)}

# producer-side script (writing data stream, being advised by consumer)
write advised zilla:redirect
  hash "abc123xyz"
  ext ${zilla:beginExHttp(...)}

Implementation surface

  • New frame type in the engine's primitive frame ABI: REDIRECT.
  • Flyweight builder/decoder + RW types alongside existing BeginFW / WindowFW / ResetFW / ChallengeFW.
  • Parent-side handler hook on bindings that opt in (binding-http server is the immediate consumer for the MCP use case).
  • Small addition to AffinityResolver callers to accept an external hash key (vs. extracting from BEGIN).
  • k3po-zilla transport extensions: read advise zilla:redirect and write advised zilla:redirect with hash and ext fields, mirroring the challenge / challenged advisory shapes.
  • No SPI changes.
  • No schema changes to existing bindings.

Relationship to other work

Emerged from cluster state management discussions around #1739 (MCP elicitation across clustered zilla deployments). The cluster work needs an AffinityResolver SPI with cross-node routing; REDIRECT is the optimization that makes that SPI usable without leaking MCP-specific config into HTTP server binding.

Treat as a follow-on optimization to whatever lands for the cluster work — non-blocking, but unlocks cleaner layering once present.

Acceptance criteria

  • REDIRECT frame defined in engine ABI with the documented fields, including round-tripped extension.
  • binding-http server handles REDIRECT: closes original stream, reissues BEGIN with round-tripped extension and the supplied hash via the affinity resolver path.
  • AffinityResolver callers accept an external hash key from the REDIRECT path and route accordingly.
  • One REDIRECT-per-lineage enforcement (engine counter or documented convention).
  • traceId preserved across redirect; observability shows one logical request.
  • authorization preserved across redirect; no re-auth required.
  • binding-mcp server emits REDIRECT for misrouted requests (sessionId mismatch on POST /mcp, sessionId-prefix mismatch on state for callback path), allowing HttpAffinityConfig to drop MCP-specific extraction options.
  • k3po-zilla transport supports read advise zilla:redirect / write advised zilla:redirect with hash and ext fields.
  • Test: consumer emits REDIRECT in response to BEGIN; original stream is closed; reissued stream lands on the affinity-resolved worker with the correct extension.
  • Test: consumer emits REDIRECT against a non-supporting parent → RESET and a config-time error surfaced.

Activity

  1. changed the title [-]Add REDIRECT control frame to defer affinity routing to consumer binding[/-] [+]Add REDIRECT control frame to defer affinity routing to sender[/+] on May 8, 2026
  2. self-assigned this
    on May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions