NOTE 001 / CLOUD PLATFORM, SECURITY / 2026-11 / 11 MIN READ
Choosing a control channel for a bring-your-own-cloud fleet
Abstract. When a vendor manages databases inside thousands of customer cloud accounts, every agent has to dial out to the vendor, and the vendor still has to order failovers, backups and upgrades down that connection. We compared NATS with JetStream, MQTT brokers, point-to-point gRPC, WebSocket and HTTPS long-poll streams, and managed IoT services for this job. Bandwidth and throughput do not separate them. What does is how each handles a long command whose executor dies halfway, how it isolates tenants, which internal resource runs out first at tens of thousands of agents, and how its price responds to the status interval.
The problem
In a bring-your-own-cloud model the database runs in the customer's account and the vendor manages it from outside, through an agent on every node. We planned for hundreds to thousands of three-node clusters: 300 to 30,000 agent connections.
The channel carries two classes of traffic with opposite needs. Status flows up every few seconds per node, and a lost message is corrected by the next one. Commands flow down rarely, around ten per cluster per day, and a lost or doubled command is an incident: a switchover performed twice, or a failover executed a day late.
Two constraints come before any technology. The agent opens the connection, outwards, so the customer adds one egress rule and no inbound rule, and the vendor holds no route into the customer's network. And the vendor holds no standing credential that can log in to a node. A common starting point, a control plane that dials into each node over SSH and polls it, meets neither, and is not the subject of this note.
Bandwidth does not decide
Wire efficiency and benchmark throughput are the obvious place to start, and neither discriminates. Every push design we modelled, NATS, MQTT, a gRPC stream and a WebSocket, lands within ten percent of the others in bytes per status update. One node of any serious broker has two to three orders of magnitude of published headroom over 30,000 messages per second. A broker adds a hop of low single-digit milliseconds, against a cluster manager whose own failure detection runs on ten-second ticks. What remains is semantics and ceilings.
What "guaranteed" has to mean
"Reliable delivery" hides two guarantees. Delivery means the message reached a process. Execution means the command was carried out once. No transport provides the second, because the executor can die after acting and before acknowledging, and the sender cannot tell "never ran" from "ran and died". Exactly-once execution against a database is not available from any product. The achievable property is effectively-once: at-least-once delivery, with commands built so that a repeat converges.
- Every mutating command names its target and condition. "Promote replica B" converges when replayed; "promote the most caught-up replica" performs a second switchover.
- Every mutating step reads cluster state immediately before acting, acts only if the condition holds, and reads again afterwards, because the outcome of a step whose executor died is unknown until observed.
- Every command carries an expiry, so a failover ordered during a day-long outage does not run when the link returns.
A fencing token helps, within a limit. Following Kleppmann, a lease can expire while its holder is paused, so the resource that matters must reject stale tokens. A cluster manager checks no token, so the token protects the control plane's task record, not the database. The rules above bound what a stale executor can still do to one convergent step.
The transport therefore has to supply, or the control plane team has to write: a durable queue, a lease that lapses on silence, redelivery to a sibling agent in the same cluster, deduplication by message id, an acknowledgement sent after execution rather than on receipt, a heartbeat that extends a long step's lease, per-tenant isolation, and a way for several control plane replicas to share the fleet.
The candidates
Point-to-point streams
gRPC streams, WebSockets and HTTPS long-poll all satisfy the direction constraint, and the connection carries nothing durable. The control plane keeps a per-cluster outbox, a visibility timeout returns unacknowledged commands, and with several replicas a session registry routes each command to the replica holding that cluster's connection. This is the dominant shape in the category, used by Kubernetes, Elastic Fleet, MongoDB Cloud Manager and Redpanda's BYOC offering. It is proven and needs no new concepts; its price is that every mechanism above becomes code the team owns. Load balancer idle timeouts (350 seconds on an AWS Network Load Balancer, 60 by default on an Application Load Balancer) make application pings mandatory.
MQTT brokers
MQTT 5 has the right vocabulary: QoS 0 for status, QoS 1 for commands, shared subscriptions so that one of three agents takes each command. Two details of the specification decide the matter.
First, a client must send its acknowledgements in the order the messages arrived. An agent that acknowledges only after execution, the only acknowledgement that means anything here, cannot acknowledge command 3 until commands 1 and 2 are done. A backup running for an hour stalls every later acknowledgement on that connection, and once the unacknowledged count reaches the negotiated Receive Maximum the broker must stop sending. The workarounds are a connection per command, or acknowledging on receipt and persisting locally, which moves redelivery back into the agent.
Second, when a shared-subscription member's session ends holding an unacknowledged message, the server SHOULD pass it to another member. That is a recommendation, so redelivery to a sibling is product behaviour: HiveMQ documents it, and we could not find the statement for EMQX. Licensing narrows the field further: an EMQX cluster needs a commercial licence, and Mosquitto is single-threaded with no clustering.
NATS with JetStream
Commands go into a work-queue stream; each cluster has one durable pull consumer shared by its three agents, so each command reaches exactly one. An agent extends its lease with in-progress acknowledgements and acknowledges after execution. If it dies, the heartbeats stop, the acknowledgement deadline lapses (30 seconds by default), and the command goes to a waiting sibling with its delivery count raised. Acknowledgements are unordered, so a long step blocks nothing else. Publishes are deduplicated by message id, and a per-message TTL carries the command's expiry into the broker. Worst-case takeover is about 40 seconds on defaults, tunable to seconds. Status rides core NATS on the same connection, at most once.
Managed IoT services
AWS IoT Core with Jobs and Azure IoT Hub with cloud-to-device messages both document at-least-once command delivery, though Azure's Basic tier has no cloud-to-device path. They fail on price.
| Property | Point-to-point stream | MQTT 5 broker | NATS with JetStream | Managed IoT |
|---|---|---|---|---|
| Durable queue, lease, redelivery | control plane code | broker-specific | configuration | vendor |
| Acknowledge after execution | application protocol | yes, in arrival order per connection | yes, with in-progress heartbeat | job or lock state |
| Several control plane replicas | session registry, or stateless over a shared store | broker holds sessions | broker holds sessions | vendor holds sessions |
| Ordering | per stream or request | per topic; none across shared members | per subject; none across pullers | not assessed |
| Cost driver | control plane capacity | hosts, plus cluster licence for EMQX | hosts | messages |
Ordering, often raised first, matters least. Strict order across a cluster's agents is neither available nor needed, since the control plane serialises commands per cluster. The ordering rule that bites is MQTT's acknowledgement order.
Isolation is configuration, so test it like code
The strongest NATS primitive is the account: identical subject names in two accounts are different subjects, crossing requires a deliberate export and import, and each account has its own JetStream store. We give every customer an account and confine each node's credential to its own subjects, so a permission mistake reaches that customer's own clusters at worst. A per-account connection cap stops a leaked credential from crowding out other customers.
Three details matter more than they look. Pulling from a consumer and acknowledging are themselves publishes, so a "status only" credential can neither receive nor acknowledge a command; enumerate the full grant at design time, before someone widens it under pressure. Attribute every report to the subject the broker enforced, never to a field the publisher wrote. And build subjects only from system-generated identifiers: a customer-chosen name containing a dot silently drops out of a fleet-wide wildcard subscription, and one containing a wildcard widens the grant built from it.
The ceiling at tens of thousands of agents
JetStream's real limit is not throughput. Every stream or consumer with more than one replica is its own Raft group, coordinated by one meta leader, and Synadia's guidance is to stay under roughly 1,000 to 2,000 per cluster and consolidate into fewer, larger streams. A stream and a replicated consumer per cluster, at 10,000 clusters, is an order of magnitude past that.
Hence one stream per customer with a subject-filtered consumer per cluster. Consolidation removes the stream term but not the consumer term. One-replica consumers escape the count, but their delivery state dies with their host and must be rebuilt from the control plane's record. Beyond that, the fleet shards across independent broker clusters of 1,000 to 2,000 clusters each, or the lease moves into the control plane's database with NATS as a push transport only. Both change the deployment, not the choice.
Two ceilings belong to every design. After a broker restart 30,000 agents reconnect, about 30 seconds of one core in TLS alone in our indicative local measurement, so reconnects need randomised backoff. That backoff then sets liveness: with full jitter up to a 60-second ceiling, a staleness threshold derived from the publish interval would mark thousands of healthy nodes unreachable during every planned restart. Derive it from the reconnect ceiling instead. And 30,000 nodes at 3 seconds is 10,000 messages per second into the control plane; at a second replica, the instinctive queue group on that subscription would give each replica half the fleet and sweep the other half into "unreachable".
The cost curve
Managed IoT services meter messages, so their bill scales with agents divided by interval: moving from 3 seconds to 1 roughly triples it. A self-hosted broker is priced by hosts.
| Option | 3,000 agents, 3 s | 30,000 agents, 3 s | 30,000 agents, 1 s |
|---|---|---|---|
| AWS IoT Core, publish only | $2,313 | $19,203 | $55,995 |
| AWS IoT Core, publish and delivery to the control plane | $4,390 | $37,601 | $111,185 |
| Azure IoT Hub Standard | $2,500 | $7,500 | $22,500 |
| Self-hosted NATS, three hosts | about EUR 60 to 130 on Hetzner; $100 to $450 on AWS across this range | ||
The self-hosted line is infrastructure only; its real price is one more stateful service to run, patch and audit. And if update frequency is sold as a licence tier, a tier change is a fleet-wide cost change, so the interval needs a validated floor.
Silence is not evidence
Status is lossy, and a broker restart silences every node at once. A missed heartbeat may mark a node unreachable and nothing more; replacing it needs corroboration independent of its connection, and correlated silence trips a fleet-wide breaker instead. That deserves a note of its own.
The security track record
When we evaluated it in September 2026, GitHub listed fourteen advisories against nats-server published that year. Nine are in MQTT, leafnode and WebSocket handling. Five touch paths a design like ours uses, among them an authentication bypass in certificate-to-user mapping (CVE-2026-33248) and an authorization bypass in the JetStream management API (CVE-2026-33222), all fixed in releases older than the one we evaluated. Earlier history includes a critical JetStream API authorization failure (CVE-2025-30215).
The conclusions are concrete. Enable only the client TLS listener, with monitoring on loopback; MQTT, leafnode, WebSocket and tracing stay off. Use per-node signed credentials rather than certificate mapping. Set TLS 1.3 explicitly, since the server defaults to 1.2. Run the tenant isolation test on every change, because the server code that enforces identity and permissions has had bugs before. Run the broker as its own process, never embedded in the control plane, so a patch is a broker restart rather than a release.
The alternatives have their own versions of this. Point-to-point streams have no broker to patch, but the same credential lifecycle plus queue code an auditor will read as closely. A managed service takes over patching and adds a subprocessor. For all of them, instance user data is readable by any process on the instance, so it should carry only a single-use join token exchanged at first boot. The accurate claim at the end is "no standing interactive access", not "no access": the vendor can still order a failover, but cannot log in.
What we chose
Self-hosted NATS with JetStream, both classes over one outbound connection per node, under one rule: the queue decides which agent receives work, the control plane's database decides which work is authoritative, the cluster manager decides the state of the cluster, and the agent is never a fourth source of truth. A lost stream is republished from the database. The fallback, HTTPS long-poll with a database queue, sits behind the same transport interface.
What we would do differently
- Measure the Raft group ceiling at 100, 1,000, 5,000 and 10,000 clusters before fixing the stream layout. It is the one number that can change the deployment.
- Separate heartbeat, change events and metrics in the first status contract. Immediate changes, a 5 to 10 second heartbeat and metrics only while someone watches would carry 3,000 to 6,000 messages per second at 30,000 nodes, against 10,000 at a flat 3 seconds.
- Enumerate the credential grant, including pull and acknowledgement subjects, before issuing the first credential.
Open questions
- Does revoking a credential close an open connection, or only refuse the next one? The reference does not say.
- Can a cluster manager expose a topology generation for conditional commands to name? Without one, the guarantee stays convergent rather than fenced.
- One replica or three for command consumers? One is cheaper against the ceiling; three keeps delivery alive when a broker host and the control plane fail together.
- We did not compare the advisory histories of MQTT brokers and gRPC stacks with the same rigour, and the security argument is incomplete until we do.