Hello, I am seeking your expertise and creative guidance on a project I have been struggling to solve. My goal is to enable users to manipulate a collection of orders in a stock market application. It is imperative that these orders are executed one by one, as each order corresponds to a single document in the stock's collection. Despite my efforts to implement manual locking methods, I am facing challenges with orders executing out of sequence.
var stringType = "locked";
var checkIt = await stocksLS.collection(data[5]).findOneAndUpdate({"editor": {$exists:false}},
{$set: {editor: [stringType]}},
{
sort:{"Price":1,"TS":1}
}
);
The code snippet above demonstrates my attempt to lock the next best order for execution, based on price and timestamp. However, simultaneous order executions by multiple users result in future orders being processed prematurely, disrupting the intended sequence.
My proposed solution involves applying locks to all documents during an order execution, and allowing incoming orders to repeatedly attempt to queue up until they can successfully secure a lock.
I acknowledge that there may be more efficient solutions to this issue, and I welcome any suggestions or ideas you may have to improve the process. Your input would be invaluable in helping me explore better alternatives. Thank you for your time and consideration.