Test for lost updates: two clients, one record

Ann opens an order and changes the delivery address. Bob opens the same order a second later and changes the quantity. Ann saves. Bob saves. Bob's request carried the old address, so Ann's change is gone, and nobody got an error.

That's a lost update. It doesn't show up in single-user tests, and it's easy to reproduce with two requests in a collection.

Reproduce it

Four requests in a folder, in this order:

  1. Ann reads: GET {{baseUrl}}/orders/{{orderId}}
  2. Bob reads: the same request
  3. Ann writes: PUT with a changed address
  4. Bob writes: PUT with a changed quantity, built from Bob's copy

Store each copy in a post-response script:

JavaScript
// "Ann reads"
pm.environment.set('annCopy', pm.response.text());
JavaScript
// "Bob reads"
pm.environment.set('bobCopy', pm.response.text());

Build each write from its copy in a pre-request script:

JavaScript
// "Ann writes" pre-request
const order = JSON.parse(pm.environment.get('annCopy'));
order.address = 'Marszałkowska 1, Warszawa';
pm.request.body.raw = JSON.stringify(order);
JavaScript
// "Bob writes" pre-request
const order = JSON.parse(pm.environment.get('bobCopy'));
order.quantity = 3;
pm.request.body.raw = JSON.stringify(order);

Then add a fifth request, Read final, that checks both changes:

JavaScript
const order = pm.response.json();
pm.test('Ann\'s address is kept', () => pm.expect(order.address).to.equal('Marszałkowska 1, Warszawa'));
pm.test('Bob\'s quantity is kept, or Bob was told', () => pm.expect(order.quantity).to.be.oneOf([3, JSON.parse(pm.environment.get('annCopy')).quantity]));

If "Ann's address is kept" fails, the API loses updates.

What a protected API does

With optimistic locking, every read returns a version, and every write must send the version it was based on. Ann's write succeeds and bumps the version. Bob's write carries the old version and is refused.

Two common shapes:

A version field in the body

JSON
{ "id": "ord_42", "version": 7, "address": "…", "quantity": 1 }

An ETag header with If-Match

HTTP
PUT {{baseUrl}}/orders/{{orderId}}
If-Match: {{bobEtag}}
Content-Type: application/json

Save the ETag from each read with pm.environment.set('bobEtag', pm.response.headers.get('ETag')). The ETag article covers the header side in detail.

The checks for a protected API

JavaScript
// "Ann writes"
pm.test('fresh write succeeds', () => pm.expect(pm.response.code).to.be.oneOf([200, 204]));
JavaScript
// "Bob writes"
pm.test('stale write is refused', () => pm.expect(pm.response.code).to.be.oneOf([409, 412]));
pm.test('error says why', () => pm.expect(pm.response.text().toLowerCase()).to.match(/version|conflict|modified|precondition/));

And one more case that teams forget: a write without any version. Decide whether it's allowed (last write wins) or required (428 Precondition Required), and test that decision.

True concurrency

The folder above runs in sequence, which is enough to show a lost update caused by stale reads. It won't catch two writes that arrive at the same millisecond and both pass the version check. For that, fire them together from a terminal:

Terminal
for q in 2 3; do
  curl -s -o /dev/null -w "%{http_code}\n" -X PUT "$BASE_URL/orders/$ORDER_ID" \
    -H "Authorization: Bearer $TOKEN" -H "If-Match: $ETAG" \
    -H "Content-Type: application/json" -d "{\"quantity\": $q}" &
done
wait

Exactly one should succeed. Two 200s mean the check and the write aren't in one transaction.