IMD SolutionComputing LibraryShop main store

Troubleshooting

Why a PLC needs one MQTT session per broker

Edge computing and IIoT Published 6 min read By IMD Solution engineering team

Short answer

Yes: MQTT puts no limit on how many brokers one device talks to, and each connection is a separate session on a separate socket. What the protocol demands is independence: every broker gets its own client ID, credentials, TLS trust and reconnect handling. Whether your controller can run both sessions is decided by your runtime and the license of the software that owns the connections, not by MQTT.

On this page
  1. What one broker session binds to
  2. Why the protocol answer is not the license answer
  3. The per-session checklist
  4. Keeping one broker's failure off the other
  5. Buffer needed to cover a 30 minute outage at one message per second
  6. The decision for each case
  7. Questions asked

Yes, you can publish the same data to two MQTT brokers from one controller. MQTT sets no limit on how many brokers a device talks to; every connection is an independent session on its own socket. What the protocol demands is that the two sessions stay independent: each broker gets its own client ID, its own credentials and TLS trust, its own keep-alive and its own reconnect logic. Whether your controller may run both sessions is not an MQTT question at all. It is decided by your runtime and by the license of the software that owns the connections: a licensed connectivity feature limits only that feature, and a separately licensed client library brings its own terms. Work through the checks in order; the final section gives the decision for each outcome.

What one broker session binds to #

An MQTT client session is one TCP connection to one broker, negotiated with a client ID, a keep-alive interval, credentials and a TLS configuration. The client ID only has to be unique among the clients of that one broker: identifiers live in a per-broker namespace, so the same ID on two different brokers never collides. Give them distinct IDs anyway, because it makes broker logs and your own tracing readable.

The collision you must never create is the same ID twice against one broker. When a second client connects with an ID that is already in use, the broker closes the older session, and the displaced client drops into a connect-disconnect loop; MQTT 5 names the cause in disconnect reason code 142, session taken over. That symptom, reconnects that immediately drop again, is exactly what two publisher instances sharing one client ID produce, as described in why two Telegraf MQTT inputs share one client ID and disconnect.

Why the protocol answer is not the license answer #

Two different licenses sit over this decision, and each governs only its own product.

The controller's licensed connectivity feature, the one that manages broker connections for you, limits the connections that feature makes. Its connection count says nothing about what the MQTT protocol allows, and it never licenses different software.

An application library is that different software. MQTT Client SL, shipped inside the IIoT Libraries SL package, documents sending messages from a controller to a broker and subscribing by topic, and it is built as client function blocks you instantiate: each instance carries its own broker address, port and connection parameters, so a second broker means a second instance beside the first. Set up the MQTT Client SL library on a CODESYS controller walks through the installation and the first connection. Before you build on that, pin down two things for your exact stack: that your runtime version is in the library's compatibility list, and that the library's license covers the number of instances you need on that device. If the license does not cover the deployment, the supported route is the vendor's connectivity feature, not an unlicensed second instance.

The per-session checklist #

Each item is a setting the second broker needs from its own configuration, not a copy of the first connection's:

  • Client ID: unique on that broker; a duplicate closes the older session.
  • Credentials: that broker's own user and password or client certificate.
  • TLS trust: that broker's root CA and a server certificate whose hostname matches the address you connect to; certificate validity also depends on the controller's clock being right.
  • Keep-alive: an interval the client task can always meet, set per connection.
  • Topic authorization: a broker can accept the login and still refuse a publish on a topic it has not allowed, so a successful handshake does not prove the data path.
  • Publish acknowledgment: a locally accepted publish is not a delivered publish; the broker confirms on its own session, per connection.
  • Reconnect handling: its own backoff and retry state, so a broken path to one broker never resets the other client.
  • Buffer policy: its own queue, sized from your outage budget as the worked example below shows.

Keeping one broker's failure off the other #

Read the process values once per cycle and hand each publisher its own copy of the snapshot; reading them separately for each broker can put different values into supposedly identical messages. Stamp each snapshot with a sequence number or timestamp so the two feeds can be reconciled later.

Call the client function blocks asynchronously and never wait on either broker inside the control task: a broker outage must never stretch control execution, because a watchdog trip on the controller costs far more than a gap in cloud data. Give each publisher a separate bounded queue, and log connection state per client so a fault names its session. A locally accepted publish that never received its broker confirmation is a per-connection failure, the pattern behind why MQTT publish returns error code 4 on your PLC, not a fault in the other client. And when a session does drop, let its own reconnect logic recover it; a client that reconnects and then stays silent has usually lost its subscription or publish path, as in why an MQTT client reconnects but then publishes nothing.

Buffer needed to cover a 30 minute outage at one message per second #

Inputs, all example values for a typical condition-monitoring feed rather than documented limits: the second broker receives one consolidated message per second, each 2 kB, and your outage budget, the longest second-broker downtime the data must survive, is 30 minutes. A disconnected broker receives nothing until it reconnects, so the queue must hold everything published while it is down.

The rule in words: queue entries equal outage seconds times publish rate; queue memory equals entries times message size.

Arithmetic: 30 min = 1800 s. 1800 s × 1 msg/s = 1800 messages. 1800 messages × 2 kB = 3600 kB = 3.6 MB.

Result: the second broker's queue needs about 3.6 MB to carry that outage without losing a message.

Decision: if you can spare 3.6 MB of controller RAM for that queue, keep full history through the outage. The same outage at 10 messages per second needs 18,000 messages × 2 kB = 36 MB; at that rate, switch the second broker to a latest-value policy and keep full history only on the connection whose queue you can afford.

The decision for each case #

  • Your runtime runs a licensed MQTT client library that permits the instances you need. Build the second client as an independent instance with its own client ID, credentials, TLS trust, keep-alive, queue and reconnect handling. That is the supported architecture, governed by the library's own license rather than by the connectivity feature's connection count.
  • Your existing component exposes exactly one connection. Entering the second broker's settings inside it will not open a second session; a single-connection component duplicates configuration, it does not multiply sessions. The supported routes are its licensed multi-connection capability, if it has one, or a separately licensed application library.
  • Neither route's license covers the second session on your device. Then the second broker connection waits for the license, not for a workaround: enable the connectivity feature or a library scope that covers the use. An unlicensed instance is a licensing failure that no amount of correct configuration repairs.

Questions asked

Does the same client ID on two brokers cause a conflict?

No. A client ID only has to be unique among the clients of one broker, so the same identifier on two independent brokers never collides. The failure mode is different: if the same ID connects twice to one broker, the broker closes the older session, and MQTT 5 reports reason code 142, session taken over, to the displaced client. Distinct IDs per broker still make logs easier to trace.

Can both broker connections share one certificate and one credential set?

Rarely. Each broker authenticates on its own terms: its own username and password or client certificate, its own trusted root CA, and a server certificate whose hostname matches the broker address you connect to. A certificate issued for one broker's address will fail hostname validation against the other. Keep the two credential and trust configurations separate so a change for the second broker cannot disturb the working first connection.

How fast does an idle MQTT connection actually drop?

On the keep-alive clock. The client promises a keep-alive interval when it connects; if the broker hears nothing from that client for one and a half times the interval, it closes the connection. A client publishing every second with a 60 second keep-alive stays well inside the limit. A client whose task stalls past 90 seconds does not, even though nothing in its configuration changed.

If both publishers send the same snapshot, do both brokers always receive it?

No. The two deliveries are independent transactions on independent connections: one broker can confirm while the other is unreachable. With QoS 1 the guarantee runs per connection, and the broker confirms each publish with a PUBACK on that session, so a locally accepted publish is not yet a delivered publish. Applications that must reconcile the two destinations need their own sample identity and a delivery record per broker.

Keep reading

  • How-to

    PLC MQTT failover to a redundant second broker

    Yes, with limits. A PLC MQTT client can switch to a second broker without anyone touching it, provided the library supports a second broker destination and you program the trigger, the reconnect sequence and the subscriber-side recovery yourself. Two independent brokers share no sessions, subscriptions or retained messages, so this is failover with a computable gap, not hot standby.

    Edge computing and IIoT

  • Troubleshooting

    Why two Telegraf MQTT inputs share one client ID and disconnect

    Both [[inputs.mqtt_consumer]] blocks carry client_id = "Telegraf", and the MQTT specification allows only one active connection per client identifier. Each connection displaces the other at the broker, producing the EOF and reconnect loop. Give each block a distinct client_id and leave the parsers and topics alone.

    Edge computing and IIoT

  • Troubleshooting

    Fix a PLC MQTT client that never reaches your broker

    Most PLC MQTT failures sit in one of three stages: the broker listener, the network path to the port the client is actually configured for, or the client's own publish logic. Test in that order; a connect timeout points to the first two, a silent publish to the third.

    Edge computing and IIoT

Why the historian query tool keeps freezing after restart