Definition
Live state transfer is the step of a live update, a replacement of code in a running program, that converts the old version’s data in memory into data the new version can use. A representation invariant is a condition that a data structure’s stored values must satisfy for the code using them to work, including what each value means, such as its unit. A transfer is correct when it turns every valid old state into a new state that satisfies the new invariant and means the same thing.
Where it fits
Main entry: Schema migration (§8.36).
The Schema migration entry treats this problem for stored records; live state transfer meets it in memory that a running program is using. Let an abstraction function map a version’s stored bits to the value they stand for: for the old version and for the new one. Let be the transfer. For every old state that satisfies the old invariant, must satisfy the new invariant, and . The condition has the shape of the simulation relation of a refinement proof: it pairs each old state with a new state that stands for the same value.
A change of unit shows that copying bits can change meaning. The old code stores a timeout §9.34 of 5, meaning 5 ms; the new code reads the same field in microseconds. Copied unchanged, the value means 5 microseconds, 1000 times shorter. The correct transfer multiplies by 1000, because 1 ms is 1000 microseconds: microseconds. If the field is a 16-bit unsigned integer in both versions, its largest value is microseconds, about 65.5 ms. An old timeout of 70 ms would need microseconds, more than 65,535, and so has no valid image, so the transfer must refuse that state, or the new version must widen the field.
Serialization §4.28, the conversion of values into bytes by a fixed format and back, carries each stored value across unchanged, not its meaning, so it cannot detect the change of unit. A transfer must also handle what is not a plain value.
New fields need values consistent with the rest of the state, not merely a default.
Pointers must lead to the corresponding new objects.
Ownership, the duty to release a resource, must stay with exactly one owner for each resource.
Operations that the old code started, such as armed timers, must complete in a way the new code accepts.
Other threads must see a coherent snapshot §9.23: the whole old state or the whole new state, never a mix.
Limits and common mistakes
The transfer may need extra memory while the old and new state exist side by side. If a fault or a reset can interrupt the transfer while some state survives, for example in flash memory §6.6, the interruption must leave the complete old state or the complete new state; building the new state aside and switching to it in one atomic step achieves this.
Questions answered
Can serialization automatically solve state transfer?
No. Serialization converts values into bytes by a fixed format and back; it carries each stored value across unchanged, but not the value’s meaning, the conditions linking values, or the program’s live relationships. If the old version stores a timeout of 5 meaning 5 ms and the new version reads the field in microseconds, serializing and restoring it yields 5 microseconds, 1000 times too short, whereas the correct transfer computes microseconds. Serialization also does not fill new fields consistently, redirect pointers, keep each resource with one owner, carry over operations in progress, or hide a half-converted state from other threads.
Live state transfer, the conversion of a running program’s state for its new code, therefore needs an explicit mapping. The mapping takes each valid old state to a new state that satisfies the new code’s representation invariant, the conditions its data must meet, and that stands for the same value. Old states with no valid image must be rejected, and the switch must look atomic to every observer. Serialization can be a tool inside the mapping, never the whole of it.
Related entries: Ownership (§4.2), Coherent snapshot (§9.23), Refinement (§3.32).