It's difficult to reproduce the error, but the gist of it should go like this:
say we have script_A and script B.
In script_A we do (t1-t5 are the times when each command happens):
while in script_B we do:
Because the locking only happens for the duration of the REST call, the update in script_A will override the changes that script_A made.
Possible solution is returning to optimistic locking with versions.