IMD SolutionComputing LibraryShop main store

Troubleshooting

Why a Sparkplug B node drops when the PLC task loads the CPU

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

Short answer

On a compact controller running a hard real-time PLC task plus Node-RED, a recurring Sparkplug B disconnect is usually a missed MQTT keep-alive window: CPU utilization spikes to 100% on a roughly one-minute cycle and the broker declares the session dead. Measure with htop, correlate timestamps, lengthen the cyclic task interval as a diagnostic, and move Node-RED to its own box if headroom never recovers.

On this page
  1. Decide first: who ends the session
  2. Measure utilization instead of guessing
  3. The keep-alive arithmetic
  4. Lengthen the cyclic task interval as a diagnostic
  5. When to stop tuning and move Node-RED
  6. The decision
  7. Questions asked

A Sparkplug B node that disconnects on a repeating cycle from a compact PLC controller is almost always losing its MQTT keep-alive window to CPU contention: the real-time PLC task plus periodic kernel housekeeping pushes utilization to 100%, the client cannot send traffic in time, and the broker declares the session dead. The fix is measured, not guessed: capture utilization with htop, correlate disconnect timestamps against the spike pattern, then either lengthen the cyclic task interval, tune the keep-alive, or move Node-RED to a separate edge box. This covers the case no broker-side setting explains — the network is clean, the broker is healthy, and the drops still come back.

Decide first: who ends the session #

Before touching anything, establish which side ends the MQTT session, because the remedies do not overlap. Check three things in this order:

  • The Node-RED Sparkplug node status: does it report disconnected, and at exactly which timestamp?
  • The broker connection log for that client ID: does the broker log a keep-alive timeout or session takeover before the node reacts?
  • The network path: does ICMP loss appear at the same times, or only MQTT?

If the broker logs the disconnect first and the timestamps align with CPU spikes on the controller, you have the CPU-contention fingerprint. If the broker logs a session takeover, look for a duplicate client ID elsewhere — see why a broker rejects your Sparkplug client over its STATE will for the connect-time negotiation side. If the network itself drops, nothing on the controller will fix it.

Measure utilization instead of guessing #

Node-RED's MQTT client stack is stable over long uptimes, so a repeating disconnect pattern on a small embedded controller points at resource starvation. Capture it:

  1. Enable SSH on the controller if it is not already on.
  2. Open a session from a workstation on the same subnet as the root user, then run htop.
  3. Leave it running at least 3 to 5 minutes so you capture several housekeeping cycles. Watch the per-core meters at the top; press F6 and sort by PERCENT_CPU, or sort by memory, to find the dominant consumers. Expect the PLC runtime process and the Node.js process at the top.
  4. Record the disconnect timestamps: run Node-RED's log in the same session, or timestamp a debug node on the Sparkplug node's status output.
  5. Check free memory with free -m; low free memory with swap activity means memory starvation, not just CPU.

If htop is absent, top -d 1 shows the same picture. Three numbers decide the case:

Metric What to note What it means
Baseline CPU Steady value with both workloads running A high baseline leaves no margin for transients
Spike period Interval between 100% excursions About one minute suggests kernel housekeeping
Spike duration vs keep-alive How long the stall lasts A stall past the keep-alive window kills the session

The same measurement discipline applies to a Node-RED agent that runs slow without dropping — see troubleshooting high CPU usage in a Node-RED agent.

The keep-alive arithmetic #

MQTT keep-alive is negotiated at connect. The broker may end the session if it receives nothing — publish, subscribe or PINGREQ — within one and a half times the keep-alive interval. With the common 60-second keep-alive, that window is 1.5 × 60 s = 90 s of total silence.

Worked example: your htop capture shows housekeeping stalls of 15 seconds each, once per minute. On its own, a 15-second stall fits inside the 90-second window and costs nothing. The failure mode is compounding: the PLC task at a 10 ms cyclic interval takes most of each cycle, Node-RED gets the remainder, and a housekeeping stall lands on top of an already starved scheduler. If the client's PINGREQ is delayed past 90 seconds of accumulated starvation, the broker disconnects, the Sparkplug state machine forces a fresh NBIRTH on reconnect, and the cycle repeats. Lengthening the keep-alive to 120 s moves the deadline to 180 s and buys margin, but it is a tolerance increase, not a capacity fix — the underlying starvation stays.

Lengthen the cyclic task interval as a diagnostic #

The direct lever on PLC-side CPU demand is the cyclic task interval in the runtime's task configuration — the place where task type and interval are set, under the application's task list in your engineering environment. Some runtimes even expose functions to read and change the current cycle interval while the application runs, which makes the test fast.

For a diagnostic, set the main cyclic task's interval to 500 ms, download, and rerun htop for another 3 to 5 minutes. Read the result:

  • Baseline CPU drops sharply and disconnects stop: the PLC task was the dominant load. Find the longest cycle time your process tolerates and hold there.
  • CPU drops but disconnects continue on the roughly one-minute cadence: the housekeeping stall is the trigger. Increase keep-alive tolerance and plan a hardware move.
  • CPU barely changes: Node-RED itself is the dominant consumer. Cut the flow's work — publish rates, polling breadth, string handling — or move it off the controller.
  • Free memory stays low regardless: the box is at its memory ceiling and cannot host both workloads. Migrate.

One safety note: do not leave a diagnostic interval in production blind. A longer cyclic task changes the timing of every timer, edge detection, PID sample and interlock in the program; validate each against the new interval first, and if any logic needs fast execution, split it into its own short-interval task and move only aggregation and payload assembly to the slow one.

When to stop tuning and move Node-RED #

If the controller has no headroom to spare — baseline stays near 100% or free memory never recovers after the task change — tuning is a stopgap, not an architecture. Running a Node.js flow engine beside a production hard real-time application on a compact controller asks one silicon package to do two jobs with different deadlines. Move the data work instead: keep the PLC task on the controller and host the flows on a separate fanless mini PC sized for the combined load, which also gives Node-RED its own memory envelope and restart behavior. For the placement decision — broker in the PLC, on the edge box, or in the plant network — see running an MQTT broker in the PLC or on a separate device, and why an uncaught ECONNRESET stops Node-RED when a PLC drops for the handling your flows need once the two no longer share a host.

The decision #

  • Disconnects align with one-minute CPU spikes and the baseline drops when you lengthen the task: it was PLC-task load. Set the interval to the longest value your process tolerates, verify all timing-dependent logic, done.
  • Disconnects persist after the task change: it is the housekeeping stall against a starved scheduler. Raise the keep-alive and schedule the move of Node-RED to its own edge box.
  • Free memory stays low no matter what: the controller is over-subscribed. Migrate; do not tune.
  • The broker, not the controller, logs the problem: duplicate client ID or broker policy, not CPU. Fix that side instead.

Questions asked

How long can a Sparkplug B client stay silent before the broker disconnects it?

The MQTT keep-alive timer is negotiated at connect time. With the common default of 60 seconds, the broker may declare the session dead if it receives no packet, including a PINGREQ, within one and a half times that value, so 90 seconds of total silence ends the session. A CPU stall longer than that window produces a disconnect even when the network and broker are healthy.

Can I leave the PLC task at 500 ms permanently?

No. Treat 500 ms as a diagnostic only. Before any longer interval goes into production, validate every timer, edge-detection function, PID sample assumption and interlock timing against the new interval. If fast logic must stay fast, split it into its own short-interval task and move only aggregation, string handling and payload assembly to the slow task.

How do I tell a CPU problem from a broker or network problem?

Check which side logs the disconnect first. If the broker logs a keep-alive timeout or session takeover before the Node-RED node reacts, look at broker policy, duplicate client IDs and broker resources. If the network path itself drops, ICMP shows loss alongside MQTT. If only MQTT drops and the timestamps line up with utilization spikes on the controller, it is CPU contention.

Does a rebirth after every reconnect lose data?

The Sparkplug state machine forces a fresh NBIRTH and full DBIRTH set on every reconnect, so the broker rebuilds the metric snapshot each time. That is overhead on the broker and the network, and it is one more reason to fix the disconnect cause instead of letting the node cycle: each drop repeats the whole birth sequence.

Keep reading

  • Troubleshooting

    Why MQTT Engine shows online status after an edge node drops

    An Info/Online tag that stays true with Good quality is not proof the edge node is alive: Engine's online state comes from the node's Sparkplug death certificate, and earlier Engine versions marked tags stale only on Engine shutdown. Read Info/Online, tag quality and Store & Forward stored metrics as three separate indicators, retest on Engine 4.0.8 with Ignition 8.0.16 or later, and compare against a node that reports correctly before escalating.

    Edge computing and IIoT

  • How-to

    Modbus TCP from a third-party PLC to cloud MQTT on IOT2040

    Yes. Modbus TCP has no vendor lock: any PLC that runs a Modbus TCP server on port 502 can be polled by an IOT2040, whatever its maker. One Modbus client node in Node-RED reads the PLC on X1 and one MQTT out node publishes on X2 to the broker over TLS on port 8883; OPC UA is needed only if the PLC itself runs an OPC UA server.

    Edge computing and IIoT

  • Troubleshooting

    Sparkplug B sends data but receives no SCADA commands

    Sparkplug B telemetry that reaches SCADA while SCADA writes never arrive is usually a direction-of-function fault: the open-source IEC SparkplugB library V1.0.0.0 implements only sending controller data to SCADA. Verify the broker path, session and authorization, watch the 30-minute demo window of the MQTT Client SL dependency, and choose a documented receive-capable implementation when writes are required.

    Edge computing and IIoT

Why InfluxDB shows a periodic CPU peak on an edge controller