Lesson 4 of 4
Change a Shared Record Shape Safely
Estimated time: 10 minutes
Change a shared record shape without breaking old readers
Two runs that share a record may disagree about its shape, and a change made for a new reader can break an old one that still reads the previous shape. The break is not in the writer, which keeps producing valid records, but in the reader that no longer understands them.
Add, do not rename
A new field that old readers can ignore is a safe change. Renaming a field in place is two changes at once, an addition and a removal, and the removal is what breaks the reader that still asks for the old name.
Readers ignore what they do not know
A reader that fails on an unknown field cannot survive any change. Teach every reader to ignore fields it does not read, so an addition is invisible to it.
def read(record, known):
view = {k: record[k] for k in known if k in record}
extra = [k for k in record if k not in known]
return {"view": view, "extra": extra}
Remove last, not first
A field is safe to remove when no reader reads it, and that is a fact you check rather than assume. Removing first and reading the failure later reverses the order in which the evidence arrives.
Keep the writer able to produce the old shape
During a change, have the writer emit both the old and the new field. That costs one field for the length of the change and lets every reader keep working while they move at their own pace.