Troubleshooting
Why a Sparkplug B node drops when the PLC task loads the CPU
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
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:
- Enable SSH on the controller if it is not already on.
- Open a session from a workstation on the same subnet as the root user, then run
htop. - 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.
- 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.
- 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.


