- Node.js Web Development
- David Herron
- 577字
- 2021-06-25 21:53:56
Is Node.js a cancerous scalability disaster?
In October 2011, software developer and blogger Ted Dziuba wrote a blog post (since pulled from his blog) titled Node.js is a cancer, calling it a scalability disaster. The example he showed for proof is a CPU-bound implementation of the Fibonacci sequence algorithm. While his argument was flawed, he raised a valid point that Node.js application developers have to consider the following: where do you put the heavy computational tasks?
A key to maintaining high throughput of Node.js applications is ensuring that events are handled quickly. Because it uses a single execution thread, if that thread is bogged down with a big calculation, Node.js cannot handle events, and event throughput will suffer.
The Fibonacci sequence, serving as a stand-in for heavy computational tasks, quickly becomes computationally expensive to calculate, especially for a na?ve implementation such as this:
const fibonacci = exports.fibonacci = function(n) { if (n === 1 || n === 2) return 1; else return fibonacci(n-1) + fibonacci(n-2); }
Yes, there are many ways to calculate fibonacci numbers more quickly. We are showing this as a general example of what happens to Node.js when event handlers are slow, and not to debate the best ways to calculate mathematics functions. Consider this server:
const http = require('http'); const url = require('url'); const fibonacci = // as above http.createServer(function (req, res) { const urlP = url.parse(req.url, true); let fibo; res.writeHead(200, {'Content-Type': 'text/plain'}); if (urlP.query['n']) { fibo = fibonacci(urlP.query['n']); res.end('Fibonacci '+ urlP.query['n'] +'='+ fibo); } else { res.end('USAGE: http://127.0.0.1:8124?n=## where ## is the Fibonacci number desired'); } }).listen(8124, '127.0.0.1'); console.log('Server running at http://127.0.0.1:8124');
For sufficiently large values of n (for example, 40), the server becomes completely unresponsive because the event loop is not running, and instead this function is blocking event processing because it is grinding through the calculation.
Does this mean that Node.js is a flawed platform? No, it just means that the programmer must take care to identify code with long-running computations and develop solutions. These include rewriting the algorithm to work with the event loop, or rewriting the algorithm for efficiency, or integrating a native code library, or foisting computationally expensive calculations on to a backend server.
A simple rewrite dispatches the computations through the event loop, letting the server continue to handle requests on the event loop. Using callbacks and closures (anonymous functions), we're able to maintain asynchronous I/O and concurrency promises:
const fibonacciAsync = function(n, done) {
if (n === 0) return 0; else if (n === 1 || n === 2) done(1);
else if (n === 3) return 2; else { process.nextTick(function() { fibonacciAsync(n-1, function(val1) { process.nextTick(function() { fibonacciAsync(n-2, function(val2) {
done(val1+val2); }); }); }); }); } }
Because this is an asynchronous function, it necessitates a small refactoring of the server:
const http = require('http'); const url = require('url'); const fibonacciAsync = // as above http.createServer(function (req, res) { let urlP = url.parse(req.url, true); res.writeHead(200, {'Content-Type': 'text/plain'}); if (urlP.query['n']) { fibonacciAsync(urlP.query['n'], fibo => {
res.end('Fibonacci '+ urlP.query['n'] +'='+ fibo);
});
} else {
res.end('USAGE: http://127.0.0.1:8124?n=## where ## is the Fibonacci number desired');
}
}).listen(8124, '127.0.0.1'); console.log('Server running at http://127.0.0.1:8124');
Dziuba's valid point wasn't expressed well in his blog post, and it was somewhat lost in the flames following that post. Namely, that while Node.js is a great platform for I/O-bound applications, it isn't a good platform for computationally intensive ones.
Later in this book, we'll explore this example a little more deeply.
- 深入核心的敏捷開發:ThoughtWorks五大關鍵實踐
- 軟件項目估算
- R語言數據分析從入門到精通
- JavaScript+jQuery網頁特效設計任務驅動教程(第2版)
- 用Flutter極速構建原生應用
- 信息技術應用基礎
- 碼上行動:用ChatGPT學會Python編程
- Access 2010中文版項目教程
- Creating Data Stories with Tableau Public
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- 計算機應用基礎(第二版)
- Machine Learning for OpenCV
- 少兒編程輕松學(全2冊)
- Clojure編程樂趣
- 算法訓練營:海量圖解+競賽刷題(入門篇)