Skip to content
Wade Womersley

wade.one

wade womersley – york based software engineer

  • Home
  • 2026
  • November
  • 13
  • Optimistic Locking Stops an Old Tab Overwriting New Work

Optimistic Locking Stops an Old Tab Overwriting New Work

Posted on November 13, 2026November 12, 2026 By
Programming

An old browser tab should not quietly overwrite somebody else’s work. If two people open the same record, the second person to save may be submitting a perfectly valid form based on information that is no longer current. Validation will happily accept it. The person whose changes disappear will be rather less happy.

For a record edited as a whole, I like an explicit version number. The client reads version 7 and sends that version back with its changes. The database update checks the version and increments it in the same statement. Here is the basic shape in PostgreSQL, assuming an integer version column:

UPDATE projects
SET name = $1,
    version = version + 1
WHERE id = $2
  AND version = $3
RETURNING id, name, version;

If another writer has already moved the record to version 8, this update changes no rows. PostgreSQL’s Read Committed behaviour also matters when the writes overlap: after waiting for a concurrent updater to commit, it checks the condition against the updated row. The check belongs inside the write. Reading the version, comparing it in application code and then running an unconditional update leaves a gap.

That query is the concurrency part, not the entire endpoint. Keep the usual authorization checks and tenant boundary. A zero-row result needs handling according to the API’s rules, since a missing or inaccessible record can also produce no result. Every path that changes the protected record needs to maintain the version too, including background jobs and administrative tools.

The interface has work to do when there is a conflict. Keep the person’s unsaved changes, explain that the record changed and let them compare or reload. Automatically fetching the latest version and resubmitting the old form defeats the protection. It turns a conflict into a silent overwrite with an extra request.

This is useful where overwriting an edit would matter; a live counter may need a different operation entirely. I would test it with two clients reading the same version and then saving different values. One save should succeed, and the other should get a result the person can actually recover from.

Share:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit

Related

Comments

comments

Tags: concurrency optimistic-locking postgresql

Post navigation

❮ Previous Post: GitHub Actions Dependencies Deserve a Lockfile Too
Next Post: A Backup Is More Convincing After You Have Restored It ❯

You may also like

Programming
#PHPUK2010 Part 1
February 26, 2010
Programming
An API Timeout Does Not Mean the Operation Failed
October 14, 2026
Programming
Android 17 Makes Adaptive Layouts the Default
August 19, 2026
Programming
Node.js 26 Finally Makes Temporal the Normal Date API
August 21, 2026
  • AI
  • artificial intelligence
  • Ego-centric
  • Events
  • Films
  • Food
  • Gaming
  • Gym
  • Hardware
  • Holidays
  • News
  • PHP
  • Programming
  • Random Stuff
  • Reviews
  • Science
  • SEO
  • Software
  • Software Engineer
  • Support
  • Uncategorized
  • Work

Categories

  • AI
  • artificial intelligence
  • Ego-centric
  • Events
  • Films
  • Food
  • Gaming
  • Gym
  • Hardware
  • Holidays
  • News
  • PHP
  • Programming
  • Random Stuff
  • Reviews
  • Science
  • SEO
  • Software
  • Software Engineer
  • Support
  • Uncategorized
  • Work

Archives

  • November 2026
  • October 2026
  • August 2026
  • July 2026
  • June 2026
  • May 2026
  • April 2026
  • March 2026
  • August 2023
  • April 2023
  • March 2023
  • April 2016
  • October 2015
  • July 2015
  • June 2015
  • May 2015
  • March 2015
  • February 2015
  • January 2015
  • October 2014
  • December 2013
  • September 2013
  • June 2013
  • October 2012
  • May 2012
  • March 2012
  • February 2012
  • April 2011
  • March 2011
  • October 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009

Copyright © 2026 wade.one.

Theme: Oceanly News Dark by ScriptsTown