gobridge

MQTT behaviour

How the MQTT transport behaves at run time: message settlement, reconnection, backpressure, shared subscriptions and ingress headers.

See MQTT for sessions and a worked example, and MQTT options for the field-by-field reference.


Settlement Semantics

QoS 2 is NOT exactly-once across a bridge restart. autopaho keeps the outbound packet queue in memory, so an in-flight QoS 1/2 publish (sent, PUBACK/PUBCOMP not yet received) is lost at the MQTT-protocol level on a crash or restart — client_id / clean_start=false resume broker-side state, not the client-side outbound queue. Whether the wired delivery modes (direct_hold, shared_outbox) recover this loss on the source side is conditional. It depends on source redelivery, durable outbox persistence, and producer identity. See the source-to-destination guarantee matrix below for the rows that are safe and the rows that can still lose or duplicate. Operators evaluating an end-to-end exactly-once claim must account for this — see also ADR 0009.

MQTT deliveries are acknowledged after the bridge settles them, not on receipt. The adapter connects with manual acknowledgement and holds the PUBACK (QoS 1) / PUBCOMP (QoS 2) until the runtime acks the delivery — after the downstream send or outbox persist succeeds. Acks are released in receive order, so an in-flight message survives a crash and is redelivered by the broker when a Persistent/Exclusive session resumes.

Ingress cap violations are acked-and-dropped (poison escape)

The one deliberate exception to ack-after-settlement: an inbound publish that violates a local representational cap — max_payload_bytes, the ingress metadata byte cap (128 KiB), or the User Property count cap (128) — while fitting the CONNECT-advertised Maximum Packet Size. The broker enforces only the whole-packet limit (max_payload_bytes + the 128 KiB metadata allowance), so a compliant broker forwards such a packet from any authorized publisher. The adapter acks and drops it (MQTTIngressPoisonDropped, Error log once per violation class) instead of failing the session: an un-acked rejection would be redelivered on every clean_start=false resume and latch the session terminal forever — a publisher-triggerable permanent kill switch. The ack is an acknowledged, counted loss of a message the bridge was configured to refuse; alert on any non-zero value and follow the ingress-poison runbook. Malformed packets and totals above the advertised Maximum Packet Size — producible only by a broken broker — are rejected at the raw pre-decode guard instead. The guard tries to send DISCONNECT with reason code 0x81 (Malformed Packet) or 0x95 (Packet too large), closes the connection, and counts MQTTIngressRejected. The session is not terminal: it reconnects with an extra backoff that grows with consecutive rejects, and it reads not ready until a connection that came up after the last reject has stayed up for 30 s (ADR 0021).

On MQTT 3.1.1 no Maximum Packet Size is advertised, so an oversized PUBLISH is cut to max_payload_bytes + 1 payload bytes, then acked and dropped as above. A malformed packet, an oversized packet of another type, and a broker window above receive_maximum take the reject path without a DISCONNECT, so the broker publishes the Last Will (MQTT 3.1.1).

The same guard bounds the one cap whose decode cost the wire does not bound. A PUBLISH carrying more than 129 User Properties has its list cut to 129 on the raw bytes before the SDK decodes it: the callback still sees a violation and acks-and-drops the packet, but the SDK never decodes more than 129 User Properties, instead of spending roughly 1.3 KiB per property on tens of thousands of five-byte properties. Every such packet is counted on MQTTIngressUserPropertiesTruncated, and the count the publisher actually sent is logged at Debug — the callback’s Error log can only ever show 129.

Bounded recovery from an unsettled delivery

A delivery the runtime received but never settled — a refused emit, a failed Delivery.Retry — pins a broker Receive-Maximum slot until an unrelated teardown. The bounded recovery that clears it, its per-mode policy (a resuming session recycles; an ephemeral one acks, drops and records the loss), its safety bounds and its metrics are documented separately: MQTT settlement recovery.

Source-to-destination guarantee matrix

The delivery guarantee is conditional on five inputs: the source QoS/session, the route delivery mode, whether the publish carries a producer identity, the outbox store’s durability and whether the record was persisted, and where the failure falls relative to the Persist boundary, the envelope TTL, and the replay/poison budget. “No source-side loss” means the bridge does not drop the message — not exactly-once: an accepted-then-unconfirmed send can still duplicate at the destination, so downstream idempotency is required in every row.

Source QoS / session Delivery mode Producer identity Outbox store & persist state Persist / recovery boundary Guarantee
QoS 1/2, Persistent/Exclusive direct_hold any n/a (source-side hold) resume within source session/queue expiry No source-side loss: un-acked input is redelivered on resume and the in-flight publish is re-sent.
QoS 1/2, Persistent/Exclusive direct_hold any n/a resume after source session/queue expiry Possible loss: the source broker dropped the queued input before the bridge resumed.
Configured QoS 1/2, Ephemeral (clean start) direct_hold any n/a config load Rejected: a clean-start session cannot recover the requested stronger deliveries. Persistent clean start is also rejected.
QoS 0, any otherwise-valid session direct_hold any n/a any Accepted best effort, alone or mixed with protected subscriptions. Possible loss: no broker redelivery exists for this packet. An in-flight send may already have reached the destination.
any source shared_outbox unique durable store (SQLite/DynamoDB), persisted envelope TTL not expired, within replay/poison budget No source-side loss: once a uniquely-identified record is durably persisted, the outbox drainer replays it independently of the source session — the source QoS/session no longer matters.
QoS 1/2, Persistent/Exclusive shared_outbox any durable store, crash before Persist source redelivers on resume No source-side loss: Persist precedes the source ack, so a crash before Persist leaves the source un-acked; it redelivers and the record is built and persisted on replay.
QoS 0 or Ephemeral (clean start) shared_outbox any crash before Persist no source redelivery Possible loss: before a successful Persist there is no durable record, and a QoS 0 / clean-start source cannot redeliver.
any source shared_outbox unique volatile store (in-memory; unit-test only, not production), “persisted” process restart Possible loss: an in-memory outbox does not survive a restart, so no record remains to replay.
QoS 1/2 shared_outbox missing (no producer ID) durable, persisted any No silent collapse and no cross-redelivery dedup: each publish gets a fresh per-publish UUID, so two equal-valued events both flow and a broker redelivery of one publish duplicates it. An MQTT 3.1.1 session with message_id: content_hash reverses both: a redelivery is suppressed, and so is an equal-valued event from the same session (message ids).
QoS 1/2 shared_outbox reused (same ID for distinct events) durable, persisted any Collapse of a distinct event: the second event reuses the first’s dedup key (partition + EnvelopeID + binding); its Persist returns ErrDuplicateRecord and is acked-and-dropped. A supplied producer ID is preserved and trusted as identity.
QoS 1/2, source broker offline either any n/a source queue/session expiry or capacity drop before receipt Possible loss: the source broker can expire or drop its offline/session queue before the bridge ever receives the message.
any shared_outbox any durable, persisted ReplayCount > MaxReplayAttempts and ReplayBudget elapsed since first attempt Permanent failure: the record reaches the terminal action below. A record whose envelope TTL passes is expired first, per OnExpired.
any (stable identity) direct_hold present, or bridge dedup/idempotency key n/a source attempts reach MaxReplayAttempts (count only, no wall-clock gate) Permanent failure: the source delivery reaches the terminal action below. A recoverable send failure is first retried inside the bridge for send_retry_budget (default 60s) with the source delivery still held, so a short destination outage no longer recycles the session. Only afterwards is the source asked to redeliver. Count-less sources are counted by the bridge-owned replay ledger keyed on the stable identity.
Any QoS (no stable identity) direct_hold missing (no producer ID) n/a a transient failure that outlives send_retry_budget The message is retried in process for send_retry_budget (default 60s) first. Only if it is still failing then does the terminal action apply: a count-less source with an adapter-generated id cannot be counted across redelivery, so it uses category unstable_identity. Supply mqtt.message-id/correlation data for a countable retry budget (on MQTT 3.1.1, message_id: content_hash, after reading its trade-off), or raise send_retry_budget.
any direct_hold any n/a terminal action after permanent failure/expiry Per OnPermanentFailure/OnExpired (default dlq): confirmed DLQ persistence counts DLQEntries and settles the source; an explicit drop records its terminal metric and settles without a DLQ record.
Retry-capable QoS 1/2, resuming session direct_hold any n/a DLQ persistence fails Remains protocol-unsettled; bounded session recovery requests redelivery. No acknowledged terminal drop or false DLQ success.
Actual QoS 0 direct_hold any n/a Retry unsupported and bounded DLQ persistence fails Terminal loss counted once as MessagesDropped{reason=retry_unsupported_dlq_failed}; the persistence error is surfaced. No successful DLQ entry, no QoS-0-induced session recycle, and route capacity is released.
any shared_outbox any durable, persisted terminal action after permanent failure/expiry The source was already ACKed right after Persist. Per OnPermanentFailure/OnExpired, the drainer completes the outbox record (OutboxStore.Complete) only after a successful DLQ write (MetricDLQEntries) or a recorded drop (loss by design). A DLQ write failure leaves the record pending/claimed, so the drainer retries it — never a silent drop.
any either any any send accepted, response lost Ambiguous: a send timeout after the destination accepted the publish is indistinguishable from a real failure, and a retry may duplicate. Downstream must dedupe.

Rows describe the actual delivered packet, not a route-wide promise. Mixed subscriptions retain the QoS 1/2 effective-session requirements. Configured QoS 0 still requires a DLQ or explicit retry-drop permission and compatible terminal policies. Abrupt-crash gaps cannot be counted by the dead process. See scenario 24.

MQTT 3.1.1 sources carry no producer identity, so by default every message from one is count-less. With a finite max_replay_attempts every retry decision for it is terminal unless the session sets message_id: content_hash. The first recoverable failure dead-letters or drops it (a direct_hold send is still retried in process for send_retry_budget first). max_replay_attempts: 0 opts the route into unbounded retry (no headers on ingress, message ids).

Producer identity is a stable mqtt.message-id (or MQTT correlation data); a content hash of topic+payload is not a producer ID, because two legitimate equal-valued events would hash the same and one would be silently collapsed (an MQTT 3.1.1 session with message_id: content_hash opts into that). A reused producer ID has the same effect from the other direction: a supplied ID is trusted as identity, so a distinct event carrying a duplicate ID collapses into the first record. When no producer ID is present, GoBridge stamps a fresh per-publish UUID so distinct publishes stay distinct — see Envelope identity and no-ID redelivery.

shared_outbox durability is only as strong as the store behind it, and it protects a record only from the moment Persist succeeds. Before that point there is nothing durable: an over-capacity or unavailable store makes Persist fail or block, so the source delivery is retried or DLQ’d — no persisted work is lost, because none exists yet. After a successful Persist, a pending record is not subject to a store-retention TTL. The production SQLite and DynamoDB stores and the in-memory fake never evict a pending or claimed record; retention only compacts terminal (completed/expired) records. A durably persisted record is instead bounded by:

What the bridge records is the settlement and outbox state it observes: unsettled_count, outbox record status, DLQ entries, and drop counters. What it cannot know is whether a message sat in a broker’s offline queue before the bridge connected, or whether a destination that never returned a response committed the publish. Those unknowns are why the matrix labels those rows possible-loss or ambiguous rather than safe.

The only way an in-flight loss becomes bridge-level loss outside the rows above is a delivery mode that acks the source before the transport confirms the publish. No such mode exists today, so the bridge emits a route-aware startup advisory (bridge.egressDurabilityAdvisory) that stays silent for both current modes and exists only to flag such a future mode.

Resilience Behavior

QoS downgrade

An MQTT 5 broker may answer a SUBSCRIBE with a SUBACK that grants a lower QoS than the route asked for, for example QoS 0 when the route asked for QoS 1. Brokers do this to enforce a QoS cap (Mosquitto max_qos, an authorization rule). The bridge keeps such a subscription running instead of failing:

  1. Confirm. One low grant can be transient (a cluster node restarting, an authorization rule still propagating). The SUBACK the reconcile received is the first answer, logged at Warn (broker downgraded subscription QoS below requested; confirming with fresh SUBSCRIBEs …) with the topic, the requested QoS and the granted QoS. The session then sends SUBSCRIBE for the same filter twice more, 5 s apart. These re-sends use Retain Handling 1, so the broker does not replay retained messages, and the same QoS and No-Local as the original. While it confirms, the filter is not counted as active (messages still arrive at the granted QoS), session health is Degraded, and the reconcile itself succeeds, so the supervisor does not retry the session and an exclusive session keeps its lease. A grant that meets the requested QoS during confirmation returns the subscription to normal. A different lower grant starts the confirmation again. A confirmation SUBSCRIBE that gets no grant keeps the last one and is retried after 5 s, the wait doubling with each further miss up to 1 min.
  2. Accept as best effort. When three fresh SUBACKs report the same lower grant, the subscription becomes active at the granted QoS and has that QoS’s guarantee. At QoS 0 (see the QoS 0 row of the guarantee matrix) there is no acknowledgement and no redelivery, and messages published while the bridge is disconnected may be lost. The bridge logs this once at Error with the topic, the requested QoS and the granted QoS. Deep health lists the filter in best_effort_topics, and the session can report Full.
  3. Re-check. Every qos_recheck_interval (per subscription; default 1h, 0s turns it off, minimum 1m; see Subscription Options Reference) the session sends a fresh SUBSCRIBE with Retain Handling 1. It never runs at the same time as a reconcile of that session. If the broker grants the requested QoS, the subscription returns to normal and the bridge logs it at Info. A re-check that is still lower changes nothing. A re-check the broker refuses keeps the last grant and is retried at the next qos_recheck_interval. A run of refusals, during confirmation or re-check, is logged at Warn once. Every reconnect also re-evaluates the grant through the normal reconcile.

Failed deliveries. The bridge validates a route for the QoS it requests. A route whose subscriptions are all QoS 1 or 2 on a session that resumes counts as a source that redelivers, so it is exempt from the retry check that asks a source without redelivery for a DLQ store or allow_retry_drop. An accepted QoS 0 grant breaks that assumption: the broker cannot redeliver a message the route fails to process, so the route’s retry fallback takes over. With the default route policies (on_permanent_failure and on_expired both dlq) the bridge must have a DLQ store anyway, so the message is dead-lettered. It is dropped and counted as MessagesDropped{reason=retry_unsupported} only on a bridge with no DLQ store, which validation allows only when every route sets on_permanent_failure: drop and on_expired: drop and none sets on_filtered: dlq. For a broker that caps QoS, keep a DLQ store configured, or lower the route’s qos to the granted level.

Two metrics follow the downgrade. MQTTQoSDowngraded counts once when a SUBACK first reports a lower grant (a changed grant counts again; confirmations and re-checks do not). MQTTQoSDowngradedActive, tagged session_id, is a gauge of the accepted downgrades per session, written when the count changes and every 30 s while it is above zero. It falls when a grant recovers, when the plan stops wanting the filter, or when the session closes, so alarm on the gauge for a standing condition. See adapter diagnostic metrics.

A lower grant never stops the session and never restarts the process. A refused subscription (SUBACK reason code 0x80 or higher) is different: it still fails that session’s reconcile, which retries on its own without affecting other sessions.

To remove the downgrade, lower the route’s qos to the granted level, or lift the broker’s cap.

On MQTT 3.1.1 the SUBACK carries the granted QoS too, so a downgrade is detected the same way, but every confirmation, re-check and reconnect SUBSCRIBE of the filter replays its retained messages, because 3.1.1 has no Retain Handling.

Backpressure and dispatch

The publish callback paho invokes must return quickly or the client stops servicing PINGRESP/PUBACK and the connection dies of keepalive starvation. The adapter therefore hands each inbound publish to a serialized dispatch queue and returns:

On MQTT 3.1.1 receive_maximum is not sent, so the broker’s own per-client in-flight limit sets the window. The session enforces receive_maximum itself: a broker that sends more unacknowledged QoS 1/2 publishes is refused with an ingress reject before the extra publish reaches the dispatch queue. Set receive_maximum to at least the broker’s limit (broker limits).

Capacity sizing

Sustained QoS 1/2 ingress throughput is bounded by the un-acked in-flight window and how fast the bridge settles:

max sustained msg/s ≈ receive_maximum / avg settlement latency (s)

where settlement latency is the route’s end-to-end accept time — outbox persist for shared_outbox, target accept for direct_hold. With the default receive_maximum: 192 and a 20 ms settlement, that is ~9,600 msg/s per session; a 200 ms downstream caps the same session at ~960 msg/s. Levers, in order:

  1. receive_maximum — widens the in-flight window. It is a count you set (default 192); the broker must also allow the window. The same count sizes the dispatch queue and caps the messages waiting for a receiver (see ingress limits are counts).
  2. Route max_in_flight — concurrency downstream of dispatch; raising it reduces settlement latency until the target saturates.
  3. max_payload_bytes — the message-size limit the session announces to the broker. It does not change the window; it caps how large each message in the window may be.

GoBridge does not estimate memory. A larger window or larger messages use more memory, so measure the process under its real load after changing any of these.

QoS 0 is not flow-controlled by receive_maximum: a QoS 0 flood sheds at the dispatch queue (MQTTRouterDropped) rather than backpressuring the broker. Watch MQTTReceiveWindowUtilization (sustained → 1.0 means the window, not the network, is the ceiling) and MQTTOldestUnsettledAge (rising means the downstream, not MQTT, is the bottleneck).

The dispatch queue and the pending buffer are bounded by counts, never by bytes. A non-compliant broker can still put one decoded packet in the SDK before the callback sees it, but an oversize body is rejected before the adapter copies or enqueues it; QoS 1/2 remains unacknowledged, preserving at-least-once semantics.

Shared subscriptions ($share)

MQTT declares the shared_consumer capability. A subscription filter of the form $share/<group>/<filter> is a shared subscription: the broker load-balances the topic’s deliveries across every client in <group>, so several bridge instances (or several receivers) consume one logical subscription as a scale-out group instead of each receiving a full copy.

Declare the shared filter in the receiver’s topics[] exactly as the broker expects it ($share/<group>/<filter>). The adapter strips the $share/<group>/ prefix before matching, so routing keys off the concrete topic the broker delivers on, not the $share wrapper. Ordinary (non-shared) subscriptions are unaffected.

Each scale-out instance needs a UNIQUE client_id

Shared-subscription scale-out and client_id interact in a way that is easy to misconfigure into a self-DOS. The broker load-balances a $share group across distinct sessions, and a session is keyed by client_id. So:

The adapter cannot see the other replicas’ client_ids from one process, so it detects the symptom: when $share subscriptions are configured on a non-Ephemeral session it warns once about the unique-client_id requirement, and a session takeover while $share is active (outside Exclusive mode) is logged at Error on the first occurrence — that combination is the smoking-gun of a reused client_id. MQTTSessionTakeover counts every takeover; a persistent non-zero rate on a $share deployment means the client_ids are colliding.

MQTT 3.1.1 has no takeover reason code. On a v3.1.1 session a connection that drops within 30 s of coming up feeds the same takeover penalty instead, with its own log line naming a client-id collision; MQTTSessionTakeover is not counted. $share/ from a 3.1.1 client is the broker’s choice: Mosquitto, EMQX, HiveMQ, VerneMQ and AWS IoT accept it; Azure Event Grid disconnects the client.

Recipe: unique client_id per replica from one config file

A Kubernetes Deployment or ECS service scales one config (ConfigMap / task definition) to replicas: N, so every pod reads the same client_id — the self-DOS above. client_id_suffix resolves it at build time without per-pod templating:

sessions:
  - id: telemetry-in
    transport: mqtt
    session_mode: ephemeral        # scale-out, NOT exclusive
    options:
      session:
        broker_url: tls://mqtt.prod.example.com:8883
        client_id: telemetry-consumer   # shared base in the ConfigMap
        client_id_suffix: hostname       # -> telemetry-consumer-<pod name>
        clean_start: true

Do not set client_id_suffix on an exclusive session. Exclusive failover needs a stable shared client_id so the standby can resume the dead owner’s broker session; a per-instance suffix would strand queued QoS 1/2 messages on every failover. The build rejects client_id_suffix when session_mode: exclusive. See scenario 08.

The ingress header reference is on its own page: MQTT ingress headers.