Skip to content

PATTERN Cited by 1 source

Socket routing through worker chain

A composable pattern where an inbound TCP socket is passed through a chain of compute primitives — Worker → Worker, Worker → Durable Object, or Durable Object → Container — giving full programmatic control over where and how a non-HTTP connection is routed.

Problem

Traditional L4 proxies route connections based on static rules (IP, port, SNI). When the routing decision requires application-level logic (tenant identification, protocol inspection, load balancing, or state-aware placement), the proxy needs to be programmable.

Solution

The ingress proxy (e.g. Spectrum) delivers the socket to a Worker; the Worker inspects headers, performs authentication, and forwards the socket to the appropriate destination:

Spectrum → Worker [connect(socket)]
               ↓ stub.connect("host:port")
           Durable Object [connect(socket)]
               ↓ ctx.container!.getTcpPort(8080).connect(...)
           Container (any language/protocol)

Each hop uses bidirectional pipeTo() to forward bytes in both directions simultaneously.

Properties

  • Full-duplex at every hop: both directions stream concurrently
  • Stateful routing: a Durable Object can route based on per-connection state (e.g. session affinity, tenant isolation)
  • Language-agnostic terminal: the Container at the end runs any TCP server in any language
  • Composable: chain length is flexible — skip the DO for simple routing, add multiple Workers for multi-stage inspection

Seen in

Last updated · 608 distilled / 1,858 read