NodeJS如何扩展企业应用程序?

时间:2022-04-11 20:11:59

Suppose I have an enterprise Java application that basically does the following:

假设我有一个企业Java应用程序,其基本功能如下:

gather user input, query the backend databases (maybe multiple), run some algorithm (say do some in-memory calculation of the queried data sets to produce some statistics etc.), then return the data in some html pages.

收集用户输入,查询后端数据库(可能是多个),运行一些算法(比如对查询的数据集进行内存计算以生成一些统计数据等等),然后返回一些html页面中的数据。

My question is: If the bottleneck of the application is on the db query, how can NodeJS helps me in this scenarios since I still need to do all those post-db algorithm before I render the page? How an application architecture looks like?

我的问题是:如果应用程序的瓶颈在db查询上,NodeJS如何在这种情况下帮助我呢?应用程序架构是什么样子的?

1 个解决方案

#1


3  

Of course node can't speed up your storage layer and make that single request that's incurring so much backend processing satisfy that request any faster to the end user. But what it can do is not tie up a thread in the application server thread pool. The single thread can continue on it's loop while that work is going on and accept another request.

当然,node不能加快存储层的速度,不能让产生如此多后端处理的单个请求更快地满足最终用户的请求。但是,它所能做的不是在应用服务器线程池中绑定一个线程。当工作正在进行并接受另一个请求时,单个线程可以继续执行它的循环。

That other request might be a cheaper request that will return when it's work is done. That can also happen in an application server with a thread pool model ... that is unless all the threads in the thread pool model are tied up blocked on I/O requests - along with the overhead of each thread. The cheaper request will get queued waiting on a thread out of the thread pool because they are all blocking. Nodes single thread would loop and server the cheap request.

另一个请求可能是一个更便宜的请求,在完成工作时返回。在具有线程池模型的应用服务器中也可能发生这种情况……除非线程池模型中的所有线程都被阻塞在I/O请求上,以及每个线程的开销。更便宜的请求将排队等待线程池中的线程,因为它们都是阻塞的。节点单线程将循环和服务器廉价的请求。

This works because node mandates that all I/O is async and the only work that blocks the loop is your code. That's why the saying "everything in node runs in parallel except your code". While it's possible to write async code in other application servers and achieve similar results, many offer non-async thread pool models where the coding is easier but sometimes less scalable.

这是可行的,因为node要求所有I/O都是异步的,而阻塞循环的惟一工作就是代码。这就是为什么说“节点上的所有东西都并行运行,除了您的代码”。虽然可以在其他应用程序服务器中编写异步代码并实现类似的结果,但是很多都提供了非异步线程池模型,在这些模型中,编码更容易,但有时可扩展性更差。

For example, this hanselman post illustrates how asp.net is capable of doing async requests but it's not the common model that most have used.

例如,这篇hanselman文章说明了asp.net如何能够执行异步请求,但是它不是大多数人使用的通用模型。

#1


3  

Of course node can't speed up your storage layer and make that single request that's incurring so much backend processing satisfy that request any faster to the end user. But what it can do is not tie up a thread in the application server thread pool. The single thread can continue on it's loop while that work is going on and accept another request.

当然,node不能加快存储层的速度,不能让产生如此多后端处理的单个请求更快地满足最终用户的请求。但是,它所能做的不是在应用服务器线程池中绑定一个线程。当工作正在进行并接受另一个请求时,单个线程可以继续执行它的循环。

That other request might be a cheaper request that will return when it's work is done. That can also happen in an application server with a thread pool model ... that is unless all the threads in the thread pool model are tied up blocked on I/O requests - along with the overhead of each thread. The cheaper request will get queued waiting on a thread out of the thread pool because they are all blocking. Nodes single thread would loop and server the cheap request.

另一个请求可能是一个更便宜的请求,在完成工作时返回。在具有线程池模型的应用服务器中也可能发生这种情况……除非线程池模型中的所有线程都被阻塞在I/O请求上,以及每个线程的开销。更便宜的请求将排队等待线程池中的线程,因为它们都是阻塞的。节点单线程将循环和服务器廉价的请求。

This works because node mandates that all I/O is async and the only work that blocks the loop is your code. That's why the saying "everything in node runs in parallel except your code". While it's possible to write async code in other application servers and achieve similar results, many offer non-async thread pool models where the coding is easier but sometimes less scalable.

这是可行的,因为node要求所有I/O都是异步的,而阻塞循环的惟一工作就是代码。这就是为什么说“节点上的所有东西都并行运行,除了您的代码”。虽然可以在其他应用程序服务器中编写异步代码并实现类似的结果,但是很多都提供了非异步线程池模型,在这些模型中,编码更容易,但有时可扩展性更差。

For example, this hanselman post illustrates how asp.net is capable of doing async requests but it's not the common model that most have used.

例如,这篇hanselman文章说明了asp.net如何能够执行异步请求,但是它不是大多数人使用的通用模型。