Isolation (simultaneous doc updates): how to ensure that all changes persisted?

Probably dumb question, but I searched forum and didn’t find any clear answer (wrong keywords, perhaps):

taking into account async nature of SG, what is the best pattern to ensure that all simultaneous updates to one doc in SG will persist as if they happened in sequence?

What I mean:

Let’s say we have in SG doc containing an array [A, B, C]. Web worker (multi-treaded) recieves two different requests to change doc:

(1) SG (doc[ABC]) ==get==> doc[ABC] worker(add D) => [ABCD] ==put==> SG([ABCD])
(2) SG (doc[ABC]) ==get==> doc[ABC] worker(remove B) => [AC] ==put==> SG([AC])

If (let’s assume) these changes happen simultaneously we will have in SG two doc revisions:
[ABCD] and [AC], one of which will eventually win (if I understand _rev concept right) - and that means that one change to doc was completely lost.

The question is - what is the best way to ensure that changes happen in strict queue and resulting doc will have consistent data - [ACD] ?

I see option to implement changes queue mechanism in worker and add “locked: true” prop todoc indicating that doc is being changed right now and worker should wait and retry next change in a while.

Or there could be more simple approach?

when (1) update success , (2) will fail with 409 conflict. you should get doc with latest rev. then update doc with the latest rev and doc. you will get [ACD].

1 Like

You are completely right, I missed the 409 conflict point, we can’t save doc unless it has the latest revision.
Thank you a lot!

This conflicting revisions (with winning one eventually) situation could happen only for mobile offline writers, which eventually sync their versions to SG.