Skip to content
Commitwise
Menu

Optimistic locking

Detecting that someone else changed a record since you read it, and failing your update instead of overwriting theirs.

Definition

Each row has a version number. An update includes the version it was based on (where id = ? and version = ?) and increments it. If another transaction changed the row in the meantime, no row matches, and the update fails with a conflict that can be retried or shown to the user. In JPA you add a @Version field. It suits low-contention edits; for hot rows, atomic updates or pessimistic locks work better.

In practice

Two admins edit the same product; the second save fails with 409 Conflict instead of silently undoing the first admin's change.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.