Skip to content

Design: Multi Tenant Hub

Timo Sariwating edited this page Nov 21, 2026 · 3 revisions

Status: design draft (2026-11-04). No code yet: the point is to make the choices now that keep this possible, so the self-hosted hub of today grows into it without a rewrite.

The question: should Maker Connect already be designed for a large online service, with thousands of devices and users, many makerspaces that must never see each other's data, permissions per device, and several hub servers sharing the load? Yes in the design, not yet in code. A few seams cost little now and are expensive later; the rest can wait until there is a service to run.

What it has to do

  • Tenants. A makerspace (or a maker at home) is a tenant. Nothing leaks between tenants: not devices, files, jobs, telemetry, names, nor the fact that another exists.
  • People. Users belong to one or more tenants with a role, and may use a device only as far as their rights go: see it, run jobs on it, operate it (move, heat, stop), manage it. Rights can depend on a permission (who has had the laser training) and on bookings (who has the laser now).
  • Scale. Thousands of tenants, tens of thousands of devices reporting every few seconds, browsers following them live, files of hundreds of megabytes.
  • Several servers. Any server can serve any request; one can fail or be replaced without machines noticing more than a reconnect.
  • One codebase. The same software still runs as one file on a Pi for one space.

The shape

flowchart LR
    subgraph Edge["Gateways (stateless, many)"]
        G1["Agent gateway<br/>HTTPS · WebSocket · MQTT"]
        G2["Prusa Connect gateway"]
    end
    subgraph App["App servers (stateless, many)"]
        W["Website and API"]
        R["Workers<br/>jobs · rules · notifications"]
    end
    subgraph Data["State"]
        PG[("PostgreSQL<br/>tenants, users, devices,<br/>jobs, stock — row-level security")]
        TS[("Time series<br/>telemetry history")]
        OB[("Object storage<br/>files, per tenant prefix")]
    end
    BUS{{"Pub/sub backbone<br/>NATS JetStream<br/>an account per tenant"}}
    Agents["Agents and machines"] --> G1 & G2
    G1 & G2 <--> BUS
    W & R <--> BUS
    W & R --> PG & TS & OB
    Browsers --> W
Loading

The pub/sub backbone

Everything that happens is a message on a subject, per tenant:

t.<tenant>.d.<device>.telemetry     a report (state, temperatures, progress)
t.<tenant>.d.<device>.event         a job started, ended, a cassette out, an alarm
t.<tenant>.d.<device>.cmd           a command for the device (G-code, pause, a label)
t.<tenant>.d.<device>.presence      online, offline, through which gateway
t.<tenant>.stock.*                  consumables used, moved, received
  • A gateway holds the connection of a device. It publishes what the device says, and subscribes to that device's cmd. A command reaches the device through whichever gateway holds it: no sticky routing, no list of who is where.
  • A browser follows a tenant's subjects through an app server, filtered by what that user may see.
  • Workers subscribe to events: a job that ended moves stock, a rule switches the fume extractor, a notification goes out.
  • Last values (each device's latest state, presence) live in the bus's key-value store, so a new page shows everything at once without asking each device.

Choice: NATS with JetStream. Its accounts are multi-tenant by design: subjects of one account are invisible to another unless exported on purpose. That makes isolation a property of the bus, not only of our code. It is small, one binary, clusters across servers, has durable streams and a key-value store, and serves tens of thousands of messages a second on modest hardware. Alternatives: an MQTT broker (EMQX) is close to the devices' world (Shelly, Tasmota speak it) and can be bridged in either way; Zenoh (as in Hive-Rail) is very light and fits the edge, but has less for tenants and durable streams. A bridge lets Shelly's and Tasmota's MQTT into the tenant's subjects either way.

The numbers are small for a bus: 20 000 devices reporting every 2 s is 10 000 messages a second. History is stored downsampled (every report for an hour, a minute's average for a month).

Isolation in three layers

  1. The bus: a NATS account per tenant; gateways and app servers only get credentials scoped to the tenant they act for.
  2. The database: a tenant_id on every row, and PostgreSQL row-level security: every connection sets its tenant, and the database itself hides other rows. A forgotten WHERE cannot leak.
  3. Files: object storage under a prefix per tenant, served only through signed, short-lived links.

Plus tests that try to leak: one tenant's token against another's devices, files, jobs and subjects, on every endpoint.

People and rights

Thing What
User A person, logged in through OpenID Connect (Google, Microsoft, the makerspace's own) or a password with a second factor
Membership A user in a tenant, with a role: owner, admin, member, guest
Permission Per device (or group of devices): view, use (run jobs, print labels), operate (move, heat, stop), manage (settings, remove)
Permission Given by an admin after a training (the laser course); some devices require it before use. In the code: induction.
Machine group Machines given permission on together ("3D printers FFF"), also those added later. A member gets the most of their permissions per machine and per group. New machines need permission.
Booking A time slot on a device; outside it, use is refused to others
Audit Who did what on which device, kept per tenant

Devices authenticate apart from people: a token per device (or per agent, for the devices under it), revocable, rotated now and then.

Agents at scale

Today an agent asks every 2 s over HTTPS and gets commands in the answer: fine for a space, wasteful for many. Next, a persistent connection (WebSocket, or MQTT for small devices), with the same messages, so a command arrives at once and an idle device costs almost nothing. Agents keep working against a self-hosted hub the same way.

The same code, small and large

The seams that let one codebase be both:

Seam Self-hosted (today) Online service
Store PostgreSQL with TimescaleDB and row-level security (Hub Core v2; SQLite until step 2) The same, larger
Bus In the process (a broadcast channel) NATS JetStream, an account per tenant
Files A folder beside the database Object storage (S3, MinIO)
Login None (or one local user) OpenID Connect, memberships, permissions
Servers One process Gateways and app servers, many of each

What to do now, and what not

Now (cheap):

  • Keep every record under a space (already so: space_id everywhere), and every query through the store, never around it.
  • Put the in-process event channel behind a small Bus trait (publish, subscribe by subject), with subjects shaped like the ones above, so NATS can replace it.
  • Give every device a stable id that is not its token (already so), and keep tokens per device.
  • Keep no state in a server that the next request needs, apart from the store and the bus (today's in-memory command list and temperature history are the exceptions to move).
  • Keep the agent protocol versioned (already so), so a WebSocket transport can come beside polling.

Moved forward: PostgreSQL with row-level security, commands kept as records and one event stream are part of Hub Core v2.

Later (when there is a service to run): NATS, object storage, login and permissions, gateways, billing.

Open questions

  • A space inside a tenant, or a tenant per space? (A makerspace with two locations; a maker with a home shop who is also a member of a space.)
  • Can one device be shared between tenants (a space lending its laser to a school), by an export between accounts?
  • Where does the hub run for a space that wants its data in its own country?

Clone this wiki locally