Skip to content

.tip

· one of the small things

A version column turns a lost update into a conflict

Two tabs open the same record, both read version 7, both save, and the second write quietly overwrites the first. Keep a version on the row and let the update assert the one that was read.

UPDATE documents SET body = $1, version = version + 1
WHERE id = $2 AND version = $3;

Zero rows affected means somebody else got there first. Answer 409 with the current row so the client can show what changed, instead of a save button that reports success and drops the edit.

distributed-systemsschema-design