Download Game! Currently 83 players and visitors. Last logged in:WilliLolzoDornierEwige

Blitzer's Blog >> 71360

Back to blogs index
Posted: 10 Jul 2026 17:59 [ permalink ]
This is brilliant. What you've just described is the exact architectural
divide between a **Control Plane** and a **Data Plane**.

This is the holy grail of scalable systems. It's how WebRTC manages video
calls (Signaling Servers vs. TURN Relays), how Discord scales voice chat, and
how modern MMOs orchestrate spatial game worlds.

By keeping FastAPI completely out of the byte-shoveling business, you allow it
to do what it does best: asynchronous I/O, routing, and state orchestration.
Meanwhile, your relay workers can be highly optimized, bare-metal loopers
dedicated entirely to blasting UDP/TCP packets.

Here is a breakdown of why this makes sense, along with architectural
enhancements to take it to the next level.

### 1. The Control Plane (FastAPI)

Your logic for the FastAPI server is spot on. It acts as the "Directory" and
the "Matchmaker."

* **Provider Registration:** Providers hit a `/register` endpoint, dropping a
JSON payload of their capabilities and format.
* **Consumer Matching:** Consumers hit `/subscribe/{NAME_ID}`.
* **Orchestration:** FastAPI asks the `lxc-manager-api` (or a local process
manager) to spin up a worker, reserves a port in the `15000-16000` range, and
hands the IP:PORT tuple back to both parties.

**Enhancement: The Stateless Ledger**
Because FastAPI uses an asynchronous event loop, running CPU-heavy or
state-heavy tasks in the main thread blocks concurrency. While keeping
subscriptions in FastAPI's RAM works for a single instance, if you ever scale
to two FastAPI servers, they won't share that memory.

* **Recommendation:** Use a lightweight, blazing-fast in-memory store like
**Redis** (or even a shared SQLite in WAL mode on your MeshFS) as the actual
ledger. FastAPI simply reads/writes to Redis. This keeps FastAPI completely
stateless.

### 2. The Data Plane (Stream Relay Workers)

You mentioned launching workers locally utilizing ports 15000-16000. This is a
great starting point, but we need to design the workers to be ruthless
packet-pushers.

**Enhancement: UDP Multiplexing (Avoiding the Port Exhaustion Trap)**
If you assign one port per stream, you are strictly capped at 1,000 active
streams. In a massive mesh, you'll eat through those ports instantly.

* **Recommendation:** Instead of spawning 1,000 workers on 1,000 ports, spawn
**one** highly optimized worker (e.g., written in C, Rust, or utilizing Node's
raw UDP sockets) listening on port `15000`.
* Require providers to prefix their raw binary data with a **4-byte Stream
ID**.
* The worker reads the first 4 bytes, looks up the Stream ID in its local
routing table, and instantly blasts the rest of the payload to the IPs of the
subscribed consumers. This allows a single port to handle $65,536$ unique
streams concurrently.

### 3. The Lifecycle & "The Reaper"

The hardest part of this architecture is dealing with ghosts: providers that
crash without deregistering, or consumers that drop their connection.

**Enhancement: Ephemeral Heartbeats**
FastAPI shouldn't just "do its best" to keep tabs; it should enforce a strict
"Dead Man's Switch."

* **For Providers:** When FastAPI gives a provider a port, the provider must
ping the FastAPI server's `/heartbeat/{NAME_ID}` every 5 seconds. If FastAPI
doesn't hear from them, it assumes they died, deletes them from the ledger,
and commands the relay worker to tear down the stream.
* **For Consumers:** The relay worker itself should monitor consumer activity.
If a consumer stops acknowledging packets or drops its TCP socket/UDP
keep-alive, the worker drops them from the routing table and reports the stat
back to FastAPI.

### Summary of the Flow

1. **Provider:** "Hey FastAPI, I have a new VGA frame stream."
2. **FastAPI:** "Great. I've allocated Stream ID `0x0A` on Relay Worker
`hauki-crm:15000`. Send your bytes there."
3. **Consumer:** "Hey FastAPI, I want to watch that VGA stream."
4. **FastAPI:** "Listen to `hauki-crm:15000` and filter for Stream ID `0x0A`."
5. **Relay Worker:** Blindly and ruthlessly takes packets with `0x0A` and fans
them out to all known consumer IPs.

This setup isolates your business logic from your data firehose. You can
restart the FastAPI server, update the schema, or deploy new code without
dropping a single frame of the actual streams passing through the data plane!

---

[FastAPI Internals - How does it work?](https://www.youtube.com/watch?v=nYAMtzA
bNN8)
This video dives deep into the internal async mechanics of FastAPI, which is
critical for understanding how to structure your control plane for maximum
concurrency without blocking the event loop.