Skip to content

ADR 0010: the shape of broadcast - #12

Merged
teceer merged 1 commit into
mainfrom
docs/adr-0010-broadcast-shape
Aug 7, 2026
Merged

teceer merged 1 commit into
mainfrom
docs/adr-0010-broadcast-shape

Conversation

@teceer

@teceer teceer commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Docs only. Numbered 0010 as requested — 0008 and 0009 are unused.

Why now

broadcast has been in the service map since the beginning and is still a mix.exs and a .gitkeep. Its decisions live in services/broadcast/CLAUDE.md — which is where idioms belong and where architecture does not.

More to the point, the problem it exists to solve is already in the code. gateway.go:101 opens one WatchDeltas upstream per connected browser:

upstream, err := s.sim.WatchDeltas(ctx, ...)   // one per connected browser

So the simulation's fan-out is already O(clients), at the one process that must not be slowed down by any of them. And the gateway cannot spread that load: replicaCount: 1 is load-bearing until its economy loop is idempotent (ADR 0005). Writing this down before there is code is the cheapest it will ever be.

The three decisions that carry the rest

Fan in once per node, fan out locally. One StreamWorld upstream per broadcast node, every browser on that node served from it. This is the entire justification for the service existing — if each browser still cost sim-core a stream, an Elixir hop in the middle would be pure added latency.

Interest is a grid. One Channel topic per world cell; a client joins the cells its viewport covers plus a ring. Deliberately the dumbest version of interest management — no visibility graph, no per-entity subscriptions — because the world is 48×30 tiles and the point is to have the seam in place before it matters.

Deltas are droppable state, and resync goes through JoinWorld. This extends a posture already in the code rather than inventing one: grpc.rs:223 drops a lagging subscriber rather than slowing the tick loop, reasoning that a client which cannot keep up should resync from a snapshot. It also explains why WatchDeltasRequest.from_tick being ignored by the implementation is not a bug to fix here — with a snapshot endpoint and idempotent state, delta history buys little and costs retention everywhere.

Then four smaller ones: distributed Erlang between nodes rather than a fourth broker, binary protobuf rather than Phoenix's JSON default, bounded lossy backpressure (drop oldest, since newer state supersedes older), and presence deferred until something needs it.

The rejected option worth arguing about

Kafka to the browser. ADR 0002 makes Kafka the fact log, so consuming it here looks natural on a diagram. Rejected because the render path does not want facts — it wants the current state of a region at 10 Hz with the right to skip. Durability, per-partition ordering, replay and retention are all cost here and none is a benefit: a browser that missed two seconds does not want them, it wants now. The payloads are the wrong shape too — PipDied says what happened, a renderer needs where everything is.

That distinction (fact log vs state feed) is stated explicitly in the ADR, because the two are easy to conflate and pipsim now runs both on purpose.

Also recorded: why not serve WebSockets straight from the Go gateway (it would cap the connection fleet at one replica and couple "how many can watch" to "how many buildings can be driven"), and why not per-client interest instead of a grid (it makes every message a unicast and undoes the reason the fan-out node exists).

Honest costs, in Consequences

  • The client gains a second connection and a second protocol — Connect for commands, Channels for deltas. That is the strongest argument the "just use the gateway" option has.
  • broadcast needs a server-streaming Connect/gRPC client in Elixir, which the tavern deliberately avoided. 🔧 chore(broadcast): raise grpc past the RCE, and commit the lockfile #7 just moved the declared grpc dependency to 1.0 — this makes it the first real use of it in the repo rather than a lockfile entry.
  • mix test currently fails on Broadcast.Application is not available; the first commit of real code fixes it as a side effect.

Status

proposed. Five next steps, ordered so each leaves something runnable — starting with the supervision tree, which alone makes the test suite work.

broadcast has been in the service map since the beginning and is still a
mix.exs and a .gitkeep. Its decisions live in services/broadcast/CLAUDE.md,
which is where idioms belong and where architecture does not.

The thing it exists to prevent is already in the code: gateway.go opens one
WatchDeltas upstream per connected browser, so the simulation's fan-out is
O(clients) at the process that must not be slowed by any of them — and the
gateway cannot spread that load, because replicaCount: 1 is load-bearing
until its economy loop is idempotent (ADR 0005).

Seven decisions, the first three load-bearing:

- Fan in once per node, fan out locally. If each browser still cost sim-core
  a stream, an Elixir hop would be pure added latency.
- Interest is a grid of Channel topics; a client joins the cells it can see.
  Deliberately the dumbest version — the point is to have the seam before it
  matters.
- Deltas are droppable state, and resync goes through JoinWorld rather than
  replayed history. This extends the posture already in grpc.rs, where a
  lagging subscriber is dropped rather than allowed to slow the tick loop,
  and explains why the ignored from_tick is not a bug to fix here.

Plus: distributed Erlang between nodes rather than a fourth broker, binary
protobuf rather than Phoenix's JSON default, bounded lossy backpressure, and
presence deferred until a feature needs it.

Records why not Kafka to the browser — a renderer wants the current state of
a region with the right to skip, not durable ordered facts, and pipsim now
has both kinds of feed on purpose.
@teceer
teceer merged commit 07b2b02 into main Aug 7, 2026
2 of 7 checks passed
@teceer
teceer deleted the docs/adr-0010-broadcast-shape branch August 7, 2026 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant