Building a Virtual Factory: A Reproducible Testbed for Industrial Data | UNS to Lakehouse — Part 2
From the publisher’s feed, collected in the ZeroLabs newsroom.
You can’t benchmark a factory you don’t have. And even if you have one, you can’t ask it to break down on Tuesday at 10:20, lose its network for six minutes, and do it all again tomorrow for the next experiment.In Part 1, we looked at the missing link between the Unified Namespace and the lakehouse, and the questions this series will try to answer with real measurements. Before we can measure anything, we need data that behaves like the real thing, and we need to be able to produce exactly the same data again and again. So in this part, we build a virtual factory.Why Not Just Generate Random Numbers?Random numbers are easy. Realistic factory data is not. The problems we want to study only show up if the simulator reproduces how plants actually behave:A hierarchy: Every value belongs to a site, area, line and machine.Report-by-exception: Devices publish a value only when it changes enough, so silence is normal.Machine faults: Machines stop, and related signals react together.Stuck sensors: A sensor freezes at its last value while the machine keeps running.Network outages: An edge gateway loses its connection, buffers data and sends it late.Slow wear: Some signals drift over days, which is what predictive maintenance looks for.If any of these are missing, our experiments would be measuring an idealized world, and the results wouldn’t survive contact with a real plant.The PlantOur virtual factory is a small bottling plant, structured the way most Unified Namespace designs are: site / area / line / machine / metric.acme-plant├── filling-area│ ├── line-1 filler, capper, labeler│ └── line-2 filler, capper, labeler└── packaging-area └── line-3 case-packer, palletizerThat’s 8 machines publishing 37 signals. The entire plant lives in a single YAML file, so growing it to 50 machines or adding a new line is a configuration change, not a code change:machine_types: filler: [pressure, temperature, speed, vibration, count, state] capper: [temperature, speed, vibration, count, state]areas: filling-area: line-1: [filler, capper, labeler] line-2: [filler, capper, labeler] packaging-area: line-3: [case-packer, palletizer]Making Signals BehaveEach sensor follows one of three simple models:Gauges (pressure, temperature, speed, vibration) wander around a target value and are constantly pulled back towards it, like a real controlled process. Each one has its own noise level and its own “pull strength”. Vibration also carries a slow upward trend to simulate wear.Counters (bottle count) only grow, and only while the machine is running.State (RUNNING / FAULT) is published whenever it changes.When a machine faults, its signals react the way you would expect: speed falls to zero, filler pressure drops towards 120 mbar, vibration fades to background noise, and the counter stops. When the fault clears, everything recovers on its own.Report-by-Exception: Where Silence BeginsReal devices rarely publish every sample. They publish a value only when it moves more than a configured deadband, plus a heartbeat after a period of silence so subscribers know the device is still alive:pressure: kind: gauge mean: 800 # mbar deadband: 2.0 # publish only if the value moved at least 2 mbar interval: 1 # sample every second max_silence: 60 # ...but publish at least once a minuteThe effect is significant. In one simulated day, the factory took 2,090,880 samples but published only 1,158,046 of them, about 55%. The other 45% were never sent. That’s efficient for the network, but it means a gap in the data doesn’t automatically mean anything is wrong. Keep this in mind; it comes back in a big way later in the series.When Things Go WrongThree kinds of trouble are built in, each with its own random schedule:Machine faults: On average a few per machine per day, lasting minutes.Frozen sensors: Occasionally a sensor gets stuck. The machine keeps running, but the sensor keeps reporting the same value through its heartbeats. From the outside, everything looks fine.Network outages: A line’s edge gateway loses its connection for a few minutes. It keeps collecting data, buffers it, and delivers it after reconnecting. This is called store-and-forward.Outages are where things get interesting for data engineers. Every reading carries two timestamps: when it was measured and when it was delivered. During an outage, these drift apart by minutes. Any pipeline that relies on arrival time will put these readings in the wrong place. In our simulated day, 22,601 readings arrived late this way.Speaking MQTT: Two FlavoursThe factory can publish its data in two ways.Plain UNS with JSON payloads, one topic per signal:acme-plant/filling-area/line-2/filler/pressure {"value": 812.4, "unit": "mbar", "timestamp": 1767592800000}Sparkplug B, the more structured option many plants use. Each line becomes an edge node, and each machine becomes a device:group_id → site: acme-plantedge node → area + line: filling-area.line-2device → machine: fillerSparkplug adds a small but important protocol on top of MQTT. To see it in action, I wrote a small listener that decodes the traffic. Here’s what happens when the factory starts:NBIRTH filling-area.line-2 bdSeq=0 seq=0DBIRTH filling-area.line-2/filler seq=1 aliases: pressure=1, temperature=2, speed=3, vibration=4, count=5, state=6DDATA filling-area.line-2/filler seq=4 pressure=802.553@06:00:00.000 speed=120.785@06:00:00.000 ...A few things are worth noticing:NBIRTH announces the edge node and carries a bdSeq number, the “birth/death sequence”.DBIRTH introduces every metric with its name, its data type and a short numeric alias.DDATA messages carry only the aliases, not the names. That keeps messages small, but it means any consumer that missed the birth messages simply cannot tell which value is which.seq counts every message from an edge node from 0 to 255, so subscribers can detect lost messages.Now watch what happens at 10:20, when line 2 loses its network connection:NDEATH filling-area.line-2 bdSeq=0... (line 2 is silent for about six minutes)NBIRTH filling-area.line-2 bdSeq=1 seq=0DBIRTH filling-area.line-2/filler seq=1 aliases: pressure=1, temperature=2, ...DDATA filling-area.line-2/filler seq=6 count=28614@10:20:36.000 (historical) pressure=804.808@10:20:37.000 (historical) ...The broker publishes the node’s death certificate (NDEATH). When the gateway comes back, it is reborn with bdSeq=1, announces its metrics again, and then flushes six minutes of buffered data, every value flagged as historical. The timestamps say 10:20, but the data arrived after 10:27.This is exactly the kind of situation our future connector must handle correctly.All of this comes from a small, dependency-free Sparkplug B encoder and decoder in the project. Writing it by hand turned out to be a great way to understand the protocol, and it’s short enough to read in one sitting.Reproducible by DesignEvery random decision in the simulator, from sensor noise to fault times to outage lengths, derives from a single seed in the config file. Same config, same seed, same data. Always.I tested this on two very different machines: my MacBook and a Linux server in the cloud. Both produced exactly 1,158,046 readings for the same simulated day, with the same faults at the same seconds.The simulator also writes a ground truth file next to the data: every fault, frozen sensor and outage that really happened, with exact start and end times. This turns out to be very valuable. Later in the series, we won’t just ask “how fast is this pipeline?” but also “how correct are its results?”, and the ground truth is what we’ll compare against.Try It YourselfEverything in this post is in the project repository: https://github.com/BuseKaylan/uns-to-lakehouseTo generate a full day of factory data as Parquet, no broker needed:make installmake dataset # one simulated day in about 13 seconds on my laptopTo watch the live factory, start a local MQTT broker and run the simulator and the listener side by side:mosquitto -c docker/mosquitto.conf.venv/bin/unslake simulate --mode sparkplug --duration 1d --speed 60make listenAt 60x speed, a full factory day flows by in 24 minutes, outages included.What’s Next?Now that we have a factory that never gets tired, we can start measuring. In Part 3, we’ll take one simulated day and store it in several formats: raw MQTT messages as JSON, CSV, and Parquet with different compression settings, sort orders and encodings. The question is simple: how much space does sensor data really take? Some of the early results surprised me.Wrapping UpA good experiment needs a good test environment. Our virtual factory reproduces the behaviours that make industrial data hard: a deep hierarchy, report-by-exception silence, faults, stuck sensors and late data after network outages. It speaks both plain MQTT and Sparkplug B, produces exactly the same data every time, and records the truth so we can check our answers later. With that foundation in place, the measurements can begin.Thanks for reading, and see you in Part 3.Building a Virtual Factory: A Reproducible Testbed for Industrial Data | UNS to Lakehouse — Part 2 was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.