Detect a Stale Digital Twin in Twenty Minutes¶
You will build a staleness monitor for a simulated digital twin: a script that watches a twin's state file and says, on every read, whether the value can be trusted. By the end you will see the monitor catch a simulated link failure that a naive reader would have missed.
You need Python 3.10 or newer and a terminal. Nothing else.
1. Simulate a twin's state file¶
A twin's live state, reduced to the smallest thing that can go stale: a
file holding one value and the time it was written. Create twin.py:
1 2 3 4 5 6 7 | |
Run it in one terminal and leave it running:
1 | |
2. Read the state naively¶
In a second terminal, create read_naive.py:
1 2 3 4 5 | |
1 2 | |
It prints a temperature every time -- including, as you are about to see, when the value is minutes old.
3. Mark staleness instead¶
Create read_marked.py. The only change is that the reader compares
the write timestamp with its own clock and refuses to present a stale
value as current:
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 | |
4. Break the link, and watch the difference¶
Stop twin.py with Ctrl-C -- this is your link failure. Wait ten
seconds, then run both readers:
1 2 3 4 5 | |
The naive reader is confidently wrong: it presents an eleven-second-old value with no hint that the twin has stopped tracking reality. The marked reader gives you the same information and the honesty to use it.
Restart twin.py and run read_marked.py again to see it recover on
its own -- staleness is a property of the read, so no reset is needed.
5. Where you are¶
You have a twin, a link failure, and a reader that cannot be fooled by one. The pattern -- carry the write time with the state, and make every read compare it against a staleness budget -- scales unchanged from this one file to a production state stream.
Where to go next¶
Explicit staleness marking roughly halved operator mistrust incidents in a comparative study of synchronisation strategies [@sample_dt_sync_2023], and the factory case study shows what silent staleness costs when it is not marked: both of its recorded failures were divergences nobody was told about [@sample_dt_factory_2022]. For the vocabulary of what you just built -- and why it is a shadow, not yet a twin -- see [@sample_dt_overview_2024].
References¶
[1] C. Chen and D. Devi, "State Synchronisation Strategies for Operational Digital Twins," Synthetic Sample Papers, vol. 1, pp. 6–11, 2023. sample_dt_sync_2023
[2] E. Eriksen, "A Digital Twin on the Factory Floor: an Eighteen-Month Case Study," Synthetic Sample Papers, vol. 1, pp. 12–17, 2022. sample_dt_factory_2022
[3] A. Author and B. Builder, "Digital Twins: Definitions, Distinctions, and a Short Taxonomy," Synthetic Sample Papers, vol. 1, pp. 1–5, 2024. sample_dt_overview_2024