简体   繁体   English

多节点请求处理

[英]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. PS第一个请求在数据库中进行了一些更改,第二个请求的数据反映在其中。 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. 版本控制 :考虑这是已实现版本控制的数据库记录{id:1, status: unimportant, version:5}现在,您已经排成3到4个没有执行顺序的请求。

    Each request can have following pseudo code: Request 1 每个请求可以具有以下伪代码:请求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. 事件源 :在此应用程序中,我们将对应用程序状态的所有更改捕获为临时队列中的事件序列,这些事件可以是简单的FIFO或基于Weigthage等。

    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 evenstore模块是有助于在NodeJS中创建CQRS事件存储的模块之一


This is just tip of the iceberg, you may want to go through following references before you decide on which is best for you. 这只是冰山一角,您可能需要先阅读以下参考资料,然后再确定最适合您的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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