A 1000-device chaos test and the reconnect storm it exposed
Before a protocol gateway went near production, I built a harness that kills, freezes and flaps 1000 simulated field devices. It found a synchronized reconnect herd that dropped 710 messages, a status flood, and a leaking metric. Jitter fixed the drops and cut median recovery in half, but it made the slowest device slower.
- Rust
- OPC UA
- Kafka
- Chaos testing
Context
The service is a gateway between OPC UA field devices and Kafka: one actor per device holds the session, subscribes to its data points, and publishes changes and connection status. It had just been rewritten in Rust, and happy-path tests said nothing about what happens when a whole site loses its network at once.
So the question was concrete: when hundreds of devices fail at once, how fast do we notice, how fast do we recover, and do we lose anything on the way back?
The harness
Everything ran locally: 1000 simulated OPC UA servers with 20 changing nodes each, a local Postgres for configuration, and PocketKafka, my own Kafka-compatible broker, as the message bus. A tap consumer timestamped every status and data message so detection and recovery could be measured per device, not eyeballed from logs.
Six scenarios: hard kill and silent freeze on 10% of devices, the same on 100% of devices for 30 seconds, and two five-minute runs of random flapping (5% of devices every 10 seconds), one with hard closes and one with a proxy that silently blackholes traffic. Each run also recorded CPU, memory, file descriptors, database connections, socket states and the service's own gauges.
What it found
Silent failures were handled well: a 5-second health-read timeout bounded detection to about 5 seconds, sockets closed cleanly, and all 1000 devices came back with zero stuck sessions. Database connections stayed at 4 or fewer through every storm, so reconnects never fanned out to Postgres.
The problems were about synchronization. Reconnect backoff was exponential but had no jitter, so after a 30-second outage every device retried at the same moments and all 1000 recovered inside a 43 ms window. In the freeze-and-resume scenario, that herd flushed into the bounded publisher queues at once and 710 messages were dropped.
Two more findings: the service re-sent an offline status on every failed connect attempt instead of only on change, which produced about 10,000 status messages per minute during a full outage. And the subscription gauge was incremented on every reconnect but never decremented, so after storms it reported up to 2.5 times the real number of subscriptions.
The fixes
Three changes, all in the per-device actor: backoff now starts at 1 second, doubles to a 15-second cap, and carries ±25% jitter; status and its paired log event are emitted only when the state actually changes; and the subscription counter is tied to a guard that decrements it when the session is torn down.
Then the same scenarios were run again with the same parameters.
| Measurement | Before | After |
|---|---|---|
| Messages dropped after freeze-and-resume | 710 | 0 |
| Median recovery after 30 s hard outage | 7.0 s | 3.9 s |
| Time until all 1000 devices are back | 7.1 s | 18.3 s |
| Status messages per minute during full outage | ~10,000 | ~4,000 |
| Subscription gauge vs live sessions | 2,000 vs 1,000 | 1,000 vs 1,000 |
What I took away
- Exponential backoff without jitter is a metronome. Lockstep recovery looks fast in a dashboard and is exactly what overflows a bounded queue.
- Jitter is a trade, not a free win: the median halved, but the last device now waits longer. I report both numbers because the slowest device is the one an operator notices.
- Emit state on change, not on attempt. A status topic that repeats itself during an outage hides the one message that matters.
- Measure from the outside. The tap consumer and socket snapshots caught a metric that was lying about itself.
Write-ups from production work on industrial gateways. Patterns and measurements only: no code, no client names, no internal identifiers.