Skip to content
Ikbal.
All notes

Noticing a dead field device in milliseconds, not 60 to 90 seconds

A Modbus gateway only marked a device offline after ten failed polling cycles. Fixing that meant reacting to the first transport error, probing actively, catching half-open sockets, and then untangling the locks, queues and races that a faster failover exposed.

  • Go
  • Modbus TCP
  • Kafka
  • Reliability

Context

The service polls registers from Modbus TCP devices and publishes values and a per-device online status to Kafka. Downstream, operators and alarm logic trust that status. When a device dropped off the network, the gateway kept retrying the poll and only reported it offline after ten consecutive error cycles, which in practice was 60 to 90 seconds of showing stale data as if it were live.

Detect on the first real signal

A timeout on one register block and a reset connection are different kinds of failure. The polling loop now aborts the remaining blocks as soon as it sees a transport error (end of stream, reset, timeout) and publishes the offline status immediately, then hands the device to the reconnect path.

Some failures never produce an error: a cable is pulled somewhere in the middle and the socket stays half-open. Two mechanisms cover that. TCP keepalive at 5 seconds lets the kernel notice a dead peer, and an active probe reads a single register with a short timeout (500 ms by default) so a silent device is detected even between polls. Status is now sent as a continuous heartbeat, so a missing heartbeat downstream is itself a signal.

What faster failover broke

Reacting faster surfaced problems that slow failover had hidden. A probe could time out after the connection had already been replaced, and its late result would mark a healthy new connection as dead. Each connection now carries a generation number, and results from an older generation are discarded.

Reconnect could also re-enter a lock it already held, a recursive deadlock that only appeared when failure handling ran quickly enough to overlap. And dialing a dead device inside the per-client lock meant one unreachable device could stall startup, the Kafka consumers, the pollers and the stats endpoint. Dialing now happens outside the lock and in the background, with a fixed lock order, and the readiness probe reports that startup finished rather than how many devices happen to be connected.

Protecting the message path

When the broker was unreachable, the Kafka client library kept unbounded per-partition queues, and memory grew to several gigabytes. It was replaced by a bounded outbound queue with one drain worker: when the queue is full, telemetry is dropped and counted instead of buffered, so memory stays flat and polling never blocks.

Not all messages are equal in that queue. Status changes are prioritized over telemetry, queued telemetry for a device that just went offline is discarded, and consumer offsets are committed only after the handler succeeds, with bounded retries, so a crash does not silently skip a command.

The protocol parser was hardened at the same time: frame lengths outside the valid range are rejected, partial reads are completed instead of misparsed, and a quantity check that a 16-bit cast could wrap (65,537 becoming 1) now validates before the cast.

What I took away

  • Count failures by kind, not by number. One transport error says more than ten timeouts.
  • Silence needs its own detector. Keepalive and an active probe catch the half-open socket that polling alone never sees.
  • Speeding up failure handling exposes races. Tag every connection with a generation and ignore answers from the past.
  • Never do network I/O while holding a lock that other parts of the service need.
  • Bound every queue and decide in advance which messages may be dropped. Status and commands may not; a stale sensor value can.

Write-ups from production work on industrial gateways. Patterns and measurements only: no code, no client names, no internal identifiers.