There is a document export worker with exponential backoff: if a network error occurred, for example, it will retry operation in 35 sec, 246 sec, 1027 sec and so forth.
The worker is built on top of GenServer and documents are plain maps.
Naturally, this opens the door to a race condition when an operation started earlier can overwrite most recent changes.
Req A +---------> Failure +-------------------------------> Success
Req B +--------> Success
It’s worth noticing that insert and create operations are the same HTTP PUT operations and have no difference whatsoever.
To solve this problem I want to introduce operation merging, so when new upsert is scheduled:
- It will check for presence of any worker operating on the same document (thanks for recent Registry release!)
- If there is one and it’s a delete operation, it will be jut stopped
- If there is one and it’s an upsert operation too then:
- Its document will be fetched and merge with the new one
%{old_doc | new_doc}
- It will be stopped
- A new worker with updated document will be spawned
In a case of scheduling delete operation scenario is a little simpler: if there is a worker operating on the same document it will be just stopped.
Is this a good plan or is it overcomplicated/oversimplified? What might I be missing?
And advice will be appreciated as well.