Optimistic locking with versions 00:00:01 Two editors open version seven Ada opens a market listing and starts correcting its title. The database gives her the fields and a version number, seven. She can spend a minute writing without holding a database lock for that whole minute. Bo opens the same listing while Ada is thinking. He sees version seven too. Even when they intend to change different fields, saving a whole old copy can erase somebody else's work. Ada submits her change with the version she read. The database changes the title and advances the stored version from seven to eight in the same conditional operation. Ada now has a confirmed new revision. 00:00:42 Bo brings an older promise Bo's update says to change the listing only if its version is still seven. The condition is false. The database changes zero rows, giving the application a way to detect the conflict. A response that silently reports success would leave Bo believing his work was saved. Return a clear version conflict or failed precondition and keep his proposed change available for review. Bo reads the current version, sees Ada's corrected title and can decide whether his description still applies. A retry must use this new evidence; sending the same stale comparison again cannot succeed. 00:01:24 What is actually atomic? The version check belongs in the write, such as a database update restricted by both listing identifier and expected version. Reading the version and later issuing an unconditional write recreates the original race. Imagine two saves racing from version eight. With the expected version enforced at the shared row, only the first successful change advances it. The second cannot both match and overwrite the same old revision. An HTTP precondition can carry a suitable validator, but the handler still has to enforce it against authoritative state. Comparing a cached copy at the web server would leave a gap at the database. 00:02:05 A merge is a product decision In our case the title and description can often be merged after checking the current listing. If both editors changed the same price in different directions, an automatic merge could invent a price neither one intended. A busy item can invalidate fresh versions repeatedly. Back off, bound retries and make the conflict visible. Under persistent contention, a short lock or a designated writer may better fit this product. A version on one listing protects changes to that listing. It cannot by itself enforce a rule spanning several stock records. Choose a boundary that includes every fact the business decision depends on. 00:02:46 Try the edits again Bo submits the reviewed description against version eight. If nobody else has changed it, the update succeeds and advances the version to nine. Ada's title remains in the resulting record. A test can pause two clients after reading the same version, then release their saves together. Check the success, the conflict and the final content, not merely the number of responses returned. The version number says which state an editor actually saw. Comparing that evidence at write time lets the database refuse a stale promise instead of quietly turning a successful save into a lost edit.