N
N
Nwton2017-08-14 11:41:16
MongoDB
Nwton, 2017-08-14 11:41:16

What is the correct way to save mongoose document in this case?

Let there be a document with a numeric parameter. And the server receives requests to change this parameter (n + 1 or n-1) several times a second.
Let's say we received 2 requests almost simultaneously and ran the processing function 2 times. The function gets the document, substitutes a new number and saves. But due to asynchrony and a delay in the execution of the script, it may turn out that when #2 request has already been processed and the document has been saved, #1 is still in progress and after its completion we will lose the result of processing request #2. How to solve this problem?
Alternatively, create a new special document for each request, and then process them all synchronously once a minute, save the result to the main document and delete the special ones. Are there other ways?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
de1m, 2017-08-14
@Nwton

In this case, it is necessary to make a queue, when two requests arrive, then the last one queues up and waits until the first one is completely executed (sends its status to the client) and only after that the second one starts working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question