简体   繁体   中英

Multiple node request handling

I am developing a dynamic application where a huge number of request comes in a quick time. The flow be like,

First request comes in before the end of that second requests come in. I want to make the second request wait until the first request has completed. How can I do that? Any generalized ideas are required.

PS The first request does some changes in the database and the second request's data is reflected in it. So, I have to make the second request wait until the first one completes the process.

I think your basic problem is how to avoid database race condition ( A generic answer to a generic question ) in case of multiple write request being made to the node server. Well, if this is the case, your problem is not new and there are ways to avoid it or manage it, I will try to list and explain some of them and I think you can be the best judge of which one suits your scenario.

  1. Locking the database or record

    You need to "read your writes", which means before you write down a change, you need to read the record again and check if any changes where made to it since you last read it. You can do this field-by-field (fine-grained) or based on a timestamp (coarse-grained). While you do this check you need an exclusive lock on the record. If no changes were made, you can write down your changes and release the lock. If the record has changed in the meantime, you abort the transaction, release the lock and notify the user.

    Referenced from this article.


  1. Version Control : Consider this is the database record with versioning implemented {id:1, status: unimportant, version:5} And now you have 3 to 4 requests lined up which have no order of execution.

    Each request can have following pseudo code: Request 1

      1. Read record (r1) 2. if (the version of previosuly read rrecord is <= recordToSave) : GoTo Step 5 3. update record 4. else : read again 5. Update Database and Increament version 

  1. Event Sourcing : It's an application where we capture all changes to an application state as a sequence of events in temporary Queue which can be a simple FIFO or Weigthage based, etc.

    In this way, you can store all your requests in a queue and resolve them one-by-one without losing any of the requests and maintaining the flow. Please refer this article before you dive into implementation.

    This evenstore module is one of the modules which helps to create a CQRS event store in NodeJS


This is just tip of the iceberg, you may want to go through following references before you decide on which is best for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM