Architecture

The thread-per-core data plane, cross-core routing, sessions, retained store, extension lanes and the control plane — what is built and what is planned.

Built and planned

The single-node broker described here is built (milestone M1). Items marked planned are not implemented yet, and details may change as they are built and measured.

Overview

OrbitMQ separates a data plane, which handles every MQTT packet, from a control plane, which handles management. The data plane is designed so that the common path (receive a PUBLISH, match subscribers, write to sockets) never takes a lock and never waits for the control plane.

OrbitMQ node architectureClients connect to cores. Each core holds a listener, sessions, a local subscription index, a wildcard trie replica and a retained shard. Cores exchange messages over data and control rings. A shared route directory maps exact filters to the cores with subscribers. A publish on core 0 looks up the directory and is handed to cores 2 and 3, which deliver it to their subscribers. A subscribe on core 1 sets core 1's bit in the directory. The control plane serves the admin API and metrics.NODEpublishernew subscribersubscribersubscriberCORE 0listenersessionslocal indexwildcard trieretained shardCORE 1listenersessionslocal indexwildcard trieretained shardCORE 2listenersessionslocal indexwildcard trieretained shardCORE 3listenersessionslocal indexwildcard trieretained sharddata ringscontrol ringsshared route directory · exact filter → core bitmask · wait-free readsbit 0bit 1bit 2bit 3control plane · admin REST API · /metrics · config (separate thread)publishsubscribering hand-off
One node, four cores. A publish on core 0 looks up the shared route directory, then crosses the data rings once per destination core. A subscribe on core 1 sets that core's bit, so every later publish routes to it.

Thread-per-core data plane

  • One event-loop thread runs per core (one per CPU by default, cores to change it), pinned to its CPU when pin_threads = true (unless the process runs under a CPU quota without a cpuset). The event loops run on compio: io_uring with an epoll fallback on Linux, kqueue on macOS.
  • Each core owns the connections it accepts, their sessions and buffers, a shard of the subscription index, a shard of the retained store and a shard of the client-id registry. State is shared-nothing: plain Rc/RefCell data owned by one thread, with no atomics or locks on the hot path.
  • On Linux every core accepts on its own SO_REUSEPORT socket and the kernel spreads connections across cores. On macOS one acceptor hands connections to the cores round-robin.
  • Each connection is split into a read half and a write half driven by separate tasks on the same core, so reading never waits for a write and the reverse.
  • All runtime access goes through the orbitmq-rt crate, which keeps the runtime backend replaceable.

Cross-core routing

A publisher on one core must reach subscribers on any core without locks on the publish path. OrbitMQ routes in two levels:

  • Local index (per core): every core keeps an index of its own sessions' subscriptions.
  • Shared route directory (per node): a single node-wide table maps each exact (wildcard-free) filter to the set of cores that have at least one subscriber for it, stored as a small bitmask. Every core reads it without locking, and each core only flips its own bit when its first subscriber for a filter arrives or its last one leaves. It costs about 60 bytes per filter, whatever the number of cores.
  • Per-device wildcard filters (per node): a wildcard filter with at least two literal levels before its first wildcard, such as devices/42/# or tenants/acme/devices/+/status, is stored once per node, keyed by that literal prefix. These can number in the millions, and they cost about 100 bytes each, whatever the number of cores.
  • Other wildcard filters (per core): filters such as sensors/# or +/status are few, so every core keeps its own copy. A node-wide counter tells each core when a new wildcard subscription is pending, and the core applies it before it routes its next batch of messages.

Publish. The publisher's core looks the topic up in the shared directory and in its wildcard copy, serves its own subscribers directly, and hands the message once to each other destination core over a bounded ring. The payload is a reference-counted buffer, so fan-out does not copy it. Each destination core matches the message against its local index and applies the per-subscription options (QoS downgrade, No Local, Retain As Published, subscription identifiers, topic aliases). A message crosses at most one ring.

Ordering. A publisher lives on one core and each ring is first-in-first-out, so every subscriber receives one publisher's messages in the order they were published, as MQTT requires.

Subscribe visibility.

  • A message the broker processes after it has processed a SUBSCRIBE always reaches the new subscription, on any core. A message processed before it never leaks into it.
  • In particular, every message published after the SUBACK is delivered, as is anything causally after the subscription (for example the response in an MQTT 5.0 request/response exchange).
  • If a SUBSCRIBE and a PUBLISH from two different connections reach two cores in the very same instant, neither comes first, and MQTT defines no order between connections before SUBACK. Either outcome is correct. Clients that need the message should wait for SUBACK before triggering it, as MQTT clients normally do.
  • Retained messages are collected from the retained shards and delivered after the SUBACK, exactly once.

Backpressure. If a ring or a subscriber's buffer fills, the broker stops reading from the publisher until it drains, for every QoS level, so overload slows publishers down instead of losing messages. A subscriber that stops reading entirely (a slow consumer) does not hold publishers back: its own QoS 0 messages are dropped at its buffer cap and counted in orbitmq_dropped_messages_total.

Shared subscriptions. The directory keeps a separate bitmask for each $share/{group}/{filter}, so the publishing core picks exactly one member core per group and message.

Sessions and session takeover

  • A session holds a client's subscriptions, its in-flight QoS 1 and QoS 2 state and its queued messages. It lives on the core of the client's current connection.
  • Each client identifier has a registry core, chosen by hashing the identifier. On CONNECT the accepting core claims the identifier there. If another core holds it, that core closes the old connection and transfers the session; CONNACK is sent only after the claim and transfer complete.
  • While a session moves between cores, the old core keeps its subscriptions live and forwards matching messages to the new core until every core has switched over, so no QoS 1 or QoS 2 message is lost and per-publisher order is kept.
  • Clean and persistent sessions are supported, including MQTT 5.0 session expiry, message expiry, Receive Maximum, topic aliases and flow control.
  • Sessions are held in memory. Persistence across restarts is planned.

Protocol codec

  • One codec handles MQTT 3.1, 3.1.1 and 5.0. The protocol level is negotiated at CONNECT and fixed for the life of the connection.
  • Decoding works on owned buffers and references payloads instead of copying them; frames to subscribers are encoded once per variant and written with vectored writes.
  • Every malformed-packet rule in the specifications is a catalogued requirement with tests (see Conformance).

Retained store

  • Retained messages are sharded across cores by topic. Each shard is a level trie that matches filters with the same semantics as the router, including the $ rule.
  • Limits per shard are configurable (max_retained_messages, max_retained_bytes), and expired messages are removed as their Message Expiry Interval runs out.
  • A SUBSCRIBE with an exact filter first checks a small node-wide summary (256 KiB) that counts retained messages per topic hash. When the count is 0, the topic provably has no retained message, so the broker skips the lookup across cores. This keeps subscribe storms from slowing down publishes.
  • Retained messages are held in memory. A crash-safe persistent store is planned.

Extension lanes

Authentication, authorization and other extensions plug into the broker through hook points defined in community crates, in one of three lanes, so that they never slow down the publish path:

LaneRunsContractStatus
InlineOn the owning core, synchronouslyNo I/O, bounded timeBuilt: the authorization chain (Authorization), evaluating ACL files and the rules loaded at CONNECT
AsyncStarted on the core; may hand work to other threadsThe core keeps serving other connections while it runsBuilt: the authentication chain and the CONNECT-time ACL loads (Authentication), with CPU-heavy hashing on a blocking pool, network sources on a separate runtime, and a timeout
TapControl-plane workers fed through a bounded ringThe core hands over a reference-counted message and never waitsPlanned

Extensions are registered as factories. Each core builds its own instances at start-up, so calls need no atomics or locks, and a hook with nothing registered costs no call at all. Authorization fails closed by default: when no source decides, the action is refused. The authentication and authorization chains can be reordered and edited at runtime: the control plane publishes an immutable snapshot, and each core picks it up by comparing one generation counter, so the publish path takes no lock. Live reloading of other configuration is planned; today, it is read at start-up.

Control plane

  • The admin REST API and the Prometheus /metrics endpoint run on a dedicated thread with their own runtime, never on a data-plane core.
  • Queries and metric scrapes are sent to every core over a dedicated, bounded control ring. Cores answer between other events from their own state; nothing on the publish path waits for the control plane.
  • Metrics are plain per-core counters, copied and added up only when scraped.
  • A live metrics stream for the dashboard and OpenTelemetry export are planned.

On this page