I am currently writing a Node application, and I am thinking ahead in scaling. As I understand it, horizontal scaling is one of the easiest ways to scale an application to handle more concurrent requests. My working copy is currently using MongoDb on the server.
My question is this: I have a data structure that resembles a linked list that requires strict order maintenance. My (imaginary) concern is that when there is a race condition in the database using multiple Node instances, it is possible that the resolution of the linked list will be incorrect.
To give an example: imagine that the server has this list a-> b. Instance 1 enters object c, and instance 2 enters object d. It is possible that there is a race condition in which both instances read a-> b and decide to add their own objects to the list. Then instance 1 suggested that its insertion is a-> b-> c, and instance 2 believes that a-> b-> d when the database actually contains a-> b-> c-> d.
In general, this seems to work for an optimistic lock, however, as far as I understand, neither MongoDB nor Redis (the other database that I am considering) do transactions in the SQL Mac.
Therefore, I believe that the solution should be one of the following:
Implement my own transaction in MongoDB using flags. The client executes findAndModify on the lock variable and, if successful, performs operations. If this is unsuccessful, the client retries after a certain timeout.
Use Redis and pubsub transactions to achieve the same effect. I'm not quite sure how to do this, but it seems like it could be believable.
Introducing some kind of smart load balancing. If multiple clients are running on the same element, lay them in the same instance. Since JS is single-threaded, the problem will be solved. Unfortunately, I did not find a direct solution for this.
I am sure that there is a better, more elegant way to achieve the above, and I would like to hear any solutions or suggestions. Thanks!
source share