# Networks by design

> How Minekube networks are designed: three questions with three answers, labels and selectors as in Kubernetes, one routing question with data for answers, versions deployed to selectors, and primitives instead of policy: identity, the network boundary and verified facts.

> **Preview: Connect in the Minekube plugin:**
> This page describes networks in the Minekube plugin, the next generation of Connect. Today's Connect plugin and its `<endpoint>.play.minekube.net` addresses are described in the [Connect docs](https://minekube.com/guide/) and keep working. [Networks in the Minekube plugin](https://minekube.com/guide/networks/) compares the two.

A network should be developed the way a game is: Effect code against the real, generated API, `mk dev`, save, and the change applies while players stay connected. That covers commands like `/lobby`, routing between servers, queues, parties and what happens after a kick. And every server and proxy should be reachable for players, at home behind NAT too, without Minekube running anything per server.

This page describes the design. Each part says whether it is built or planned.

## Three questions, three answers

An early draft mixed three questions. They have different answers:

| Question | Answered by | Status |
| --- | --- | --- |
| How does a player get to a server? | The **connector**, the same in every endpoint, inside the platform's own process. See [Connector, relay and API](https://minekube.com/guide/networks/connectivity). | Built, not yet in production |
| Who holds the session when a server goes away? | Whoever is the proxy: Velocity today. Without a proxy, a waiting room in the relay. | Velocity built; waiting room planned |
| Where does programmable code run? | The **runtime of each platform**, with types from its real API. | Paper and Velocity built |

A Velocity network is then as reachable as a single Paper server; what differs is only which endpoint players enter through.

## Labels and selectors, as in Kubernetes

Every instance has **labels**, which say what it is, and a **status**, which says how it is doing.

- **Your labels** are free: `game=bedwars`, `stage=lobby`, `channel=canary`.
- **System labels** carry a prefix so nothing collides, and Minekube sets them: `minekube.com/platform=paper`, `minekube.com/version=26.3`.
- **The instance ID** is the key of its connector, not a label. It is for "exactly this instance".
- **Status** is reported continuously: players, free slots, ready.

You select with a `LabelSelector` as in Kubernetes: `matchLabels` and `matchExpressions` with `In`, `NotIn`, `Exists` and `DoesNotExist`, and the same text form for the CLI (`game=bedwars,mode in (solo,duo),!beta`). Among the matches, status decides, for example the instance with the most free slots. There is deliberately no shorthand without `matchLabels`, because a free label key could collide with it, and no annotations: what changes is status.

The same selectors pick everywhere: where a player goes, where a version goes, what a list shows. Velocity keeps its server names; each name resolves through a selector. This part is built: see [Labels and selectors](https://minekube.com/guide/networks/labels).

## One routing question, answers as data

Wherever the network does not know where a player should go (a join, a server gone, a kick, the waiting room), it is planned to ask **one** question, with the reason as a tagged union, and the answer is data:

| Answer | Means |
| --- | --- |
| `Route.To(selector)` | To a matching instance. Whether that happens inside the session (behind a proxy) or by a transfer is the system's concern. A reconnect is `To` the same instance. |
| `Route.Hold` | Hold the player until something happens, with a time limit and a next answer. |
| `Route.Kick(message)` | Disconnect, with a message. |

Because answers are data, a proxy can cache the answers a routing bundle gives for when it cannot reach it, and no separate rule language is needed. Today, proxy bundles in Velocity handle `/lobby` and kicks with Velocity's own events; see [Proxy bundles](https://minekube.com/games/write/proxy-bundles).

## Versions, deployed to selectors

A **version** is immutable, content-addressed code, and a **deployment** points a selector at it. Every matching instance converges to it, as desired state rather than a push, and an instance started later gets the current version of its selector by itself. Rolling back is deploying the previous version; undeploying ends a deployment. Built: `mk deploy`, `mk versions`, `mk deployments`, `mk rollback` and `mk undeploy`, see [Versions and deployments](https://minekube.com/games/versions-and-deployments/). A canary is `channel=canary` first, then a share, then everyone; shares, waves and updates a game holds until its round ends are planned. Our own features reach operators the same way: an opt-in channel is just a label.

## Primitives, not policy

Who may route whom where is not ours to decide. Each network writes its own rules as code; Minekube only guarantees what nobody can build alone:

1. **Identity.** Every instance has a key, and the registry says with authority which network it belongs to. Labels grant no rights.
   Every network is also an OpenID Connect issuer that publishes its members' keys, so a token an instance signs can be checked anywhere with standard tools, and servers can join with their environment's own token instead of a code. See [Connector, relay and API](https://minekube.com/guide/networks/connectivity#every-network-is-an-openid-connect-issuer).
2. **The network boundary.** A connector accepts streams only from Minekube relays and from instances of its own network. This is the one fixed rule; without it, no other rule would be safe. A transfer to a public hostname always works, like a link; whether the target takes the player is the target's decision.
3. **Verified facts in the question.** The routing question carries only what is true: UUID and name after Mojang's check, the client address from the relay, the hostname the player typed, and where they came from.
4. **An origin that cannot be forged.** An instance that sends a player on signs a cookie with its key; the target checks the signature through the registry. Without a valid signature, the origin is empty, never guessed. No central service hands out tickets.
5. **Rejection.** `Route.Kick` wherever the question is asked.
6. **The network's own state.** Lists, bans and invites live in the network's own storage, next to its routing.

The test of whether these are enough: a whitelist, bans, "only players from the hub", a partner network that may send players directly, and a fully open server should each be a few lines of routing, with no new function from us. The network boundary is built and tested against the registry; signed origins and the routing question are planned.

## We build on the same base

Minekube's own network features, such as routing into the waiting room, the way back after a kick, waking a sleeping server and queues, are planned as Effect bundles against the same public SDKs and runtimes every network gets. They are open, can be changed or replaced, and there is no private path for us. If we miss something, that is a gap in the platform, closed for everyone.

What we roll out ourselves stays off operators' servers. An operator runs a small, stable core (the connector and the runtime) that negotiates capabilities, so old versions keep working for a long time. Code in an operator's process changes only when they deploy, or opt in to a channel with signed revisions.

## Network shapes

| | Paper only | With Velocity |
| --- | --- | --- |
| For | New networks, also on hosting panels (planned default) | Existing networks; anyone who wants Velocity's API and plugins (built) |
| A backend crashes | Its players are disconnected; new joins wait in the relay's waiting room | Velocity holds the players and network code sends them on |
| Moving between servers | A transfer (clients from 1.20.5 on) through Minekube hostnames | Inside the session, no new login |
| Cost | Nothing extra | One more JVM ([measured](https://minekube.com/games/platform/performance#networking)) |

Velocity is not a requirement. When the same version goes to every game server, global commands like `/lobby` are simply part of every version, and what servers share lives in Durable Objects. A third shape, a slim Minekube proxy in Rust next to servers that outlive it (a VM or Kubernetes, not a hosting panel), is planned: it would hold players through a server restart without disconnecting them.
