Concurrent record editing in web applications: how to prevent lost updates
Two colleagues open the same order, each changes something different and both click Save. If the application simply writes the whole form, the second save silently overwrites the first. Nobody sees an error, the change just disappears. Concurrent record editing is therefore a common but often overlooked problem in internal systems, CRMs, booking and order management applications.
The short answer: a web application should not rely on the rule “last save wins”. Every record should carry a version, and the server should reject a save if the record has changed since the form was opened. The user then gets a clear conflict screen instead of silent data loss. Exclusive record locking and short database locks belong only where they genuinely make sense.
What a lost update is and why it usually goes unnoticed
The HTTP specification calls this the “lost update” problem: one client accidentally overwrites the work of another client that has been acting in parallel (RFC 9110, section 13). A W3C note describes it with a simple story: Ron and Shirley both load the same document, Ron saves his edits, Shirley then saves hers, and Ron’s changes are lost because she never saw them (W3C Note: Detecting the Lost Update Problem).
The problem is insidious because it does not raise an error. The application reports both saves as successful. The difference only shows up later, for example when goods are shipped to the old address or a customer receives a quote with the original price.
Why the database alone will not solve it
Database transactions protect operations that take a fraction of a second. Editing a form, however, takes minutes and consists of two separate requests: loading and saving. The Ruby on Rails documentation on optimistic locking therefore explicitly recommends adding the version number to the form as a hidden field so that the check works across web requests (Rails API: Optimistic Locking). The same principle applies in any framework.
A practical example: an order edited by two people
Illustrative example, not a real client case.
- At 9:00, Jana from sales opens order no. 1042 because the customer called with a new delivery address.
- At 9:02, Peter from the warehouse opens the same order and changes the dispatch date.
- At 9:05, Jana saves the new address.
- At 9:07, Peter saves a form that still contains the old address. Without a version check, Jana’s change is overwritten.
With a version check, the server rejects Peter’s save and the application shows him: “Jana edited this order at 9:05 in the meantime. The delivery address has changed.” Peter sees the current state, re-enters only his dispatch date and saves. Both changes are preserved.
Solution options and when to use each
The W3C distinguishes two main approaches: non-blocking editing with automatic conflict detection on save, and reserved editing, where only one person can edit a record at a time. Which one fits better depends mainly on whether the changes are easy to merge and how long the editing takes (W3C Note).
| Approach | When it fits | Main risk |
|---|---|---|
| Last save wins | Data edited by only one person, such as personal settings | Silent loss of changes once more users get involved |
| Version check on save (optimistic locking) | Most forms: orders, customers, quotes, contracts | Users occasionally have to resolve a conflict |
| Exclusive editing (record reservation) | Long texts or documents that are hard to merge | Forgotten locks block colleagues; you need a timeout and lock management |
| Short lock or atomic operation in the database | Numbers changed by many people at once: stock, capacity, credit balance | Waiting under high load; transactions may need to be retried |
Even an exclusive lock does not remove the need for a version check. The W3C points out that in practice locks get released after a timeout or by an administrator, because people forget them or lose connectivity, so conflict detection is still necessary.
How to introduce version checks in 6 steps
1. List the records edited by several people or systems
Start with those where a lost change has a financial or customer impact: orders, quotes, customer records, appointments and stock items. Include imports, integrations and automations that write to these records too.
2. Add a version number to each record
The simplest option is an integer that increases by one with every change. Rails uses a lock_version column this way and raises a StaleObjectError on a stale save, leaving conflict resolution to the application (Rails API). A last-modified timestamp is a weaker choice: RFC 9110 notes that a time value with one-second resolution may not distinguish two changes made within the same second.
3. Check the version on the server in a single statement
The form sends the version the user saw. The server writes the change only conditionally, for example:
UPDATE orders SET delivery_address = $1, version = version + 1
WHERE id = $2 AND version = $3;
If the statement updates no row, it is a conflict. In PostgreSQL, at the default Read Committed level, a second concurrent update waits for the first one and re-evaluates the WHERE condition against the updated row (PostgreSQL: Transaction Isolation). A stale version therefore writes nothing. Do not implement the check as “load, compare, then save” in separate steps; a change can slip in between them.
4. Use ETag and If-Match for APIs
If records are edited by mobile apps or integrations, the server sends the version in the ETag header and the client returns it in the If-Match header when making a change. RFC 9110 explicitly states that an ETag can be based on an internal revision number, that If-Match is evaluated using strong comparison and that on a mismatch the server must not perform the requested change; it may respond with 412 Precondition Failed (RFC 9110). If you want to require conditional requests, status 428 Precondition Required is intended precisely to prevent lost updates, and the response should explain how to resubmit the request (RFC 6585).
5. Design a conflict screen
A conflict is a normal situation, not a system error. Show who changed the record and when, which fields differ, and offer a clear next step: load the current version and reapply your own changes. Never discard text the user has typed; at least let them copy it.
6. Change numbers atomically and prepare for retries
For stock or capacity, do not send a new state from the form (“7 pcs left”) but an operation (“subtract 3 pcs”) that the server executes in a single transaction. If you use the stricter Repeatable Read or Serializable levels, PostgreSQL may abort a transaction because of a conflict, and the application should retry it from the beginning; serialization failures have the SQLSTATE code 40001 (PostgreSQL). Explicit locks such as SELECT FOR UPDATE can block other users, so hold them for as short a time as possible.
Trade-offs to expect
- More interface work: the conflict screen and field comparison are a separate feature that has to be designed and tested.
- Transition for integrations: an older client that does not send the version will start receiving errors once the check becomes mandatory. Plan a transition period and documentation.
- Field-level merging: automatically merging changes in different fields is convenient, but it must respect dependencies, for example between price and discount.
- Locks versus flow: exclusive editing reduces conflicts, but people wait and forgotten locks pile up.
The most common mistakes
- Checking the version only in the browser, not on the server.
- Not increasing the version for imports, integrations or bulk edits from the admin area.
- Forms that send all fields, including unchanged ones, so they overwrite things the user never edited.
- Automatically retrying a save with stale data after a conflict, which merely masks the conflict.
- A generic “An error occurred” message and lost unsaved input.
- Locks without a timeout and without a way to release them.
Pre-deployment checklist
- We have a list of records edited by several people or systems.
- Each such record has a version number that every write changes, including integrations.
- The server writes conditionally and changes nothing on a version mismatch.
- The API returns an ETag, accepts If-Match and responds with 412 on a conflict.
- The user sees who changed what and does not lose unsaved input.
- Stock and capacity numbers are changed by atomic operations.
- A test with two browser windows and concurrent API requests has been carried out.
- We log conflicts so we can see where they occur most often.
Frequently asked questions
Isn’t it enough that the application uses database transactions?
No. A transaction protects a single request to the server. A lost update happens between opening a form and saving it, that is, across two requests. That requires a version number sent with the form.
Isn’t it simpler to lock a record when someone opens it?
Sometimes, especially for long documents that are hard to merge. For ordinary forms, however, locks often block colleagues and are left hanging. Even with locks, we recommend keeping the version check as a safeguard.
Can a version check be added to an existing application?
Usually yes, step by step. Start with the record type with the greatest impact, add a version, a conditional write and a conflict screen. Switch integrations to a mandatory check only after a transition period. The effort depends on how many places write to the record.
How do we find out whether the problem affects us?
Open the same record in two browser windows, change a different field in each and save both. If one change is lost without a warning after refreshing the page, the application allows lost updates.
Need an application that does not lose changes?
Webiant designs and improves custom web applications and business software so that several people can work with the same data without silent overwrites. If other systems also change your records, we can help with API integrations that include version checks. Tell us where your changes disappear via the contact form. You will find more practical guides on our blog.
Sources
- RFC 9110: HTTP Semantics (IETF), sections on conditional requests, ETag, If-Match and status 412
- RFC 6585: Additional HTTP Status Codes (IETF), status 428 Precondition Required
- PostgreSQL Documentation: Transaction Isolation
- Ruby on Rails API: ActiveRecord::Locking::Optimistic
- W3C Note: Detecting the Lost Update Problem Using Unreserved Checkout