Skip to content
Ikbal.
All notes

Rewriting a live gateway without betting on the cutover

A protocol gateway that talks to field equipment was ported to Rust. Instead of switching over and hoping, the new build runs in shadow mode next to the old one, with every write command disabled, while a diff tool compares their output message by message.

  • Rust
  • OPC UA
  • Kafka
  • Migration

Context

The gateway connects OPC UA devices to Kafka and also executes commands such as writing a value or operating equipment. The goal was to align it with the other gateways on the same Rust stack. The behaviour reference was the existing production implementation, not the spec: downstream consumers depend on the exact message format, down to key order, integer versus float, and null versus a missing field.

Byte-level tests against captured traffic can prove the new code matches the old one. They cannot prove the old one was right, and they cannot reproduce what real devices do over days. That gap is what shadow mode is for.

Design that makes shadowing possible

Each device is owned by a single actor that holds the OPC UA client and receives commands through a mailbox. Because only the actor touches the client, a configuration reload can never race a command, and commands reuse the live session instead of opening a new connection each time.

Between the actors and Kafka sits a bounded channel with an explicit policy: value changes are sent with a non-blocking attempt and dropped and counted when the channel is full, because the next change carries a fresher value; status and command responses wait for space, because losing them is not acceptable.

How shadow mode works

In shadow mode the new build reads the same inputs as production but publishes every output with a topic suffix and joins its own consumer group, so it never steals messages from the live service. Every command that writes to a device is rejected with a clear error before any I/O, and this is enforced at two layers so one missed check cannot reach equipment. Read commands still run normally.

The mode flag is fail-closed: it must agree with the topic suffix, and an unrecognised or inconsistent value stops the service instead of letting it start as a second production instance.

Comparing two streams that never line up

Two services observing the same devices do not produce messages at the same instant or in the same order. The diff tool pairs messages by device and address within a time window (5 seconds by default) using broker timestamps rather than payload time, prefers a candidate with an identical normalised value before falling back to the nearest one, and keeps a bounded number of pending messages per key so a stuck side cannot exhaust memory.

Deliberate differences are written down before the comparison starts. One example: the old client never renewed its secure channel, while the new one renews at three quarters of the lifetime the server grants. That changes runtime behaviour but not the message format, and documenting it keeps the diff honest.

Where it stands

The shadow mode, the diff tool and the cutover artifacts (image, manifests with secrets kept out of the applied config, a pipeline that verifies the image) are done. The cutover itself is gated on 72 hours of zero diff in the field, including device disconnects and configuration changes, and that gate has not been run yet. Rollback is redeploying the previous image tag.

What I took away

  • Golden bytes prove "same as before". Only shadow traffic proves "correct in the field". You need both.
  • A shadow that can write to equipment is not a shadow. Block writes in more than one layer and fail closed on configuration.
  • Pair messages by identity and a time window, not by arrival order.
  • Write down intentional deviations before comparing, or every diff becomes an argument.
  • Define the cutover gate in hours and conditions, and do not cut over until it passes.

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